Skip to content

Commit b2b51d1

Browse files
committed
add to_tracestate and use it in iter_headers
1 parent 676dd54 commit b2b51d1

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

sentry_sdk/hub.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,10 @@ def iter_trace_propagation_headers(self):
693693

694694
yield "sentry-trace", span.to_traceparent()
695695

696+
tracestate = span.to_tracestate()
697+
if tracestate:
698+
yield "tracestate", tracestate
699+
696700

697701
GLOBAL_HUB = Hub()
698702
_local.set(GLOBAL_HUB)

sentry_sdk/tracing.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,9 @@ def continue_from_headers(
254254
def iter_headers(self):
255255
# type: () -> Generator[Tuple[str, str], None, None]
256256
yield "sentry-trace", self.to_traceparent()
257+
tracestate = self.to_tracestate()
258+
if tracestate:
259+
yield "tracestate", tracestate
257260

258261
@classmethod
259262
def from_traceparent(
@@ -313,6 +316,22 @@ def to_traceparent(self):
313316
sampled = "0"
314317
return "%s-%s-%s" % (self.trace_id, self.span_id, sampled)
315318

319+
def to_tracestate(self):
320+
# type: () -> Optional[str]
321+
transaction = self._containing_transaction
322+
323+
if not transaction:
324+
return None
325+
326+
# it should never happen that thre's no tracestate value, so this "or"
327+
# is just insurance
328+
header_value = transaction._sentry_tracestate_value or "{}"
329+
330+
if transaction._third_party_tracestate:
331+
header_value = ",".join([header_value, transaction._third_party_tracestate])
332+
333+
return header_value
334+
316335
def set_tag(self, key, value):
317336
# type: (str, Any) -> None
318337
self._tags[key] = value
@@ -419,8 +438,13 @@ def get_trace_context(self):
419438
if self.status:
420439
rv["status"] = self.status
421440

422-
if self._containing_transaction:
423-
rv["tracestate"] = self._containing_transaction._sentry_tracestate_value
441+
if isinstance(self, Transaction):
442+
transaction = self # type: Optional[Transaction]
443+
else:
444+
transaction = self._containing_transaction
445+
446+
if transaction:
447+
rv["tracestate"] = transaction._sentry_tracestate_value
424448

425449
return rv
426450

0 commit comments

Comments
 (0)