From c7977d3b4342ad4013580478e435ef7e130ffa7f Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Wed, 30 Nov 2022 14:40:01 +0100 Subject: [PATCH 1/2] Api for setting arbitrary contexts in spans. --- sentry_sdk/tracing.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index 5b9fbdfd0d..d2be0ce847 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -93,6 +93,7 @@ class Span(object): "timestamp", "_tags", "_data", + "_contexts", "_span_recorder", "hub", "_context_manager_state", @@ -140,6 +141,7 @@ def __init__( self.hub = hub self._tags = {} # type: Dict[str, str] self._data = {} # type: Dict[str, Any] + self._contexts = {} # type: Dict[str, Any] self._containing_transaction = containing_transaction self.start_timestamp = datetime.utcnow() try: @@ -468,6 +470,10 @@ def set_http_status(self, http_status): else: self.set_status("unknown_error") + def set_context(self, key, value): + # type: (str, Any) -> None + self._contexts[key] = value + def is_success(self): # type: () -> bool return self.status == "ok" @@ -685,11 +691,15 @@ def finish(self, hub=None): # to be garbage collected self._span_recorder = None + contexts = {} + contexts.update(self._contexts) + contexts.update({"trace": self.get_trace_context()}) + event = { "type": "transaction", "transaction": self.name, "transaction_info": {"source": self.source}, - "contexts": {"trace": self.get_trace_context()}, + "contexts": contexts, "tags": self._tags, "timestamp": self.timestamp, "start_timestamp": self.start_timestamp, From 89e554b9f700f1ee4f3b20e7d5cc31f99f939f5c Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Wed, 30 Nov 2022 16:28:58 +0100 Subject: [PATCH 2/2] Moved set_context to transaction --- sentry_sdk/tracing.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index d2be0ce847..c2dc1ab581 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -93,7 +93,6 @@ class Span(object): "timestamp", "_tags", "_data", - "_contexts", "_span_recorder", "hub", "_context_manager_state", @@ -141,7 +140,6 @@ def __init__( self.hub = hub self._tags = {} # type: Dict[str, str] self._data = {} # type: Dict[str, Any] - self._contexts = {} # type: Dict[str, Any] self._containing_transaction = containing_transaction self.start_timestamp = datetime.utcnow() try: @@ -470,10 +468,6 @@ def set_http_status(self, http_status): else: self.set_status("unknown_error") - def set_context(self, key, value): - # type: (str, Any) -> None - self._contexts[key] = value - def is_success(self): # type: () -> bool return self.status == "ok" @@ -567,6 +561,7 @@ class Transaction(Span): # tracestate data from other vendors, of the form `dogs=yes,cats=maybe` "_third_party_tracestate", "_measurements", + "_contexts", "_profile", "_baggage", "_active_thread_id", @@ -592,7 +587,9 @@ def __init__( "instead of Span(transaction=...)." ) name = kwargs.pop("transaction") + Span.__init__(self, **kwargs) + self.name = name self.source = source self.sample_rate = None # type: Optional[float] @@ -603,6 +600,7 @@ def __init__( self._sentry_tracestate = sentry_tracestate self._third_party_tracestate = third_party_tracestate self._measurements = {} # type: Dict[str, Any] + self._contexts = {} # type: Dict[str, Any] self._profile = None # type: Optional[sentry_sdk.profiler.Profile] self._baggage = baggage # for profiling, we want to know on which thread a transaction is started @@ -724,6 +722,10 @@ def set_measurement(self, name, value, unit=""): self._measurements[name] = {"value": value, "unit": unit} + def set_context(self, key, value): + # type: (str, Any) -> None + self._contexts[key] = value + def to_json(self): # type: () -> Dict[str, Any] rv = super(Transaction, self).to_json()