Skip to content

Commit ee094c2

Browse files
authored
Filter attributes on lazy event add (#1014)
1 parent 0d5f15a commit ee094c2

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

+18-16
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,11 @@ def __init__(
305305

306306
@property
307307
def attributes(self) -> types.Attributes:
308-
return self._event_formatter()
308+
attributes = self._event_formatter()
309+
_filter_attribute_values(attributes)
310+
if not attributes:
311+
attributes = Span._new_attributes()
312+
return attributes
309313

310314

311315
def _is_valid_attribute_value(value: types.AttributeValue) -> bool:
@@ -350,6 +354,16 @@ def _is_valid_attribute_value(value: types.AttributeValue) -> bool:
350354
return True
351355

352356

357+
def _filter_attribute_values(attributes: types.Attributes):
358+
if attributes:
359+
for attr_key, attr_value in list(attributes.items()):
360+
if _is_valid_attribute_value(attr_value):
361+
if isinstance(attr_value, MutableSequence):
362+
attributes[attr_key] = tuple(attr_value)
363+
else:
364+
attributes.pop(attr_key)
365+
366+
353367
class Span(trace_api.Span):
354368
"""See `opentelemetry.trace.Span`.
355369
@@ -401,7 +415,7 @@ def __init__(
401415
self.status = None
402416
self._lock = threading.Lock()
403417

404-
self._filter_attribute_values(attributes)
418+
_filter_attribute_values(attributes)
405419
if not attributes:
406420
self.attributes = self._new_attributes()
407421
else:
@@ -412,7 +426,7 @@ def __init__(
412426
self.events = self._new_events()
413427
if events:
414428
for event in events:
415-
self._filter_attribute_values(event.attributes)
429+
_filter_attribute_values(event.attributes)
416430
self.events.append(event)
417431

418432
if links is None:
@@ -553,18 +567,6 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
553567
with self._lock:
554568
self.attributes[key] = value
555569

556-
@staticmethod
557-
def _filter_attribute_values(attributes: types.Attributes):
558-
if attributes:
559-
for attr_key, attr_value in list(attributes.items()):
560-
if _is_valid_attribute_value(attr_value):
561-
if isinstance(attr_value, MutableSequence):
562-
attributes[attr_key] = tuple(attr_value)
563-
else:
564-
attributes[attr_key] = attr_value
565-
else:
566-
attributes.pop(attr_key)
567-
568570
def _add_event(self, event: EventBase) -> None:
569571
with self._lock:
570572
if not self.is_recording_events():
@@ -582,7 +584,7 @@ def add_event(
582584
attributes: types.Attributes = None,
583585
timestamp: Optional[int] = None,
584586
) -> None:
585-
self._filter_attribute_values(attributes)
587+
_filter_attribute_values(attributes)
586588
if not attributes:
587589
attributes = self._new_attributes()
588590
self._add_event(

opentelemetry-sdk/tests/trace/test_trace.py

+14
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ def event_formatter():
610610

611611
def test_invalid_event_attributes(self):
612612
self.assertEqual(trace_api.get_current_span(), trace_api.INVALID_SPAN)
613+
now = time_ns()
613614

614615
with self.tracer.start_as_current_span("root") as root:
615616
root.add_event("event0", {"attr1": True, "attr2": ["hi", False]})
@@ -623,6 +624,19 @@ def test_invalid_event_attributes(self):
623624
self.assertEqual(root.events[2].attributes, {})
624625
self.assertEqual(root.events[3].attributes, {"attr2": (1, 2)})
625626

627+
def event_formatter():
628+
properties = {}
629+
properties["attr1"] = dict()
630+
properties["attr2"] = "hello"
631+
return properties
632+
633+
root.add_lazy_event("event4", event_formatter, now)
634+
635+
self.assertEqual(len(root.events), 5)
636+
self.assertEqual(root.events[4].name, "event4")
637+
self.assertEqual(root.events[4].attributes, {"attr2": "hello"})
638+
self.assertEqual(root.events[4].timestamp, now)
639+
626640
def test_links(self):
627641
other_context1 = trace_api.SpanContext(
628642
trace_id=trace.generate_trace_id(),

0 commit comments

Comments
 (0)