Skip to content

Commit 61af741

Browse files
committed
Add trace_propagation_meta helper to hub to generate meta tags
1 parent 7b972eb commit 61af741

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

sentry_sdk/hub.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,19 @@ def iter_trace_propagation_headers(self, span=None):
717717
for header in span.iter_headers():
718718
yield header
719719

720+
def trace_propagation_meta(self, span=None):
721+
# type: (Optional[Span]) -> str
722+
"""
723+
Return meta tags which should be injected into the HTML template
724+
to allow propagation of trace data.
725+
"""
726+
meta = ""
727+
728+
for name, content in self.iter_trace_propagation_headers(span):
729+
meta += '<meta name="%s" content="%s">' % (name, content)
730+
731+
return meta
732+
720733

721734
GLOBAL_HUB = Hub()
722735
_local.set(GLOBAL_HUB)

sentry_sdk/tracing_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ def populate_from_transaction(cls, transaction):
477477
Populate fresh baggage entry with sentry_items and make it immutable
478478
if this is the head SDK which originates traces.
479479
"""
480-
481480
hub = transaction.hub or sentry_sdk.Hub.current
482481
client = hub.client
483482
sentry_items = {} # type: Dict[str, str]
@@ -511,6 +510,12 @@ def populate_from_transaction(cls, transaction):
511510
if transaction.sample_rate is not None:
512511
sentry_items["sample_rate"] = str(transaction.sample_rate)
513512

513+
# there's an existing baggage but it was mutable,
514+
# which is why we are creating this new baggage.
515+
# However, if by chance the user put some sentry items in there, give them precedence.
516+
if transaction._baggage and transaction._baggage.sentry_items:
517+
sentry_items.update(transaction._baggage.sentry_items)
518+
514519
return Baggage(sentry_items, mutable=False)
515520

516521
def freeze(self):

0 commit comments

Comments
 (0)