Skip to content

Commit d0f766b

Browse files
committed
tests and doc
1 parent 0d350a2 commit d0f766b

File tree

2 files changed

+82
-4
lines changed

2 files changed

+82
-4
lines changed

sentry_sdk/api.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,43 @@ def set_transaction_name(name, source=None):
479479
def update_current_span(op=None, name=None, attributes=None):
480480
# type: (Optional[str], Optional[str], Optional[dict[str, Union[str, int, float, bool]]]) -> None
481481
"""
482-
Update the current span with the given parameters.
483-
:param op: The operation of the span.
484-
:param name: The name of the span.
485-
:param attributes: The attributes of the span.
482+
Update the current active span with the provided parameters.
483+
484+
This function allows you to modify properties of the currently active span.
485+
If no span is currently active, this function will do nothing.
486+
487+
:param op: The operation name for the span. This is a high-level description
488+
of what the span represents (e.g., "http.client", "db.query").
489+
You can use predefined constants from :py:class:`sentry_sdk.consts.OP`
490+
or provide your own string. If not provided, the span's operation will
491+
remain unchanged.
492+
:type op: str or None
493+
494+
:param name: The human-readable name/description for the span. This provides
495+
more specific details about what the span represents (e.g., "GET /api/users",
496+
"SELECT * FROM users"). If not provided, the span's name will remain unchanged.
497+
:type name: str or None
498+
499+
:param attributes: A dictionary of key-value pairs to add as attributes to the span.
500+
Attribute values must be strings, integers, floats, or booleans. These
501+
attributes will be merged with any existing span data. If not provided,
502+
no attributes will be added.
503+
:type attributes: dict[str, Union[str, int, float, bool]] or None
504+
505+
:returns: None
506+
507+
.. versionadded:: 2.0.0
508+
509+
Example::
510+
511+
import sentry_sdk
512+
from sentry_sdk.consts import OP
513+
514+
sentry_sdk.update_current_span(
515+
op=OP.FUNCTION,
516+
name="process_user_data",
517+
attributes={"user_id": 123, "batch_size": 50}
518+
)
486519
"""
487520
current_span = get_current_span()
488521

tests/tracing/test_misc.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,3 +540,48 @@ def test_span_set_data_update_data(sentry_init, capture_events):
540540
"thread.id": mock.ANY,
541541
"thread.name": mock.ANY,
542542
}
543+
544+
545+
def test_update_current_span(sentry_init, capture_events):
546+
sentry_init(traces_sample_rate=1.0)
547+
548+
events = capture_events()
549+
550+
with sentry_sdk.start_transaction(name="test-transaction"):
551+
with start_span(op="test-span-op", name="test-span-name"):
552+
sentry_sdk.update_current_span(
553+
op="updated-span-op",
554+
name="updated-span-name",
555+
attributes={
556+
"key0": "value0",
557+
"key1": "value1",
558+
},
559+
)
560+
561+
sentry_sdk.update_current_span(
562+
op="updated-span-op-2",
563+
)
564+
565+
sentry_sdk.update_current_span(
566+
name="updated-span-name-3",
567+
)
568+
569+
sentry_sdk.update_current_span(
570+
attributes={
571+
"key1": "updated-value-4",
572+
"key2": "value2",
573+
},
574+
)
575+
576+
(event,) = events
577+
span = event["spans"][0]
578+
579+
assert span["op"] == "updated-span-op-2"
580+
assert span["description"] == "updated-span-name-3"
581+
assert span["data"] == {
582+
"key0": "value0",
583+
"key1": "updated-value-4",
584+
"key2": "value2",
585+
"thread.id": mock.ANY,
586+
"thread.name": mock.ANY,
587+
}

0 commit comments

Comments
 (0)