"""Sentry Client. Provides sentry_client attribute for ad hoc capturing of messages and errors. https://docs.getsentry.com/hosted/clients/python/usage/ """ import sentry_sdk from sentry_sdk import configure_scope from sentry_sdk import utils as sentry_sdk_utils from sentry_sdk.integrations.flask import FlaskIntegration from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration from product import config sentry_client = None def init_sentry(sentry_dsn): """Init Sentry with provided DSN url.""" global sentry_client if sentry_dsn: sentry_sdk_utils.MAX_STRING_LENGTH = 2048 sentry_sdk.init( dsn=sentry_dsn, environment=config.ENVIRONMENT, debug=False, integrations=[ # https://docs.sentry.io/platforms/python/flask/#options FlaskIntegration(transaction_style="url"), # https://docs.sentry.io/platforms/python/sqlalchemy/ SqlalchemyIntegration(), ], ) sentry_client = sentry_sdk.client def send_to_sentry(message, errors, status, sentry_message): """Send to Sentry.""" with configure_scope() as scope: scope.set_extra("message", message) scope.set_extra("errors", errors) scope.set_extra("status", status) sentry_sdk.capture_message(sentry_message)