Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore/dont-track-homepage-with-sentry #309

Merged
merged 1 commit into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/integrations/sentry/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.conf import settings
from sentry_sdk.integrations.django import DjangoIntegration

from .samplers import block_health_sampler
from .samplers import block_non_functional_endpoints_sampler


class SentryConfig(AppConfig):
Expand All @@ -15,7 +15,7 @@ def ready(self):
dsn=settings.SENTRY_SDK_DSN,
integrations=[DjangoIntegration()],
environment=settings.ENV,
traces_sampler=block_health_sampler,
traces_sampler=block_non_functional_endpoints_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=True,
Expand Down
6 changes: 4 additions & 2 deletions api/integrations/sentry/samplers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.conf import settings


def block_health_sampler(ctx):
def block_non_functional_endpoints_sampler(ctx):
non_functional_endpoints = ["/health", "/"]

sample_rate = settings.SENTRY_TRACE_SAMPLE_RATE
wsgi_env = ctx.get("wsgi_environ")
if wsgi_env and wsgi_env.get("PATH_INFO") == "/health":
if wsgi_env and wsgi_env.get("PATH_INFO") in non_functional_endpoints:
sample_rate = 0
return sample_rate
26 changes: 19 additions & 7 deletions api/integrations/sentry/tests/test_sampler.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,50 @@
from django.test import override_settings

from integrations.sentry.samplers import block_health_sampler
from integrations.sentry.samplers import block_non_functional_endpoints_sampler

SAMPLE_RATE = 10


@override_settings(SENTRY_TRACE_SAMPLE_RATE=SAMPLE_RATE)
def test_block_health_sampler_empty_context_returns_defaut_sample_rate():
def test_block_non_functional_endpoints_sampler_empty_context_returns_defaut_sample_rate():
# When
sample_rate = block_health_sampler({})
sample_rate = block_non_functional_endpoints_sampler({})

# Then
assert sample_rate == SAMPLE_RATE


@override_settings(SENTRY_TRACE_SAMPLE_RATE=SAMPLE_RATE)
def test_block_health_sampler_returns_0_for_health_check_transaction():
def test_block_non_functional_endpoints_sampler_returns_0_for_health_check_transaction():
# Given
ctx = {"wsgi_environ": {"PATH_INFO": "/health"}}

# When
sample_rate = block_health_sampler(ctx)
sample_rate = block_non_functional_endpoints_sampler(ctx)

# Then
assert sample_rate == 0


@override_settings(SENTRY_TRACE_SAMPLE_RATE=SAMPLE_RATE)
def test_block_health_sampler_returns_default_for_non_heath_check_transaction():
def test_block_non_functional_endpoints_sampler_returns_0_for_home_page_transaction():
# Given
ctx = {"wsgi_environ": {"PATH_INFO": "/"}}

# When
sample_rate = block_non_functional_endpoints_sampler(ctx)

# Then
assert sample_rate == 0


@override_settings(SENTRY_TRACE_SAMPLE_RATE=SAMPLE_RATE)
def test_block_non_functional_endpoints_sampler_returns_default_for_non_heath_check_transaction():
# Given
ctx = {"wsgi_env": {"PATH_INFO": "/home"}}

# When
sample_rate = block_health_sampler(ctx)
sample_rate = block_non_functional_endpoints_sampler(ctx)

# Then
assert sample_rate == SAMPLE_RATE