@@ -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
311315def _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+
353367class 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 (
0 commit comments