@@ -252,11 +252,6 @@ class Span(trace_api.Span):
252
252
this `Span`.
253
253
"""
254
254
255
- # Initialize these lazily assuming most spans won't have them.
256
- _empty_attributes = BoundedDict (MAX_NUM_ATTRIBUTES )
257
- _empty_events = BoundedList (MAX_NUM_EVENTS )
258
- _empty_links = BoundedList (MAX_NUM_LINKS )
259
-
260
255
def __init__ (
261
256
self ,
262
257
name : str ,
@@ -289,22 +284,20 @@ def __init__(
289
284
290
285
self ._filter_attribute_values (attributes )
291
286
if not attributes :
292
- self .attributes = Span . _empty_attributes
287
+ self .attributes = self . _new_attributes ()
293
288
else :
294
289
self .attributes = BoundedDict .from_map (
295
290
MAX_NUM_ATTRIBUTES , attributes
296
291
)
297
292
298
- if events is None :
299
- self .events = Span ._empty_events
300
- else :
301
- self .events = BoundedList (MAX_NUM_EVENTS )
293
+ self .events = self ._new_events ()
294
+ if events :
302
295
for event in events :
303
296
self ._filter_attribute_values (event .attributes )
304
297
self .events .append (event )
305
298
306
299
if links is None :
307
- self .links = Span . _empty_links
300
+ self .links = self . _new_links ()
308
301
else :
309
302
self .links = BoundedList .from_seq (MAX_NUM_LINKS , links )
310
303
@@ -325,6 +318,18 @@ def __repr__(self):
325
318
type (self ).__name__ , self .name , self .context
326
319
)
327
320
321
+ @staticmethod
322
+ def _new_attributes ():
323
+ return BoundedDict (MAX_NUM_ATTRIBUTES )
324
+
325
+ @staticmethod
326
+ def _new_events ():
327
+ return BoundedList (MAX_NUM_EVENTS )
328
+
329
+ @staticmethod
330
+ def _new_links ():
331
+ return BoundedList (MAX_NUM_LINKS )
332
+
328
333
@staticmethod
329
334
def _format_context (context ):
330
335
x_ctx = OrderedDict ()
@@ -407,9 +412,6 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
407
412
if not self .is_recording_events ():
408
413
return
409
414
has_ended = self .end_time is not None
410
- if not has_ended :
411
- if self .attributes is Span ._empty_attributes :
412
- self .attributes = BoundedDict (MAX_NUM_ATTRIBUTES )
413
415
if has_ended :
414
416
logger .warning ("Setting attribute on ended span." )
415
417
return
@@ -442,9 +444,7 @@ def _add_event(self, event: EventBase) -> None:
442
444
if not self .is_recording_events ():
443
445
return
444
446
has_ended = self .end_time is not None
445
- if not has_ended :
446
- if self .events is Span ._empty_events :
447
- self .events = BoundedList (MAX_NUM_EVENTS )
447
+
448
448
if has_ended :
449
449
logger .warning ("Calling add_event() on an ended span." )
450
450
return
@@ -458,7 +458,7 @@ def add_event(
458
458
) -> None :
459
459
self ._filter_attribute_values (attributes )
460
460
if not attributes :
461
- attributes = Span . _empty_attributes
461
+ attributes = self . _new_attributes ()
462
462
self ._add_event (
463
463
Event (
464
464
name = name ,
0 commit comments