Changeset View
Changeset View
Standalone View
Standalone View
swh/core/sentry.py
# Copyright (C) 2019-2020 The Software Heritage developers | # Copyright (C) 2019-2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import pkg_resources | import pkg_resources | ||||
import os | import os | ||||
def get_sentry_release(): | def get_sentry_release(): | ||||
main_package = os.environ.get('SWH_MAIN_PACKAGE') | main_package = os.environ.get("SWH_MAIN_PACKAGE") | ||||
if main_package: | if main_package: | ||||
version = pkg_resources.get_distribution(main_package).version | version = pkg_resources.get_distribution(main_package).version | ||||
return f'{main_package}@{version}' | return f"{main_package}@{version}" | ||||
else: | else: | ||||
return None | return None | ||||
def init_sentry( | def init_sentry(sentry_dsn, *, debug=None, integrations=[], extra_kwargs={}): | ||||
sentry_dsn, *, debug=None, integrations=[], | |||||
extra_kwargs={}): | |||||
if debug is None: | if debug is None: | ||||
debug = bool(os.environ.get('SWH_SENTRY_DEBUG')) | debug = bool(os.environ.get("SWH_SENTRY_DEBUG")) | ||||
sentry_dsn = sentry_dsn or os.environ.get('SWH_SENTRY_DSN') | sentry_dsn = sentry_dsn or os.environ.get("SWH_SENTRY_DSN") | ||||
environment = os.environ.get('SWH_SENTRY_ENVIRONMENT') | environment = os.environ.get("SWH_SENTRY_ENVIRONMENT") | ||||
if sentry_dsn: | if sentry_dsn: | ||||
import sentry_sdk | import sentry_sdk | ||||
sentry_sdk.init( | sentry_sdk.init( | ||||
release=get_sentry_release(), | release=get_sentry_release(), | ||||
environment=environment, | environment=environment, | ||||
dsn=sentry_dsn, | dsn=sentry_dsn, | ||||
integrations=integrations, | integrations=integrations, | ||||
debug=debug, | debug=debug, | ||||
**extra_kwargs, | **extra_kwargs, | ||||
) | ) |