Skip to content

Commit 98a6cc6

Browse files
committed
add method to compute a transaction's tracestate value
1 parent 4655650 commit 98a6cc6

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

sentry_sdk/tracing_utils.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
import contextlib
3+
import json
34
import math
45

56
from numbers import Real
@@ -160,6 +161,36 @@ def maybe_create_breadcrumbs_from_span(hub, span):
160161
)
161162

162163

164+
def compute_new_tracestate(transaction):
165+
# type: (Transaction) -> str
166+
"""
167+
Computes a new tracestate value for the transaction.
168+
"""
169+
data = {}
170+
171+
client = (transaction.hub or sentry_sdk.Hub.current).client
172+
173+
# if there's no client and/or no DSN, we're not sending anything anywhere,
174+
# so it's fine to not have any tracestate data
175+
if client and client.options.get("dsn"):
176+
options = client.options
177+
data = {
178+
"trace_id": transaction.trace_id,
179+
"environment": options["environment"],
180+
"release": options.get("release"),
181+
"public_key": Dsn(options["dsn"]).public_key,
182+
}
183+
184+
tracestate_json = json.dumps(data)
185+
186+
# Base64-encoded strings always come out with a length which is a multiple
187+
# of 4. In order to achieve this, the end is padded with one or more `=`
188+
# signs. Because the tracestate standard calls for using `=` signs between
189+
# vendor name and value (`sentry=xxx,dogsaregreat=yyy`), to avoid confusion
190+
# we strip the `=`
191+
return to_base64(tracestate_json).rstrip("=")
192+
193+
163194
def _format_sql(cursor, sql):
164195
# type: (Any, str) -> Optional[str]
165196

0 commit comments

Comments
 (0)