@@ -254,6 +254,9 @@ def continue_from_headers(
254
254
def iter_headers (self ):
255
255
# type: () -> Generator[Tuple[str, str], None, None]
256
256
yield "sentry-trace" , self .to_traceparent ()
257
+ tracestate = self .to_tracestate ()
258
+ if tracestate :
259
+ yield "tracestate" , tracestate
257
260
258
261
@classmethod
259
262
def from_traceparent (
@@ -313,6 +316,22 @@ def to_traceparent(self):
313
316
sampled = "0"
314
317
return "%s-%s-%s" % (self .trace_id , self .span_id , sampled )
315
318
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
+
316
335
def set_tag (self , key , value ):
317
336
# type: (str, Any) -> None
318
337
self ._tags [key ] = value
@@ -419,8 +438,13 @@ def get_trace_context(self):
419
438
if self .status :
420
439
rv ["status" ] = self .status
421
440
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
424
448
425
449
return rv
426
450
0 commit comments