Skip to content

Commit 695dd06

Browse files
committed
move _set_span_attribuets into span
1 parent acd5401 commit 695dd06

File tree

2 files changed

+30
-27
lines changed

2 files changed

+30
-27
lines changed

sentry_sdk/tracing.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -599,9 +599,24 @@ def set_tag(self, key, value):
599599
# type: (str, Any) -> None
600600
self._tags[key] = value
601601

602-
def set_data(self, key, value):
603-
# type: (str, Any) -> None
604-
self._data[key] = value
602+
def set_data(self, key, value=None):
603+
# type: (Union[str, Dict[str, Any]], Any) -> None
604+
"""Set data on the span.
605+
606+
Can be called in two ways:
607+
- set_data(key, value) - sets a single key-value pair
608+
- set_data({"key": "value"}) - sets multiple key-value pairs from a dict
609+
"""
610+
if isinstance(key, dict):
611+
# Dictionary calling pattern: set_data({"key": "value"})
612+
for k, v in key.items():
613+
self._data[k] = v
614+
else:
615+
# Traditional calling pattern: set_data(key, value)
616+
if value is None:
617+
raise ValueError("Value must be provided when key is a string")
618+
619+
self._data[key] = value
605620

606621
def set_flag(self, flag, result):
607622
# type: (str, bool) -> None
@@ -1272,8 +1287,8 @@ def set_tag(self, key, value):
12721287
# type: (str, Any) -> None
12731288
pass
12741289

1275-
def set_data(self, key, value):
1276-
# type: (str, Any) -> None
1290+
def set_data(self, key, value=None):
1291+
# type: (Union[str, Dict[str, Any]], Any) -> None
12771292
pass
12781293

12791294
def set_status(self, value):
@@ -1343,18 +1358,20 @@ def trace(func):
13431358
pass
13441359

13451360

1346-
def trace(func=None, *, as_type="span", name=None, attributes=None):
1347-
# type: (Optional[Callable[P, R]], str, Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
1361+
def trace(func=None, *, template="span", op=None, name=None, attributes=None):
1362+
# type: (Optional[Callable[P, R]], str, Optional[str], Optional[str], Optional[dict[str, Any]]) -> Union[Callable[P, R], Callable[[Callable[P, R]], Callable[P, R]]]
13481363
"""
13491364
Decorator to start a child span.
13501365
13511366
:param func: The function to trace.
1352-
:param as_type: The type of span to create.
1367+
:param template: The type of span to create.
1368+
:param op: The operation of the span.
13531369
:param name: The name of the span. (defaults to the function name)
13541370
:param attributes: Additional attributes to set on the span.
13551371
"""
13561372
decorator = create_span_decorator(
1357-
template=as_type,
1373+
template=template,
1374+
op=op,
13581375
name=name,
13591376
attributes=attributes,
13601377
)

sentry_sdk/tracing_utils.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -864,20 +864,6 @@ def _get_span_op(template):
864864
return op
865865

866866

867-
def _set_span_attributes(span, attributes):
868-
# type: (Any, Optional[dict[str, Any]]) -> None
869-
"""
870-
Set the given attributes on the given span.
871-
872-
:param span: The span to set attributes on.
873-
:param attributes: The attributes to set on the span.
874-
"""
875-
attributes = attributes or {}
876-
877-
for key, value in attributes.items():
878-
span.set_data(key, value)
879-
880-
881867
def _set_input_attributes(span, template, args, kwargs):
882868
# type: (Span, str, tuple[Any, ...], dict[str, Any]) -> None
883869
"""
@@ -907,7 +893,7 @@ def _set_input_attributes(span, template, args, kwargs):
907893
SPANDATA.GEN_AI_TOOL_NAME: span.description,
908894
}
909895

910-
_set_span_attributes(span, attributes)
896+
span.set_data(attributes)
911897

912898

913899
def _set_output_attributes(span, template, result):
@@ -923,7 +909,7 @@ def _set_output_attributes(span, template, result):
923909

924910
# TODO: Implement :)
925911

926-
_set_span_attributes(span, attributes)
912+
span.set_data(attributes)
927913

928914

929915
def create_span_decorator(template, op=None, name=None, attributes=None):
@@ -969,7 +955,7 @@ async def async_wrapper(*args, **kwargs):
969955
result = await f(*args, **kwargs)
970956

971957
_set_output_attributes(span, template, result)
972-
_set_span_attributes(span, attributes)
958+
span.set_data(attributes)
973959

974960
return result
975961

@@ -1004,7 +990,7 @@ def sync_wrapper(*args, **kwargs):
1004990
result = f(*args, **kwargs)
1005991

1006992
_set_output_attributes(span, template, result)
1007-
_set_span_attributes(span, attributes)
993+
span.set_data(attributes)
1008994

1009995
return result
1010996

0 commit comments

Comments
 (0)