Skip to content

Commit 7dba029

Browse files
committed
wip
1 parent 618d6ca commit 7dba029

File tree

4 files changed

+52
-29
lines changed

4 files changed

+52
-29
lines changed

sentry_sdk/integrations/opentelemetry/potel_span_exporter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from opentelemetry.trace import Span # type: ignore
1+
from opentelemetry.sdk.trace import ReadableSpan # type: ignore
22

33

44
class PotelSentrySpanExporter:
@@ -11,7 +11,7 @@ def __init__(self):
1111
pass
1212

1313
def export(self, span):
14-
# type: (Span) -> None
14+
# type: (ReadableSpan) -> None
1515
pass
1616

1717
def flush(self, timeout_millis):

sentry_sdk/integrations/opentelemetry/potel_span_processor.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
from opentelemetry.sdk.trace import SpanProcessor # type: ignore
1+
from opentelemetry.trace import INVALID_SPAN, get_current_span # type: ignore
22
from opentelemetry.context import Context # type: ignore
3-
from opentelemetry.trace import Span # type: ignore
3+
from opentelemetry.sdk.trace import Span, ReadableSpan, SpanProcessor # type: ignore
44

5+
from sentry_sdk.integrations.opentelemetry.utils import is_sentry_span
56
from sentry_sdk.integrations.opentelemetry.potel_span_exporter import (
67
PotelSentrySpanExporter,
78
)
9+
810
from sentry_sdk._types import TYPE_CHECKING
911

1012
if TYPE_CHECKING:
@@ -30,9 +32,19 @@ def __init__(self):
3032
def on_start(self, span, parent_context=None):
3133
# type: (Span, Optional[Context]) -> None
3234
pass
35+
# if is_sentry_span(span):
36+
# return
37+
38+
# parent_span = get_current_span(parent_context)
39+
40+
# # TODO-neel-potel check remote logic with propagation and incoming trace later
41+
# if parent_span != INVALID_SPAN:
42+
# # TODO-neel once we add our apis, we might need to store references on the span
43+
# # directly, see if we need to do this like JS
44+
# pass
3345

3446
def on_end(self, span):
35-
# type: (Span) -> None
47+
# type: (ReadableSpan) -> None
3648
self._exporter.export(span)
3749

3850
# TODO-neel-potel not sure we need a clear like JS

sentry_sdk/integrations/opentelemetry/span_processor.py

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
SENTRY_BAGGAGE_KEY,
2323
SENTRY_TRACE_KEY,
2424
)
25+
from sentry_sdk.integrations.opentelemetry.utils import is_sentry_span
2526
from sentry_sdk.scope import add_global_event_processor
2627
from sentry_sdk.tracing import Transaction, Span as SentrySpan
27-
from sentry_sdk.utils import Dsn
2828
from sentry_sdk._types import TYPE_CHECKING
2929

3030
from urllib3.util import parse_url as urlparse
@@ -125,7 +125,7 @@ def on_start(self, otel_span, parent_context=None):
125125
if not otel_span.get_span_context().is_valid:
126126
return
127127

128-
if self._is_sentry_span(otel_span):
128+
if is_sentry_span(otel_span):
129129
return
130130

131131
trace_data = self._get_trace_data(otel_span, parent_context)
@@ -208,28 +208,6 @@ def on_end(self, otel_span):
208208
self.open_spans.setdefault(span_start_in_minutes, set()).discard(span_id)
209209
self._prune_old_spans()
210210

211-
def _is_sentry_span(self, otel_span):
212-
# type: (OTelSpan) -> bool
213-
"""
214-
Break infinite loop:
215-
HTTP requests to Sentry are caught by OTel and send again to Sentry.
216-
"""
217-
otel_span_url = otel_span.attributes.get(SpanAttributes.HTTP_URL, None)
218-
219-
dsn_url = None
220-
client = get_client()
221-
222-
if client.dsn:
223-
try:
224-
dsn_url = Dsn(client.dsn).netloc
225-
except Exception:
226-
pass
227-
228-
if otel_span_url and dsn_url in otel_span_url:
229-
return True
230-
231-
return False
232-
233211
def _get_otel_context(self, otel_span):
234212
# type: (OTelSpan) -> Dict[str, Any]
235213
"""
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from opentelemetry.semconv.trace import SpanAttributes # type: ignore
2+
from opentelemetry.trace import Span # type: ignore
3+
4+
from sentry_sdk import get_client, start_transaction
5+
from sentry_sdk.utils import Dsn
6+
7+
def is_sentry_span(span):
8+
# type: (Span) -> bool
9+
"""
10+
Break infinite loop:
11+
HTTP requests to Sentry are caught by OTel and send again to Sentry.
12+
"""
13+
span_url = span.attributes.get(SpanAttributes.HTTP_URL, None)
14+
15+
if not span_url:
16+
return False
17+
18+
dsn_url = None
19+
client = get_client()
20+
21+
if client.dsn:
22+
try:
23+
dsn_url = Dsn(client.dsn).netloc
24+
except Exception:
25+
pass
26+
27+
if not dsn_url:
28+
return False
29+
30+
if dsn_url in span_url:
31+
return True
32+
33+
return False

0 commit comments

Comments
 (0)