Skip to content

Commit 033685c

Browse files
authored
Ignore __lbheartbeat__ requests in Sentry (#363)
In this commit, we: - use dynamic sampling to ignore `/__lbheartbeat__` requests in Sentry The sentry_traces_sample_rateenvironment variable is still present so that we can control the global sample rate of non-`/__lbheartbeat__` requests. - update the way we initialize Sentry to be in line with current documentation - rename test_main to test_app. `main.py` was renamed to `app.py`, but we didn't rename the complementary test module at that time
1 parent 2c4b80c commit 033685c

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

jbi/app.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
import logging
55
import time
66
from pathlib import Path
7-
from typing import Awaitable, Callable
7+
from typing import Any, Awaitable, Callable
88

99
import sentry_sdk
1010
from fastapi import FastAPI, Request, Response
1111
from fastapi.staticfiles import StaticFiles
12-
from sentry_sdk.integrations.fastapi import FastApiIntegration
13-
from sentry_sdk.integrations.starlette import StarletteIntegration
1412

1513
from jbi.environment import get_settings, get_version
1614
from jbi.log import format_request_summary_fields
@@ -21,13 +19,20 @@
2119
settings = get_settings()
2220
version_info = get_version()
2321

22+
23+
def traces_sampler(sampling_context: dict[str, Any]) -> float:
24+
"""Function to dynamically set Sentry sampling rates"""
25+
26+
request_path = sampling_context.get("asgi_scope", {}).get("path")
27+
if request_path == "/__lbheartbeat__":
28+
# Drop all __lbheartbeat__ requests
29+
return 0
30+
return settings.sentry_traces_sample_rate
31+
32+
2433
sentry_sdk.init(
2534
dsn=settings.sentry_dsn,
26-
integrations=[
27-
StarletteIntegration(),
28-
FastApiIntegration(),
29-
],
30-
traces_sample_rate=settings.sentry_traces_sample_rate,
35+
traces_sampler=traces_sampler,
3136
)
3237

3338

poetry.lock

Lines changed: 2 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ atlassian-python-api = "^3.32.1"
1414
dockerflow = "2022.8.0"
1515
Jinja2 = "^3.1.2"
1616
pydantic-yaml = {extras = ["pyyaml","ruamel"], version = "^0.8.1"}
17-
sentry-sdk = "^1.12.1"
17+
sentry-sdk = {extras = ["fastapi"], version = "^1.12.1"}
1818
backoff = "^2.2.1"
1919
statsd = "^4.0.1"
2020
requests = "^2.28.1"

tests/unit/test_main.py renamed to tests/unit/test_app.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import pytest
55
from fastapi.testclient import TestClient
66

7-
from jbi.app import app
7+
from jbi.app import app, traces_sampler
8+
from jbi.environment import get_settings
89
from jbi.models import BugzillaWebhookRequest
910

1011

@@ -33,6 +34,32 @@ def test_request_summary_defaults_user_agent_to_empty_string(caplog):
3334
assert summary.agent == ""
3435

3536

37+
@pytest.mark.parametrize(
38+
"sampling_context,expected",
39+
[
40+
# /__lbheartbeat__
41+
({"asgi_scope": {"path": "/__lbheartbeat__"}}, 0),
42+
# path that isn't /__lbheartbeat__
43+
(
44+
{"asgi_scope": {"path": "/"}},
45+
get_settings().sentry_traces_sample_rate,
46+
),
47+
# context w/o an asgi_scope
48+
(
49+
{"parent_sampled": None},
50+
get_settings().sentry_traces_sample_rate,
51+
),
52+
# context w/o an asgi_scope.path
53+
(
54+
{"asgi_scope": {"type": "lifespan"}},
55+
get_settings().sentry_traces_sample_rate,
56+
),
57+
],
58+
)
59+
def test_traces_sampler(sampling_context, expected):
60+
assert traces_sampler(sampling_context) == expected
61+
62+
3663
def test_errors_are_reported_to_sentry(
3764
anon_client, webhook_create_example: BugzillaWebhookRequest
3865
):

0 commit comments

Comments
 (0)