Skip to content

Commit 8a08fb3

Browse files
committed
lint
1 parent 048acc9 commit 8a08fb3

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

sentry_sdk/integrations/opentelemetry/potel_span_processor.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
from collections import deque
2-
from datetime import datetime
32

4-
from opentelemetry.trace import INVALID_SPAN, get_current_span, format_trace_id, format_span_id
3+
from opentelemetry.trace import format_trace_id, format_span_id
54
from opentelemetry.context import Context
65
from opentelemetry.sdk.trace import Span, ReadableSpan, SpanProcessor
76

87
from sentry_sdk import capture_event
9-
from sentry_sdk.integrations.opentelemetry.utils import is_sentry_span, convert_otel_timestamp
10-
from sentry_sdk.integrations.opentelemetry.consts import OTEL_SENTRY_CONTEXT, SPAN_ORIGIN
8+
from sentry_sdk.integrations.opentelemetry.utils import (
9+
is_sentry_span,
10+
convert_otel_timestamp,
11+
)
12+
from sentry_sdk.integrations.opentelemetry.consts import (
13+
OTEL_SENTRY_CONTEXT,
14+
SPAN_ORIGIN,
15+
)
1116
from sentry_sdk._types import TYPE_CHECKING
1217

1318
if TYPE_CHECKING:
14-
from typing import Optional, List, Any
19+
from typing import Optional, List, Any, Deque
1520
from sentry_sdk._types import Event
1621

1722

18-
class PotelSentrySpanProcessor(SpanProcessor): # type: ignore
23+
class PotelSentrySpanProcessor(SpanProcessor):
1924
"""
2025
Converts OTel spans into Sentry spans so they can be sent to the Sentry backend.
2126
"""
@@ -74,21 +79,22 @@ def _flush_root_span(self, span):
7479

7580
capture_event(transaction_event)
7681

77-
7882
def _collect_children(self, span):
7983
# type: (ReadableSpan) -> List[ReadableSpan]
8084
if not span.context:
8185
return []
8286

8387
children = []
84-
bfs_queue = deque()
88+
bfs_queue = deque() # type: Deque[int]
8589
bfs_queue.append(span.context.span_id)
8690

8791
while bfs_queue:
8892
parent_span_id = bfs_queue.popleft()
8993
node_children = self._children_spans.pop(parent_span_id, [])
9094
children.extend(node_children)
91-
bfs_queue.extend([child.context.span_id for child in node_children if child.context])
95+
bfs_queue.extend(
96+
[child.context.span_id for child in node_children if child.context]
97+
)
9298

9399
return children
94100

@@ -112,8 +118,8 @@ def _root_span_to_transaction_event(self, span):
112118
"trace_id": trace_id,
113119
"span_id": span_id,
114120
"origin": SPAN_ORIGIN,
115-
"op": span.name, # TODO
116-
"status": "ok", # TODO
121+
"op": span.name, # TODO
122+
"status": "ok", # TODO
117123
} # type: dict[str, Any]
118124

119125
if parent_span_id:
@@ -127,8 +133,8 @@ def _root_span_to_transaction_event(self, span):
127133

128134
event = {
129135
"type": "transaction",
130-
"transaction": span.name, # TODO
131-
"transaction_info": {"source": "custom"}, # TODO
136+
"transaction": span.name, # TODO
137+
"transaction_info": {"source": "custom"}, # TODO
132138
"contexts": contexts,
133139
"start_timestamp": convert_otel_timestamp(span.start_time),
134140
"timestamp": convert_otel_timestamp(span.end_time),
@@ -153,9 +159,9 @@ def _span_to_json(self, span):
153159
"trace_id": trace_id,
154160
"span_id": span_id,
155161
"origin": SPAN_ORIGIN,
156-
"op": span.name, # TODO
157-
"description": span.name, # TODO
158-
"status": "ok", # TODO
162+
"op": span.name, # TODO
163+
"description": span.name, # TODO
164+
"status": "ok", # TODO
159165
"start_timestamp": convert_otel_timestamp(span.start_time),
160166
"timestamp": convert_otel_timestamp(span.end_time),
161167
} # type: dict[str, Any]

sentry_sdk/integrations/opentelemetry/utils.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44
from opentelemetry.semconv.trace import SpanAttributes
55
from opentelemetry.sdk.trace import ReadableSpan
66

7-
from sentry_sdk import get_client, start_transaction
7+
from sentry_sdk import get_client
88
from sentry_sdk.utils import Dsn
99

1010
from sentry_sdk._types import TYPE_CHECKING
1111

1212
if TYPE_CHECKING:
1313
from typing import Optional
1414

15+
1516
def is_sentry_span(span):
1617
# type: (ReadableSpan) -> bool
1718
"""
1819
Break infinite loop:
1920
HTTP requests to Sentry are caught by OTel and send again to Sentry.
2021
"""
22+
if not span.attributes:
23+
return False
24+
2125
span_url = span.attributes.get(SpanAttributes.HTTP_URL, None)
2226
span_url = cast("Optional[str]", span_url)
2327

@@ -41,6 +45,7 @@ def is_sentry_span(span):
4145

4246
return False
4347

48+
4449
def convert_otel_timestamp(time):
4550
# type: (int) -> datetime
46-
return datetime.fromtimestamp(time / 1e9, timezone.utc)
51+
return datetime.fromtimestamp(time / 1e9, timezone.utc)

0 commit comments

Comments
 (0)