Skip to content

Commit c559669

Browse files
committed
Use attributes instead of custom_sampling_context in WSGI
1 parent eb2df70 commit c559669

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-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 `wsgi_environ` object anymore. Instead, the individual properties are accessible as `wsgi_environ.PATH_INFO`... If you need more data accessible in the `traces_sampler`, provide additional `attributes` to your `start_span`. # TODO
2324

2425
### Removed
2526

sentry_sdk/integrations/wsgi.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def __call__(self, environ, start_response):
120120
name=DEFAULT_TRANSACTION_NAME,
121121
source=TRANSACTION_SOURCE_ROUTE,
122122
origin=self.span_origin,
123-
custom_sampling_context={"wsgi_environ": environ},
123+
attributes=_prepopulate_attributes(environ),
124124
)
125125
if should_trace
126126
else nullcontext()
@@ -309,3 +309,28 @@ def event_processor(event, hint):
309309
return event
310310

311311
return event_processor
312+
313+
314+
def _prepopulate_attributes(wsgi_environ):
315+
attributes = {}
316+
317+
for attr in (
318+
"CONTENT_LENGTH",
319+
"CONTENT_TYPE",
320+
"PATH_INFO",
321+
"QUERY_STRING",
322+
"REQUEST_METHOD",
323+
"SCRIPT_NAME",
324+
"SERVER_NAME",
325+
"SERVER_PORT",
326+
"SERVER_PROTOCOL",
327+
"multithread",
328+
"multiprocess",
329+
"run_once",
330+
"url_scheme",
331+
"version",
332+
):
333+
if wsgi_environ.get(attr):
334+
attributes[f"wsgi_environ.{attr}"] = wsgi_environ[attr]
335+
336+
return attributes

0 commit comments

Comments
 (0)