@@ -305,7 +305,11 @@ def __init__(
305
305
306
306
@property
307
307
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
309
313
310
314
311
315
def _is_valid_attribute_value (value : types .AttributeValue ) -> bool :
@@ -350,6 +354,16 @@ def _is_valid_attribute_value(value: types.AttributeValue) -> bool:
350
354
return True
351
355
352
356
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
+
353
367
class Span (trace_api .Span ):
354
368
"""See `opentelemetry.trace.Span`.
355
369
@@ -401,7 +415,7 @@ def __init__(
401
415
self .status = None
402
416
self ._lock = threading .Lock ()
403
417
404
- self . _filter_attribute_values (attributes )
418
+ _filter_attribute_values (attributes )
405
419
if not attributes :
406
420
self .attributes = self ._new_attributes ()
407
421
else :
@@ -412,7 +426,7 @@ def __init__(
412
426
self .events = self ._new_events ()
413
427
if events :
414
428
for event in events :
415
- self . _filter_attribute_values (event .attributes )
429
+ _filter_attribute_values (event .attributes )
416
430
self .events .append (event )
417
431
418
432
if links is None :
@@ -553,18 +567,6 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
553
567
with self ._lock :
554
568
self .attributes [key ] = value
555
569
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
-
568
570
def _add_event (self , event : EventBase ) -> None :
569
571
with self ._lock :
570
572
if not self .is_recording_events ():
@@ -582,7 +584,7 @@ def add_event(
582
584
attributes : types .Attributes = None ,
583
585
timestamp : Optional [int ] = None ,
584
586
) -> None :
585
- self . _filter_attribute_values (attributes )
587
+ _filter_attribute_values (attributes )
586
588
if not attributes :
587
589
attributes = self ._new_attributes ()
588
590
self ._add_event (
0 commit comments