From ea1d7f0e4931c97e4a3155cb8088d5edbf3bf87f Mon Sep 17 00:00:00 2001 From: Melissa Autumn Date: Mon, 29 Apr 2024 09:46:31 -0700 Subject: [PATCH] Reduce sampling and profiling rate to 25% on stage/prod and ignore health check route --- backend/src/appointment/main.py | 34 +++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/backend/src/appointment/main.py b/backend/src/appointment/main.py index 6a3d46cb9..2c24bf0c2 100644 --- a/backend/src/appointment/main.py +++ b/backend/src/appointment/main.py @@ -66,16 +66,32 @@ def _common_setup(): if release_version: release_string = f"appointment-backend@{release_version}" + sample_rate = 0 + profile_traces_max = 0 + environment = os.getenv("APP_ENV", "stage") + + if environment == 'stage': + profile_traces_max = 0.25 + sample_rate = 1.0 + elif environment == 'production': + profile_traces_max = 0.25 + sample_rate = 0.5 + + def traces_sampler(sampling_context): + """Tell Sentry to ignore or reduce traces for particular routes""" + asgi_scope = sampling_context.get('asgi_scope', {}) + path = asgi_scope.get('path') + + # Ignore health check and favicon.ico + if path == '/' or '/favicon.ico': + return 0 + + return profile_traces_max + sentry_sdk.init( dsn=os.getenv("SENTRY_DSN"), - # Set traces_sample_rate to 1.0 to capture 100% - # of transactions for performance monitoring. - # We recommend adjusting this value in production, - traces_sample_rate=1.0, - # Only profile staging for now - profiles_sample_rate=1.0 if os.getenv("APP_ENV", "stage") else 0.0, - send_default_pii=True if os.getenv("APP_ENV", "stage") else False, - environment=os.getenv("APP_ENV", "dev"), + sample_rate=sample_rate, + environment=environment, release=release_string, integrations=[ StarletteIntegration( @@ -85,6 +101,8 @@ def _common_setup(): transaction_style="endpoint" ), ], + profiles_sampler=traces_sampler, + traces_sampler=traces_sampler )