Skip to content

Commit 74f1589

Browse files
committed
defaultdict
1 parent 2e2e5b9 commit 74f1589

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

sentry_sdk/integrations/opentelemetry/potel_span_processor.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from collections import deque
1+
from collections import deque, defaultdict
22

33
from opentelemetry.trace import format_trace_id, format_span_id
44
from opentelemetry.context import Context
@@ -17,7 +17,7 @@
1717
from sentry_sdk._types import TYPE_CHECKING
1818

1919
if TYPE_CHECKING:
20-
from typing import Optional, List, Any, Deque
20+
from typing import Optional, List, Any, Deque, DefaultDict
2121
from sentry_sdk._types import Event
2222

2323

@@ -35,7 +35,9 @@ def __new__(cls):
3535

3636
def __init__(self):
3737
# type: () -> None
38-
self._children_spans = {} # type: dict[int, List[ReadableSpan]]
38+
self._children_spans = defaultdict(
39+
list
40+
) # type: DefaultDict[int, List[ReadableSpan]]
3941

4042
def on_start(self, span, parent_context=None):
4143
# type: (Span, Optional[Context]) -> None
@@ -48,7 +50,7 @@ def on_end(self, span):
4850

4951
# TODO-neel-potel-remote only take parent if not remote
5052
if span.parent:
51-
self._children_spans.setdefault(span.parent.span_id, []).append(span)
53+
self._children_spans[span.parent.span_id].append(span)
5254
else:
5355
# if have a root span ending, we build a transaction and send it
5456
self._flush_root_span(span)
@@ -75,7 +77,7 @@ def _flush_root_span(self, span):
7577
span_json = self._span_to_json(child)
7678
if span_json:
7779
spans.append(span_json)
78-
transaction_event.setdefault("spans", []).extend(spans)
80+
transaction_event["spans"] = spans
7981
# TODO-neel-potel sort and cutoff max spans
8082

8183
capture_event(transaction_event)

tests/integrations/opentelemetry/test_span_processor.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
SentrySpanProcessor,
1111
link_trace_context_to_error_event,
1212
)
13+
from sentry_sdk.integrations.opentelemetry.utils import is_sentry_span
1314
from sentry_sdk.scope import Scope
1415
from sentry_sdk.tracing import Span, Transaction
1516
from sentry_sdk.tracing_utils import extract_sentrytrace_data
@@ -19,24 +20,24 @@ def test_is_sentry_span():
1920
otel_span = MagicMock()
2021

2122
span_processor = SentrySpanProcessor()
22-
assert not span_processor._is_sentry_span(otel_span)
23+
assert not is_sentry_span(otel_span)
2324

2425
client = MagicMock()
2526
client.options = {"instrumenter": "otel"}
2627
client.dsn = "https://[email protected]/123456"
2728
Scope.get_global_scope().set_client(client)
2829

29-
assert not span_processor._is_sentry_span(otel_span)
30+
assert not is_sentry_span(otel_span)
3031

3132
otel_span.attributes = {
3233
"http.url": "https://example.com",
3334
}
34-
assert not span_processor._is_sentry_span(otel_span)
35+
assert not is_sentry_span(otel_span)
3536

3637
otel_span.attributes = {
3738
"http.url": "https://o123456.ingest.sentry.io/api/123/envelope",
3839
}
39-
assert span_processor._is_sentry_span(otel_span)
40+
assert is_sentry_span(otel_span)
4041

4142

4243
def test_get_otel_context():

0 commit comments

Comments
 (0)