Skip to content

Commit 0d569d2

Browse files
authored
Add update_data to Span. (#4666)
This allows users to set multiple `span.data` attributes at once. In 3.x this then will become `set_attributes` (plural).
1 parent 84adbb7 commit 0d569d2

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

sentry_sdk/tracing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,10 @@ def set_data(self, key, value):
602602
# type: (str, Any) -> None
603603
self._data[key] = value
604604

605+
def update_data(self, data):
606+
# type: (Dict[str, Any]) -> None
607+
self._data.update(data)
608+
605609
def set_flag(self, flag, result):
606610
# type: (str, bool) -> None
607611
if len(self._flags) < self._flags_capacity:
@@ -1275,6 +1279,10 @@ def set_data(self, key, value):
12751279
# type: (str, Any) -> None
12761280
pass
12771281

1282+
def update_data(self, data):
1283+
# type: (Dict[str, Any]) -> None
1284+
pass
1285+
12781286
def set_status(self, value):
12791287
# type: (str) -> None
12801288
pass

tests/tracing/test_misc.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,3 +509,34 @@ def test_transaction_not_started_warning(sentry_init):
509509
"The transaction will not be sent to Sentry. To fix, start the transaction by"
510510
"passing it to sentry_sdk.start_transaction."
511511
)
512+
513+
514+
def test_span_set_data_update_data(sentry_init, capture_events):
515+
sentry_init(traces_sample_rate=1.0)
516+
517+
events = capture_events()
518+
519+
with sentry_sdk.start_transaction(name="test-transaction"):
520+
with start_span(op="test-span") as span:
521+
span.set_data("key0", "value0")
522+
span.set_data("key1", "value1")
523+
524+
span.update_data(
525+
{
526+
"key1": "updated-value1",
527+
"key2": "value2",
528+
"key3": "value3",
529+
}
530+
)
531+
532+
(event,) = events
533+
span = event["spans"][0]
534+
535+
assert span["data"] == {
536+
"key0": "value0",
537+
"key1": "updated-value1",
538+
"key2": "value2",
539+
"key3": "value3",
540+
"thread.id": mock.ANY,
541+
"thread.name": mock.ANY,
542+
}

0 commit comments

Comments
 (0)