Skip to content

Commit 7bf4b7f

Browse files
committed
add attributes
1 parent d9f5e02 commit 7bf4b7f

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

sentry_sdk/_span_batcher.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ def add(self, span: "StreamedSpan") -> None:
7171
def _to_transport_format(item: "StreamedSpan") -> "Any":
7272
# TODO[span-first]
7373
res: "dict[str, Any]" = {}
74+
75+
if item._attributes:
76+
res["attributes"] = {
77+
k: serialize_attribute(v) for (k, v) in item.attributes.items()
78+
}
79+
7480
return res
7581

7682
def _flush(self) -> None:

sentry_sdk/scope.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ def _apply_scope_attributes_to_telemetry(
15411541
if isinstance(telemetry, dict):
15421542
attributes = telemetry["attributes"]
15431543
else:
1544-
attributes = telemetry.attributes
1544+
attributes = telemetry._attributes
15451545

15461546
for attribute, value in self._attributes.items():
15471547
if attribute not in attributes:

sentry_sdk/traces.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
import uuid
99
from typing import TYPE_CHECKING
1010

11+
from sentry_sdk.utils import format_attribute
12+
1113
if TYPE_CHECKING:
1214
from typing import Optional
15+
from sentry_sdk._types import Attributes, AttributeValue
1316

1417

1518
class StreamedSpan:
@@ -22,15 +25,40 @@ class StreamedSpan:
2225
span implementation lives in tracing.Span.
2326
"""
2427

25-
__slots__ = ("_trace_id",)
28+
__slots__ = (
29+
"name",
30+
"attributes",
31+
"_trace_id",
32+
)
2633

2734
def __init__(
2835
self,
2936
*,
37+
name: str,
38+
attributes: "Optional[Attributes]" = None,
3039
trace_id: "Optional[str]" = None,
3140
):
41+
self.name: str = name
42+
self._attributes: "Attributes" = attributes or {}
43+
3244
self._trace_id = trace_id
3345

46+
def get_attributes(self) -> "Attributes":
47+
return self._attributes
48+
49+
def set_attribute(self, key: str, value: "AttributeValue") -> None:
50+
self._attributes[key] = format_attribute(value)
51+
52+
def set_attributes(self, attributes: "Attributes") -> None:
53+
for key, value in attributes.items():
54+
self.set_attribute(key, value)
55+
56+
def remove_attribute(self, key: str) -> None:
57+
try:
58+
del self._attributes[key]
59+
except KeyError:
60+
pass
61+
3462
@property
3563
def trace_id(self) -> str:
3664
if not self._trace_id:

0 commit comments

Comments
 (0)