Skip to content

Commit 2cee373

Browse files
committed
Replace custom_sampling_context with attributes in ASGI
1 parent eb2df70 commit 2cee373

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

MIGRATION_GUIDE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Looking to upgrade from Sentry SDK 2.x to 3.x? Here's a comprehensive list of wh
2020
- clickhouse-driver integration: The query is now available under the `db.query.text` span attribute (only if `send_default_pii` is `True`).
2121
- `sentry_sdk.init` now returns `None` instead of a context manager.
2222
- The `sampling_context` argument of `traces_sampler` now additionally contains all span attributes known at span start.
23+
- The `sampling_context` argument of `traces_sampler` doesn't contain the `asgi_scope` object anymore for ASGI frameworks. Instead, the individual properties, if available, are accessible as `asgi_scope.endpoint`, `asgi_scope.path`, `asgi_scope.root_path`, `asgi_scope.route`, `asgi_scope.scheme`, `asgi_scope.server` and `asgi_scope.type`.
2324

2425
### Removed
2526

sentry_sdk/integrations/asgi.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ async def _run_app(self, scope, receive, send, asgi_version):
209209
name=transaction_name,
210210
source=transaction_source,
211211
origin=self.span_origin,
212-
custom_sampling_context={"asgi_scope": scope},
212+
attributes=_prepopulate_attributes(scope),
213213
)
214214
if should_trace
215215
else nullcontext()
@@ -324,3 +324,16 @@ def _get_transaction_name_and_source(self, transaction_style, asgi_scope):
324324
return name, source
325325

326326
return name, source
327+
328+
329+
def _prepopulate_attributes(scope):
330+
# type: (Any) -> dict[str, Any]
331+
"""Unpack asgi_scope into serializable attributes."""
332+
scope = scope or {}
333+
334+
attributes = {}
335+
for attr in ("endpoint", "path", "root_path", "route", "scheme", "server", "type"):
336+
if scope.get(attr):
337+
attributes[f"asgi_scope.{attr}"] = scope[attr]
338+
339+
return attributes

0 commit comments

Comments
 (0)