From 0919fd1c51082e35180025544f279615e7c40e45 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Fri, 27 Aug 2021 14:32:39 -0700 Subject: [PATCH 1/2] add transaction name to tracestate --- sentry_sdk/tracing_utils.py | 3 +++ tests/tracing/test_http_headers.py | 1 + 2 files changed, 4 insertions(+) diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index ec7ad7743f..882b66975e 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -320,6 +320,9 @@ def compute_tracestate_entry(span): "public_key": Dsn(options["dsn"]).public_key, } + if span.containing_transaction: + data["transaction"] = span.containing_transaction.name + return "sentry=" + compute_tracestate_value(data) return None diff --git a/tests/tracing/test_http_headers.py b/tests/tracing/test_http_headers.py index d86c7bf4d4..8a324c7943 100644 --- a/tests/tracing/test_http_headers.py +++ b/tests/tracing/test_http_headers.py @@ -46,6 +46,7 @@ def test_tracestate_computation(sentry_init): "environment": "dogpark", "release": "off.leash.park", "public_key": "dogsarebadatkeepingsecrets", + "transaction": "/interactions/other-dogs/new-dog", } From b2e7c4c3f744847d7631f8cfd3d718ee6cc0b599 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Mon, 30 Aug 2021 08:17:59 -0700 Subject: [PATCH 2/2] add user data to tracestate --- sentry_sdk/tracing_utils.py | 18 +++++++++++++++++- tests/tracing/test_http_headers.py | 3 +++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/tracing_utils.py b/sentry_sdk/tracing_utils.py index 882b66975e..b2714f3e92 100644 --- a/sentry_sdk/tracing_utils.py +++ b/sentry_sdk/tracing_utils.py @@ -309,10 +309,15 @@ def compute_tracestate_entry(span): """ data = {} - client = (span.hub or sentry_sdk.Hub.current).client + hub = span.hub or sentry_sdk.Hub.current + + client = hub.client + scope = hub.scope if client and client.options.get("dsn"): options = client.options + user = scope._user + data = { "trace_id": span.trace_id, "environment": options["environment"], @@ -320,6 +325,17 @@ def compute_tracestate_entry(span): "public_key": Dsn(options["dsn"]).public_key, } + if user and (user.get("id") or user.get("segment")): + user_data = {} + + if user.get("id"): + user_data["id"] = user["id"] + + if user.get("segment"): + user_data["segment"] = user["segment"] + + data["user"] = user_data + if span.containing_transaction: data["transaction"] = span.containing_transaction.name diff --git a/tests/tracing/test_http_headers.py b/tests/tracing/test_http_headers.py index 8a324c7943..2e1f506032 100644 --- a/tests/tracing/test_http_headers.py +++ b/tests/tracing/test_http_headers.py @@ -26,6 +26,8 @@ def test_tracestate_computation(sentry_init): release="off.leash.park", ) + sentry_sdk.set_user({"id": 12312013, "segment": "bigs"}) + transaction = Transaction( name="/interactions/other-dogs/new-dog", op="greeting.sniff", @@ -46,6 +48,7 @@ def test_tracestate_computation(sentry_init): "environment": "dogpark", "release": "off.leash.park", "public_key": "dogsarebadatkeepingsecrets", + "user": {"id": 12312013, "segment": "bigs"}, "transaction": "/interactions/other-dogs/new-dog", }