Skip to content

Commit 3877df9

Browse files
committed
feat(tracing): Add user data/transaction name to tracestate value (#1177)
This adds user data (specifically `id` and `segment`) and transaction name to the `tracetate` value. Doing this has two known limitations, which, though we've discussed them, I'm adding here for posterity: 1) Adding this data puts us over the character limit for tracestate values [listed in the W3C spec](https://www.w3.org/TR/trace-context/#value) (256 characters). For reference: Tracestate data: ``` { "trace_id": "12312012123120121231201212312012", "environment": "dogpark", "release": "off.leash.park", "public_key": "dogsarebadatkeepingsecrets", "user": {"id": 12312013, "segment": "bigs"}, "transaction": "/interactions/other-dogs/new-dog/" } ``` `tracestate` with without either: 196 characters `tracestate` with user data: 256 characters `tracestate` with transaction name: 264 characters `tracestate` with both: 324 characters 2) This data may change and/or get added to the scope after the tracestate value has been calculated, making it impossible to sample based on those attributes. This is especially a problem for transaction name, which in some frameworks isn't set to its final value until the transaction ends. This poses the added problem that the transaction name in its raw, un-finalized form may contain PII, because it is often the raw URL as opposed to the parameterized one (so, `/users/maisey/tricks/` rather than `/users/:username/tricks/`). More work needs to be done to investigate whether the final transaction name can be set earlier in any/all of the frameworks where this poses a problem. (For instance, it is a known problem in our Express integration, but not yet clear if it is a problem in any Python frameworks.)
1 parent 45b2289 commit 3877df9

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,17 +309,36 @@ def compute_tracestate_entry(span):
309309
"""
310310
data = {}
311311

312-
client = (span.hub or sentry_sdk.Hub.current).client
312+
hub = span.hub or sentry_sdk.Hub.current
313+
314+
client = hub.client
315+
scope = hub.scope
313316

314317
if client and client.options.get("dsn"):
315318
options = client.options
319+
user = scope._user
320+
316321
data = {
317322
"trace_id": span.trace_id,
318323
"environment": options["environment"],
319324
"release": options.get("release"),
320325
"public_key": Dsn(options["dsn"]).public_key,
321326
}
322327

328+
if user and (user.get("id") or user.get("segment")):
329+
user_data = {}
330+
331+
if user.get("id"):
332+
user_data["id"] = user["id"]
333+
334+
if user.get("segment"):
335+
user_data["segment"] = user["segment"]
336+
337+
data["user"] = user_data
338+
339+
if span.containing_transaction:
340+
data["transaction"] = span.containing_transaction.name
341+
323342
return "sentry=" + compute_tracestate_value(data)
324343

325344
return None

tests/tracing/test_http_headers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ def test_tracestate_computation(sentry_init):
2626
release="off.leash.park",
2727
)
2828

29+
sentry_sdk.set_user({"id": 12312013, "segment": "bigs"})
30+
2931
transaction = Transaction(
3032
name="/interactions/other-dogs/new-dog",
3133
op="greeting.sniff",
@@ -46,6 +48,8 @@ def test_tracestate_computation(sentry_init):
4648
"environment": "dogpark",
4749
"release": "off.leash.park",
4850
"public_key": "dogsarebadatkeepingsecrets",
51+
"user": {"id": 12312013, "segment": "bigs"},
52+
"transaction": "/interactions/other-dogs/new-dog",
4953
}
5054

5155

0 commit comments

Comments
 (0)