Skip to content

Commit 9268111

Browse files
authored
feat(aiohttp,sanic,tornado): Add user_agent.original attr for inbound filters (#7795)
Bunched these three together since it's a very small change in each Part 3 of https://linear.app/getsentry/issue/PY-2898/implement-user-agentoriginal
1 parent 5e8bbdf commit 9268111

6 files changed

Lines changed: 112 additions & 0 deletions

File tree

‎sentry_sdk/integrations/aiohttp.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ async def sentry_app_handle(
158158
),
159159
)
160160

161+
user_agent = request.headers.get("User-Agent")
162+
if user_agent:
163+
scope.set_attribute(SPANDATA.USER_AGENT_ORIGINAL, user_agent)
164+
161165
headers = dict(request.headers)
162166

163167
span_ctx: "ContextManager[Union[Span, StreamedSpan]]"

‎sentry_sdk/integrations/sanic.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ async def _context_enter(request: "Request") -> None:
193193
),
194194
)
195195

196+
user_agent = request.headers.get("user-agent")
197+
if user_agent:
198+
scope.set_attribute(SPANDATA.USER_AGENT_ORIGINAL, user_agent)
199+
196200
if is_span_streaming_enabled:
197201
integration = client.get_integration(SanicIntegration)
198202
if (

‎sentry_sdk/integrations/tornado.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ def _handle_request_impl(self: "RequestHandler") -> "Generator[None, None, None]
137137
),
138138
)
139139

140+
user_agent = headers.get("User-Agent")
141+
if user_agent:
142+
scope.set_attribute(SPANDATA.USER_AGENT_ORIGINAL, user_agent)
143+
140144
span_ctx: "ContextManager[Union[Span, StreamedSpan, None]]"
141145

142146
if is_span_streaming_enabled:

‎tests/integrations/aiohttp/test_aiohttp.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,6 +2472,44 @@ async def hello(request):
24722472
assert child_spans[0]["attributes"][SPANDATA.SENTRY_IS_LOCALHOST] == is_localhost
24732473

24742474

2475+
@pytest.mark.asyncio
2476+
async def test_user_agent_attribute(sentry_init, aiohttp_client, capture_items):
2477+
sentry_init(
2478+
integrations=[AioHttpIntegration()],
2479+
traces_sample_rate=1.0,
2480+
trace_lifecycle="stream",
2481+
)
2482+
2483+
async def hello(request):
2484+
with sentry_sdk.traces.start_span(name="child"):
2485+
pass
2486+
return web.Response(text="hello")
2487+
2488+
app = web.Application()
2489+
app.router.add_get("/", hello)
2490+
2491+
items = capture_items("span")
2492+
2493+
client = await aiohttp_client(app)
2494+
resp = await client.get("/", headers={"User-Agent": "TestBrowser/1.0"})
2495+
assert resp.status == 200
2496+
2497+
sentry_sdk.flush()
2498+
2499+
child_spans = [item.payload for item in items if not item.payload.get("is_segment")]
2500+
server_spans = [item.payload for item in items if item.payload.get("is_segment")]
2501+
2502+
assert len(server_spans) == 1
2503+
assert len(child_spans) == 1
2504+
2505+
assert (
2506+
server_spans[0]["attributes"][SPANDATA.USER_AGENT_ORIGINAL] == "TestBrowser/1.0"
2507+
)
2508+
assert (
2509+
child_spans[0]["attributes"][SPANDATA.USER_AGENT_ORIGINAL] == "TestBrowser/1.0"
2510+
)
2511+
2512+
24752513
@pytest.mark.asyncio
24762514
@pytest.mark.parametrize(
24772515
"init_kwargs, expect_remote_addr", DATA_COLLECTION_REMOTE_ADDR_CASES

‎tests/integrations/sanic/test_sanic.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,3 +943,37 @@ def child_span_handler_localhost(request):
943943

944944
assert server_span["attributes"]["sentry.is_localhost"] is is_localhost
945945
assert child_span["attributes"]["sentry.is_localhost"] is is_localhost
946+
947+
948+
@pytest.mark.skipif(
949+
not PERFORMANCE_SUPPORTED, reason="Performance not supported on this Sanic version"
950+
)
951+
def test_user_agent_attribute(sentry_init, app, capture_items):
952+
@app.route("/child-span")
953+
def child_span_handler_ua(request):
954+
with sentry_sdk.traces.start_span(name="child-span"):
955+
pass
956+
return response.text("ok")
957+
958+
sentry_init(
959+
integrations=[SanicIntegration()],
960+
default_integrations=False,
961+
traces_sample_rate=1.0,
962+
trace_lifecycle="stream",
963+
)
964+
965+
items = capture_items("span")
966+
967+
c = get_client(app)
968+
with c as client:
969+
client.get(
970+
"/child-span",
971+
headers={"User-Agent": "TestBrowser/1.0"},
972+
)
973+
974+
sentry_sdk.flush()
975+
976+
child_span, server_span = [item.payload for item in items]
977+
978+
assert server_span["attributes"]["user_agent.original"] == "TestBrowser/1.0"
979+
assert child_span["attributes"]["user_agent.original"] == "TestBrowser/1.0"

‎tests/integrations/tornado/test_tornado.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,34 @@ def test_is_localhost_span_attribute(
11651165
assert child_spans[0]["attributes"][SPANDATA.SENTRY_IS_LOCALHOST] == is_localhost
11661166

11671167

1168+
def test_user_agent_attribute(tornado_testcase, sentry_init, capture_items):
1169+
sentry_init(
1170+
integrations=[TornadoIntegration()],
1171+
traces_sample_rate=1.0,
1172+
trace_lifecycle="stream",
1173+
)
1174+
1175+
items = capture_items("span")
1176+
1177+
client = tornado_testcase(Application([(r"/hi", ChildSpanHandler)]))
1178+
client.fetch("/hi", headers={"User-Agent": "TestBrowser/1.0"})
1179+
1180+
sentry_sdk.flush()
1181+
1182+
child_spans = [item.payload for item in items if not item.payload.get("is_segment")]
1183+
server_spans = [item.payload for item in items if item.payload.get("is_segment")]
1184+
1185+
assert len(server_spans) == 1
1186+
assert len(child_spans) == 1
1187+
1188+
assert (
1189+
server_spans[0]["attributes"][SPANDATA.USER_AGENT_ORIGINAL] == "TestBrowser/1.0"
1190+
)
1191+
assert (
1192+
child_spans[0]["attributes"][SPANDATA.USER_AGENT_ORIGINAL] == "TestBrowser/1.0"
1193+
)
1194+
1195+
11681196
@pytest.mark.parametrize(
11691197
"init_kwargs, expect_remote_addr", DATA_COLLECTION_REMOTE_ADDR_CASES
11701198
)

0 commit comments

Comments
 (0)