Changeset View
Changeset View
Standalone View
Standalone View
swh/core/api/gunicorn_config.py
# Copyright (C) 2019 The Software Heritage developers | # Copyright (C) 2019 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 | ||||
"""Default values for gunicorn's configuration. | """Default values for gunicorn's configuration. | ||||
Other packages may override them by importing `*` from this module | Other packages may override them by importing `*` from this module | ||||
and redefining functions and variables they want. | and redefining functions and variables they want. | ||||
May be imported by gunicorn using | May be imported by gunicorn using | ||||
`--config 'python:swh.core.api.gunicorn_config'`.""" | `--config 'python:swh.core.api.gunicorn_config'`.""" | ||||
import os | |||||
from ..sentry import init_sentry | |||||
def _init_sentry( | |||||
sentry_dsn, *, flask=True, integrations=None, extra_kwargs={}): | |||||
import sentry_sdk | |||||
integrations = integrations or [] | |||||
if flask: | |||||
from sentry_sdk.integrations.flask import FlaskIntegration | |||||
integrations.append(FlaskIntegration()) | |||||
sentry_sdk.init( | |||||
dsn=sentry_dsn, | |||||
integrations=integrations, | |||||
debug=bool(os.environ.get('SWH_SENTRY_DEBUG')), | |||||
**extra_kwargs, | |||||
) | |||||
def post_fork( | def post_fork( | ||||
server, worker, *, default_sentry_dsn=None, flask=True, | server, worker, *, default_sentry_dsn=None, flask=True, | ||||
sentry_integrations=None, extra_sentry_kwargs={}): | sentry_integrations=None, extra_sentry_kwargs={}): | ||||
# Initializes sentry as soon as possible in gunicorn's worker processes. | # Initializes sentry as soon as possible in gunicorn's worker processes. | ||||
sentry_dsn = os.environ.get('SWH_SENTRY_DSN', default_sentry_dsn) | |||||
if sentry_dsn: | sentry_integrations = sentry_integrations or [] | ||||
_init_sentry( | if flask: | ||||
sentry_dsn, flask=flask, integrations=sentry_integrations, | from sentry_sdk.integrations.flask import FlaskIntegration | ||||
sentry_integrations.append(FlaskIntegration()) | |||||
init_sentry( | |||||
default_sentry_dsn, integrations=sentry_integrations, | |||||
extra_kwargs=extra_sentry_kwargs) | extra_kwargs=extra_sentry_kwargs) |