"""Sentry common functions.""" from src.common.secrets_manager import LambdaSecretsManager import sentry_sdk from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration def init(sentry_dsn, environment): """ Initilize sentry_sdk. It's safe to init with empty or None values. In that case sentry_sdk perform zero-configuration. Args: sentry_dsn (str): using common_config.SENTRY_DSN as default value. environment (str): using common_config.ENVIRONMENT as default value.. """ sentry_sdk.init( dsn=sentry_dsn, environment=environment, integrations=[AwsLambdaIntegration()] ) def capture_exception(exception=None): """ Send exception to Sentry. If no exception provided then send current exception from sys.exc_info(). """ sentry_sdk.capture_exception(exception) def capture_message(message): """Send message to Sentry.""" sentry_sdk.capture_message(message) def init_for_lambda(lambda_name, env): """ Initialize sentry for given lambda name. The SENTRY_DSN is taken from secrets manger. Note: It does nothing if run in test environment. """ if env != 'test': secrets_manager_client = LambdaSecretsManager(lambda_name, env) init(secrets_manager_client.get_cred('SENTRY_DSN'), env)