Skip to content

Commit 24aa1ab

Browse files
authored
Merge branch 'potel-base' into potel-base-run-all-tests
2 parents 1096b2c + 7c2d770 commit 24aa1ab

File tree

7 files changed

+186
-197
lines changed

7 files changed

+186
-197
lines changed

sentry_sdk/integrations/opentelemetry/utils.py

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -161,36 +161,43 @@ def span_data_for_http_method(span):
161161
# type: (ReadableSpan) -> OtelExtractedSpanData
162162
span_attributes = span.attributes or {}
163163

164-
op = "http"
164+
op = span_attributes.get(SentrySpanAttribute.OP)
165+
if op is None:
166+
op = "http"
165167

166-
if span.kind == SpanKind.SERVER:
167-
op += ".server"
168-
elif span.kind == SpanKind.CLIENT:
169-
op += ".client"
168+
if span.kind == SpanKind.SERVER:
169+
op += ".server"
170+
elif span.kind == SpanKind.CLIENT:
171+
op += ".client"
170172

171173
http_method = span_attributes.get(SpanAttributes.HTTP_METHOD)
172174
route = span_attributes.get(SpanAttributes.HTTP_ROUTE)
173175
target = span_attributes.get(SpanAttributes.HTTP_TARGET)
174176
peer_name = span_attributes.get(SpanAttributes.NET_PEER_NAME)
175177

176-
description = f"{http_method}"
177-
178-
if route:
179-
description = f"{http_method} {route}"
180-
elif target:
181-
description = f"{http_method} {target}"
182-
elif peer_name:
183-
description = f"{http_method} {peer_name}"
184-
else:
185-
url = span_attributes.get(SpanAttributes.HTTP_URL)
186-
url = cast("Optional[str]", url)
187-
188-
if url:
189-
parsed_url = urlparse(url)
190-
url = "{}://{}{}".format(
191-
parsed_url.scheme, parsed_url.netloc, parsed_url.path
192-
)
193-
description = f"{http_method} {url}"
178+
# TODO-neel-potel remove description completely
179+
description = span_attributes.get(
180+
SentrySpanAttribute.DESCRIPTION
181+
) or span_attributes.get(SentrySpanAttribute.NAME)
182+
if description is None:
183+
description = f"{http_method}"
184+
185+
if route:
186+
description = f"{http_method} {route}"
187+
elif target:
188+
description = f"{http_method} {target}"
189+
elif peer_name:
190+
description = f"{http_method} {peer_name}"
191+
else:
192+
url = span_attributes.get(SpanAttributes.HTTP_URL)
193+
url = cast("Optional[str]", url)
194+
195+
if url:
196+
parsed_url = urlparse(url)
197+
url = "{}://{}{}".format(
198+
parsed_url.scheme, parsed_url.netloc, parsed_url.path
199+
)
200+
description = f"{http_method} {url}"
194201

195202
status, http_status = extract_span_status(span)
196203

sentry_sdk/integrations/rust_tracing.py

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,9 @@
3737
import sentry_sdk
3838
from sentry_sdk.integrations import Integration
3939
from sentry_sdk.scope import should_send_default_pii
40-
from sentry_sdk.tracing import Span as SentrySpan
40+
from sentry_sdk.tracing import POTelSpan as SentrySpan
4141
from sentry_sdk.utils import SENSITIVE_DATA_SUBSTITUTE
4242

43-
TraceState = Optional[Tuple[Optional[SentrySpan], SentrySpan]]
44-
4543

4644
class RustTracingLevel(Enum):
4745
Trace: str = "TRACE"
@@ -171,7 +169,7 @@ def _include_tracing_fields(self) -> bool:
171169
else self.include_tracing_fields
172170
)
173171

174-
def on_event(self, event: str, _span_state: TraceState) -> None:
172+
def on_event(self, event: str, _span_state: Optional[SentrySpan]) -> None:
175173
deserialized_event = json.loads(event)
176174
metadata = deserialized_event.get("metadata", {})
177175

@@ -185,7 +183,7 @@ def on_event(self, event: str, _span_state: TraceState) -> None:
185183
elif event_type == EventTypeMapping.Event:
186184
process_event(deserialized_event)
187185

188-
def on_new_span(self, attrs: str, span_id: str) -> TraceState:
186+
def on_new_span(self, attrs: str, span_id: str) -> Optional[SentrySpan]:
189187
attrs = json.loads(attrs)
190188
metadata = attrs.get("metadata", {})
191189

@@ -205,48 +203,35 @@ def on_new_span(self, attrs: str, span_id: str) -> TraceState:
205203
else:
206204
sentry_span_name = "<unknown>"
207205

