API

Scrubber-related things:

Sentry helpers:

fillmore.scrubber

fillmore.scrubber.scrub(value)

Scrub a value

Parameters:

value (str) –

Return type:

str

fillmore.scrubber.build_scrub_cookies(params)

Scrub specified keys in HTTP request cookies

Sentry says the cookies can be:

  • an unparsed string

  • a dictionary

  • a list of tuples

For the unparsed string, this parses it and figures things out.

For dictionary and list of tuples, this returns the scrubbed forms of those.

If the specified params is ALL_COOKIE_KEYS, then this will scrub all cookie values.

Parameters:

params (List[str]) –

Return type:

Callable

fillmore.scrubber.build_scrub_query_string(params)

Scrub specified keys in an HTTP request query_string

Sentry says the query_string can be:

  • an unparsed string

  • a dictionary

  • a list of tuples

For the unparsed string, this parses it and figures things out. If there’s nothing that needs to be scrubbed, then it returns the original string. Otherwise it returns a query_string value with the items scrubbed, and reformed into a query_string. This sometimes means that other things in the string have changed and that may make debugging issues a little harder.

For dictionary and list of tuples, this returns the scrubbed forms of those.

If the params is ALL_QUERY_STRING_KEYS, then this will scrub all query_string values.

Note

The Sentry docs say that the query_string could be part of the url. This doesn’t handle that situation.

Parameters:

params (List[str]) –

Return type:

Callable

fillmore.scrubber.str2list(s)

Splits a string into parts using “.” as the separator

Parameters:

s (str) –

Return type:

List[str]

exception fillmore.scrubber.RuleError
fillmore.scrubber.thing2fun(thing)

Convert str or Callable to a Callable

If the thing refers to a scrub function in this module, return that.

If the thing is a dotted Python path to some other function, return that.

Raises:

RuleError – if the thing is not a callable or does not exist

Parameters:

thing (Callable | str) –

Return type:

Callable

class fillmore.scrubber.Rule(path, keys, scrub)
Parameters:
  • path (List[str]) – Python dotted path of key names with [] to denote arrays to traverse pointing to a dict with values to scrub.

  • keys (List[str]) – list of keys to scrub values of

  • scrub (Callable) –

    is a callable that takes a value and returns a scrubbed value. For example:

    def hide_letter_a(value>: str) -> str:
        return "".join([letter if letter != "a" else "*" for letter in value])
    

Rule example:

Rule(
    path="request.data",
    keys=["csrfmiddlewaretoken"],
    scrub=scrub,
)

Rule(
    path="request.data",
    keys=["csrfmiddlewaretoken"],
    scrub="somemodule.scrubfunction",
)

Method generated by attrs for class Rule.

exception fillmore.scrubber.RulePathError

The rule path doesn’t match the structure of the event

class fillmore.scrubber.Scrubber(rules=[Rule(path=['exception', 'values', '[]', 'stacktrace', 'frames', '[]', 'vars'], keys=['username', 'password'], scrub=<function scrub>)], error_handler=None)

Scrubber pipeline for Sentry events

This is used as a before_send value as discussed here:

https://docs.sentry.io/platforms/python/configuration/filtering/

You create a fillmore.scrubber.Scrubber with a list of scrub rules. When sentry_sdk is about to emit an event, the fillmore.scrubber.Scrubber applies the scrub rules to the event and returns the scrubbed event data.

If a scrub rule kicks up an error, then the configured error_handler is called.

Parameters:
  • rules (List[Rule]) – list of Rule instances

  • error_handler (Callable | None) –

    function that takes a msg (str) and is called when either a scrub rule or getting the specified key by path in the Sentry event kicks up an error; this lets you emit some kind of signal when the Sentry scrubbing code is failing so it doesn’t do it silently.

    By default, this logs an exception.

fillmore.libsentry

Utility functions for setting up Sentry.

fillmore.libsentry.set_up_sentry(sentry_dsn, release, host_id, integrations=None, before_send=None, **kwargs)

Set up Sentry

By default, this will set up default integrations (https://docs.sentry.io/platforms/python/configuration/integrations/default-integrations/), but not the auto-enabling ones.

Parameters:
  • sentry_dsn (str) – the Sentry DSN

  • release (str) – the release name to tag events with

  • host_id (str) – some str representing the host this service is running on

  • integrations (List[Any] | None) – list of sentry integrations to set up;

  • before_send (Callable | None) –

    set this to a callable to handle the Sentry before_send hook

    For scrubbing, do something like this:

    scrubber = Scrubber(rules=SCRUB_RULES_DEFAULT + my_scrub_rules)
    

    and then pass that as the before_send value.

  • kwargs (Any) – any additional arguments to pass to sentry_sdk.init()

Return type:

None