1
- from collections import deque
1
+ from collections import deque , defaultdict
2
2
3
3
from opentelemetry .trace import format_trace_id , format_span_id
4
4
from opentelemetry .context import Context
17
17
from sentry_sdk ._types import TYPE_CHECKING
18
18
19
19
if TYPE_CHECKING :
20
- from typing import Optional , List , Any , Deque
20
+ from typing import Optional , List , Any , Deque , DefaultDict
21
21
from sentry_sdk ._types import Event
22
22
23
23
@@ -35,7 +35,9 @@ def __new__(cls):
35
35
36
36
def __init__ (self ):
37
37
# type: () -> None
38
- self ._children_spans = {} # type: dict[int, List[ReadableSpan]]
38
+ self ._children_spans = defaultdict (
39
+ list
40
+ ) # type: DefaultDict[int, List[ReadableSpan]]
39
41
40
42
def on_start (self , span , parent_context = None ):
41
43
# type: (Span, Optional[Context]) -> None
@@ -48,7 +50,7 @@ def on_end(self, span):
48
50
49
51
# TODO-neel-potel-remote only take parent if not remote
50
52
if span .parent :
51
- self ._children_spans . setdefault ( span .parent .span_id , []) .append (span )
53
+ self ._children_spans [ span .parent .span_id ] .append (span )
52
54
else :
53
55
# if have a root span ending, we build a transaction and send it
54
56
self ._flush_root_span (span )
@@ -75,7 +77,7 @@ def _flush_root_span(self, span):
75
77
span_json = self ._span_to_json (child )
76
78
if span_json :
77
79
spans .append (span_json )
78
- transaction_event . setdefault ( "spans" , []). extend ( spans )
80
+ transaction_event [ "spans" ] = spans
79
81
# TODO-neel-potel sort and cutoff max spans
80
82
81
83
capture_event (transaction_event )
0 commit comments