208-
kwargs = {
209-
"op": "function",
210-
"name": sentry_span_name,
211-
"origin": self.origin,
212-
}
213-
214-
scope = sentry_sdk.get_current_scope()
215-
parent_sentry_span = scope.span
216-
if parent_sentry_span:
217-
sentry_span = parent_sentry_span.start_child(**kwargs)
218-
else:
219-
sentry_span = scope.start_span(**kwargs)
206+
span = sentry_sdk.start_span(
207+
op="function",
208+
name=sentry_span_name,
209+
origin=self.origin,
210+
only_if_parent=True,
211+
)
212+
span.__enter__()
220213

221214
fields = metadata.get("fields", [])
222215
for field in fields:
223216
if self._include_tracing_fields():
224-
sentry_span.set_data(field, attrs.get(field))
225-
else:
226-
sentry_span.set_data(field, SENSITIVE_DATA_SUBSTITUTE)
227-
228-
scope.span = sentry_span
229-
return (parent_sentry_span, sentry_span)
230-
231-
def on_close(self, span_id: str, span_state: TraceState) -> None:
232-
if span_state is None:
233-
return
234-
235-
parent_sentry_span, sentry_span = span_state
236-
sentry_span.finish()
237-
sentry_sdk.get_current_scope().span = parent_sentry_span
238-
239-
def on_record(self, span_id: str, values: str, span_state: TraceState) -> None:
240-
if span_state is None:
241-
return
242-
_parent_sentry_span, sentry_span = span_state
243-
244-
deserialized_values = json.loads(values)
245-
for key, value in deserialized_values.items():
246-
if self._include_tracing_fields():
247-
sentry_span.set_data(key, value)
217+
span.set_data(field, attrs.get(field))
248218
else:
249-
sentry_span.set_data(key, SENSITIVE_DATA_SUBSTITUTE)
219+
span.set_data(field, SENSITIVE_DATA_SUBSTITUTE)
220+
221+
return span
222+
223+
def on_close(self, span_id: str, span: Optional[SentrySpan]) -> None:
224+
if span is not None:
225+
span.__exit__(None, None, None)
226+
227+
def on_record(self, span_id: str, values: str, span: Optional[SentrySpan]) -> None:
228+
if span is not None:
229+
deserialized_values = json.loads(values)
230+
for key, value in deserialized_values.items():
231+
if self._include_tracing_fields():
232+
span.set_data(key, value)
233+
else:
234+
span.set_data(key, SENSITIVE_DATA_SUBSTITUTE)
250235

251236

252237
class RustTracingIntegration(Integration):

sentry_sdk/integrations/stdlib.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ def putrequest(self, method, url, *args, **kwargs):
9696
origin="auto.http.stdlib.httplib",
9797
only_if_parent=True,
9898
)
99+
span.__enter__()
99100

100101
data = {
101102
SPANDATA.HTTP_METHOD: method,
@@ -152,7 +153,7 @@ def getresponse(self, *args, **kwargs):
152153
span.set_http_status(int(rv.status))
153154
span.set_data("reason", rv.reason)
154155
finally:
155-
span.finish()
156+
span.__exit__(None, None, None)
156157

157158
return rv
158159

sentry_sdk/tracing.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import uuid
23
import random
34
import time
@@ -1631,8 +1632,12 @@ def finish(self, end_timestamp=None):
16311632

16321633
def to_json(self):
16331634
# type: () -> dict[str, Any]
1634-
# TODO-neel-potel for sampling context
1635-
pass
1635+
"""
1636+
Only meant for testing. Not used internally anymore.
1637+
"""
1638+
if not isinstance(self._otel_span, ReadableSpan):
1639+
return {}
1640+
return json.loads(self._otel_span.to_json())
16361641

16371642
def get_trace_context(self):
16381643
# type: () -> dict[str, Any]

tests/integrations/httpx/test_httpx.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ def test_outgoing_trace_headers(sentry_init, httpx_client, capture_envelopes):
101101
(httpx.Client(), httpx.AsyncClient()),
102102
)
103103
def test_outgoing_trace_headers_append_to_baggage(
104-
sentry_init, httpx_client, capture_envelopes, SortedBaggage, # noqa: N803
104+
sentry_init,
105+
httpx_client,
106+
capture_envelopes,
107+
SortedBaggage, # noqa: N803
105108
):
106109
sentry_init(
107110
traces_sample_rate=1.0,
@@ -137,9 +140,8 @@ def test_outgoing_trace_headers_append_to_baggage(
137140
parent_span_id=request_span["span_id"],
138141
sampled=1,
139142
)
140-
assert (
141-
response.request.headers["baggage"]
142-
== SortedBaggage(f"custom=data,sentry-trace_id={trace_id},sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0,sentry-sampled=true")
143+
assert response.request.headers["baggage"] == SortedBaggage(
144+
f"custom=data,sentry-trace_id={trace_id},sentry-environment=production,sentry-release=d08ebdb9309e1b004c6f52202de58a09c2268e42,sentry-transaction=/interactions/other-dogs/new-dog,sentry-sample_rate=1.0,sentry-sampled=true"
143145
)
144146

145147

0 commit comments

Comments
 (0)