Skip to content

Commit 7a8654e

Browse files
authored
Add API to set arbitrary context in Transaction. (#1769)
* Api for setting arbitrary contexts in transactions.
1 parent 9d3047e commit 7a8654e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

sentry_sdk/tracing.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,7 @@ class Transaction(Span):
561561
# tracestate data from other vendors, of the form `dogs=yes,cats=maybe`
562562
"_third_party_tracestate",
563563
"_measurements",
564+
"_contexts",
564565
"_profile",
565566
"_baggage",
566567
"_active_thread_id",
@@ -586,7 +587,9 @@ def __init__(
586587
"instead of Span(transaction=...)."
587588
)
588589
name = kwargs.pop("transaction")
590+
589591
Span.__init__(self, **kwargs)
592+
590593
self.name = name
591594
self.source = source
592595
self.sample_rate = None # type: Optional[float]
@@ -597,6 +600,7 @@ def __init__(
597600
self._sentry_tracestate = sentry_tracestate
598601
self._third_party_tracestate = third_party_tracestate
599602
self._measurements = {} # type: Dict[str, Any]
603+
self._contexts = {} # type: Dict[str, Any]
600604
self._profile = None # type: Optional[sentry_sdk.profiler.Profile]
601605
self._baggage = baggage
602606
# for profiling, we want to know on which thread a transaction is started
@@ -685,11 +689,15 @@ def finish(self, hub=None):
685689
# to be garbage collected
686690
self._span_recorder = None
687691

692+
contexts = {}
693+
contexts.update(self._contexts)
694+
contexts.update({"trace": self.get_trace_context()})
695+
688696
event = {
689697
"type": "transaction",
690698
"transaction": self.name,
691699
"transaction_info": {"source": self.source},
692-
"contexts": {"trace": self.get_trace_context()},
700+
"contexts": contexts,
693701
"tags": self._tags,
694702
"timestamp": self.timestamp,
695703
"start_timestamp": self.start_timestamp,
@@ -714,6 +722,10 @@ def set_measurement(self, name, value, unit=""):
714722

715723
self._measurements[name] = {"value": value, "unit": unit}
716724

725+
def set_context(self, key, value):
726+
# type: (str, Any) -> None
727+
self._contexts[key] = value
728+
717729
def to_json(self):
718730
# type: () -> Dict[str, Any]
719731
rv = super(Transaction, self).to_json()

0 commit comments

Comments
 (0)