From 1bb5eabde22fcf9afd577816238a9bf250c9a087 Mon Sep 17 00:00:00 2001 From: Tibor Leupold Date: Sat, 9 Sep 2023 13:28:26 -0700 Subject: [PATCH 1/2] Ignore spammy requests --- lpld/settings.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lpld/settings.py b/lpld/settings.py index c60d6df6..48a309fb 100644 --- a/lpld/settings.py +++ b/lpld/settings.py @@ -427,6 +427,26 @@ def show_toolbar(request: "http.HttpRequest") -> bool: SENTRY_ENVIRONMENT = os.environ.get("SENTRY_ENVIRONMENT", "development") HEROKU_RELEASE_VERSION = os.environ.get("HEROKU_RELEASE_VERSION", "") + +def traces_sampler(context: dict) -> float: + """ + Define the sampling rate for Sentry transactions. + + We are using this to ignore spammy requests. + + See also: https://docs.sentry.io/platforms/python/guides/django/configuration/sampling/#setting-a-sampling-function # noqa: E501 + """ + if path := context["wsgi_environ"].get("PATH_INFO"): + if any(( + path.startswith("/wp-"), + path.endswith(".php"), + path.endswith("wlwmanifest.xml"), + )): + # Ignore spammy requests + return 0.0 + return SENTRY_SAMPLE_RATE + + if SENTRY_DSN: sentry_sdk.init( dsn=SENTRY_DSN, @@ -434,7 +454,8 @@ def show_toolbar(request: "http.HttpRequest") -> bool: # 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=SENTRY_SAMPLE_RATE, + # traces_sample_rate=SENTRY_SAMPLE_RATE, + traces_sampler=traces_sampler, # If you wish to associate users to errors (assuming you are using # django.contrib.auth) you may enable sending PII data. send_default_pii=False, From f28df3ae3f741b13f13492175ae9a7366d552017 Mon Sep 17 00:00:00 2001 From: Tibor Leupold Date: Sat, 9 Sep 2023 13:39:17 -0700 Subject: [PATCH 2/2] Fix formatting --- lpld/settings.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lpld/settings.py b/lpld/settings.py index 48a309fb..b4e9c64b 100644 --- a/lpld/settings.py +++ b/lpld/settings.py @@ -437,11 +437,13 @@ def traces_sampler(context: dict) -> float: See also: https://docs.sentry.io/platforms/python/guides/django/configuration/sampling/#setting-a-sampling-function # noqa: E501 """ if path := context["wsgi_environ"].get("PATH_INFO"): - if any(( - path.startswith("/wp-"), - path.endswith(".php"), - path.endswith("wlwmanifest.xml"), - )): + if any( + ( + path.startswith("/wp-"), + path.endswith(".php"), + path.endswith("wlwmanifest.xml"), + ) + ): # Ignore spammy requests return 0.0 return SENTRY_SAMPLE_RATE