@@ -67,28 +67,21 @@ def __iter__(self):
67
67
68
68
69
69
class _SpanRecorder (object ):
70
- __slots__ = ("maxlen" , "finished_spans" , "open_span_count" )
70
+ """Limits the number of spans recorded in a transaction."""
71
+
72
+ __slots__ = ("maxlen" , "spans" )
71
73
72
74
def __init__ (self , maxlen ):
73
75
# type: (int) -> None
74
76
self .maxlen = maxlen
75
- self .open_span_count = 0 # type: int
76
- self .finished_spans = [] # type: List[Span]
77
+ self .spans = [] # type: List[Span]
77
78
78
- def start_span (self , span ):
79
+ def add (self , span ):
79
80
# type: (Span) -> None
80
-
81
- # This is just so that we don't run out of memory while recording a lot
82
- # of spans. At some point we just stop and flush out the start of the
83
- # trace tree (i.e. the first n spans with the smallest
84
- # start_timestamp).
85
- self .open_span_count += 1
86
- if self .open_span_count > self .maxlen :
81
+ if len (self .spans ) > self .maxlen :
87
82
span ._span_recorder = None
88
-
89
- def finish_span (self , span ):
90
- # type: (Span) -> None
91
- self .finished_spans .append (span )
83
+ else :
84
+ self .spans .append (span )
92
85
93
86
94
87
class Span (object ):
@@ -157,7 +150,7 @@ def init_finished_spans(self, maxlen):
157
150
# type: (int) -> None
158
151
if self ._span_recorder is None :
159
152
self ._span_recorder = _SpanRecorder (maxlen )
160
- self ._span_recorder .start_span (self )
153
+ self ._span_recorder .add (self )
161
154
162
155
def __repr__ (self ):
163
156
# type: () -> str
@@ -330,8 +323,6 @@ def finish(self, hub=None):
330
323
if self ._span_recorder is None :
331
324
return None
332
325
333
- self ._span_recorder .finish_span (self )
334
-
335
326
if self .transaction is None :
336
327
# If this has no transaction set we assume there's a parent
337
328
# transaction for this span that would be flushed out eventually.
@@ -354,6 +345,12 @@ def finish(self, hub=None):
354
345
355
346
return None
356
347
348
+ finished_spans = [
349
+ span .to_json (client )
350
+ for span in self ._span_recorder .spans
351
+ if span is not self and span .timestamp is not None
352
+ ]
353
+
357
354
return hub .capture_event (
358
355
{
359
356
"type" : "transaction" ,
@@ -362,11 +359,7 @@ def finish(self, hub=None):
362
359
"tags" : self ._tags ,
363
360
"timestamp" : self .timestamp ,
364
361
"start_timestamp" : self .start_timestamp ,
365
- "spans" : [
366
- s .to_json (client )
367
- for s in self ._span_recorder .finished_spans
368
- if s is not self
369
- ],
362
+ "spans" : finished_spans ,
370
363
}
371
364
)
372
365
0 commit comments