Sentry Helpers

class keg_elements.sentry.SentryEventFilter(types=frozenset({}), var_names=frozenset({}), modules=frozenset({}))

Filter used with Sentry SDK to prevent secrets from being uploaded in exception reports.

Filtering may happen based on: - variable name (e.g. password, key, token) - module (e.g. cryptography, itsdangerous) - config keys (e.g. SECRET_KEY)

If you subclass or override, and you are using the install_sentry method to set up Sentry SDK, you must pass your event filter instance to install_sentry.

Types, variable names, and module sets can be provided in the constructor to extend filtering on the defaults.

class keg_elements.sentry.SentryMonitor(org: str, key: str, env: str)

Sentry allows monitoring scheduled job execution, and exposes this feature in its API. SentryMonitor wraps usage.

Init the class with the Sentry org name, “base” monitor key, and the environment to tie the monitor to in Sentry.

Note: environment is not yet well-supported. At the time of this writing, there is no support for removing a single environment. So, monitors will currently be single-env, and each monitor key is given a suffix of the env name.

Config YAML format:

jobs:
    my-cron-job:
        checkin_margin: 1 # in minutes
        schedule_type: crontab
        schedule: '30 2 * * *'
        max_runtime: 1 # in minutes
        timezone: Etc/UTC
    other-stats-run:
        checkin_margin: 1
        schedule_type: interval
        schedule: [6, minute]
        max_runtime: 1
        timezone: Etc/UTC
classmethod apply_config(org: str, config_data: dict)

Runs monitor config on Sentry API for each job in config data.

classmethod read_config(path)

Safe-load YAML from the file at path.

exception keg_elements.sentry.SentryMonitorConfigError
exception keg_elements.sentry.SentryMonitorError
keg_elements.sentry.install_sentry(app, integrations, release=None, event_filter=None, **kwargs)

Init Sentry SDK with helpful defaults and an event filter.

keg_elements.sentry.sentry_monitor_job(org: str, key: str, env: str = None, do_ping: bool = False)

Context manager for running in_progress, then ok status pings on a Sentry monitor.

Sentry will not be pinged unless the do_ping kwarg is True (default is False).

env will default to SENTRY_ENVIRONMENT config value if not specified.

Context manager returns the SentryMonitor instance. The wrapped code may ping a different ending status (such as if an error result should occur without an exception). Exceptions will result in an error status.