Skip to content

Commit c7977d3

Browse files
committed
Api for setting arbitrary contexts in spans.
1 parent 9d3047e commit c7977d3

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

sentry_sdk/tracing.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class Span(object):
9393
"timestamp",
9494
"_tags",
9595
"_data",
96+
"_contexts",
9697
"_span_recorder",
9798
"hub",
9899
"_context_manager_state",
@@ -140,6 +141,7 @@ def __init__(
140141
self.hub = hub
141142
self._tags = {} # type: Dict[str, str]
142143
self._data = {} # type: Dict[str, Any]
144+
self._contexts = {} # type: Dict[str, Any]
143145
self._containing_transaction = containing_transaction
144146
self.start_timestamp = datetime.utcnow()
145147
try:
@@ -468,6 +470,10 @@ def set_http_status(self, http_status):
468470
else:
469471
self.set_status("unknown_error")
470472

473+
def set_context(self, key, value):
474+
# type: (str, Any) -> None
475+
self._contexts[key] = value
476+
471477
def is_success(self):
472478
# type: () -> bool
473479
return self.status == "ok"
@@ -685,11 +691,15 @@ def finish(self, hub=None):
685691
# to be garbage collected
686692
self._span_recorder = None
687693

694+
contexts = {}
695+
contexts.update(self._contexts)
696+
contexts.update({"trace": self.get_trace_context()})
697+
688698
event = {
689699
"type": "transaction",
690700
"transaction": self.name,
691701
"transaction_info": {"source": self.source},
692-
"contexts": {"trace": self.get_trace_context()},
702+
"contexts": contexts,
693703
"tags": self._tags,
694704
"timestamp": self.timestamp,
695705
"start_timestamp": self.start_timestamp,

0 commit comments

Comments
 (0)