Skip to content

Commit 27401f0

Browse files
committed
ref: Move stdlib HTTP breadcrumbs to integration, remove maybe_create_breadcrumbs_from_span
Store method/URL on HTTPConnection in putrequest() so getresponse() can create breadcrumbs directly without reading span internals. Remove the now-unused maybe_create_breadcrumbs_from_span function and its call site in Span.finish().
1 parent 2e4c440 commit 27401f0

3 files changed

Lines changed: 21 additions & 26 deletions

File tree

sentry_sdk/integrations/stdlib.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from sentry_sdk.tracing import Span
1414
from sentry_sdk.tracing_utils import (
1515
EnvironHeaders,
16+
add_http_breadcrumb,
1617
add_http_request_source,
1718
has_span_streaming_enabled,
1819
should_propagate_trace,
@@ -174,6 +175,10 @@ def putrequest(
174175
self.putheader(key, value)
175176

176177
self._sentrysdk_span = span # type: ignore[attr-defined]
178+
self._sentrysdk_method = method # type: ignore[attr-defined]
179+
self._sentrysdk_url = parsed_url.url if parsed_url else None # type: ignore[attr-defined]
180+
self._sentrysdk_query = parsed_url.query if parsed_url else None # type: ignore[attr-defined]
181+
self._sentrysdk_fragment = parsed_url.fragment if parsed_url else None # type: ignore[attr-defined]
177182

178183
return rv
179184

@@ -189,14 +194,27 @@ def getresponse(self: "HTTPConnection", *args: "Any", **kwargs: "Any") -> "Any":
189194
_complete_span(span)
190195
raise
191196

197+
status_code = int(rv.status)
192198
if isinstance(span, StreamedSpan):
193-
status_code = int(rv.status)
194199
span.status = "error" if status_code >= 400 else "ok"
195200
span.set_attribute("http.response.status_code", status_code)
196201
else:
197-
span.set_http_status(int(rv.status))
202+
span.set_http_status(status_code)
198203
span.set_data("reason", rv.reason)
199204

205+
with capture_internal_exceptions():
206+
add_http_breadcrumb(
207+
status_code,
208+
{
209+
SPANDATA.HTTP_METHOD: getattr(self, "_sentrysdk_method", None),
210+
"url": getattr(self, "_sentrysdk_url", None),
211+
SPANDATA.HTTP_QUERY: getattr(self, "_sentrysdk_query", None),
212+
SPANDATA.HTTP_FRAGMENT: getattr(self, "_sentrysdk_fragment", None),
213+
SPANDATA.HTTP_STATUS_CODE: status_code,
214+
"reason": rv.reason,
215+
},
216+
)
217+
200218
# getresponse doesn't include actually reading the response body. This
201219
# is done in read(). So if the metadata/headers suggest there's a body to
202220
# read, don't finish the span just yet, but save it for ending it later.

sentry_sdk/tracing.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,6 @@ def finish(
700700
if has_ai_op or is_ai_span_op:
701701
self.set_data("gen_ai.conversation.id", conversation_id)
702702

703-
maybe_create_breadcrumbs_from_span(scope, self)
704-
705703
return None
706704

707705
def to_json(self) -> "Dict[str, Any]":
@@ -1495,5 +1493,4 @@ def calculate_interest_rate(amount, rate, years):
14951493
extract_sentrytrace_data,
14961494
has_span_streaming_enabled,
14971495
has_tracing_enabled,
1498-
maybe_create_breadcrumbs_from_span,
14991496
)

sentry_sdk/tracing_utils.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -219,32 +219,12 @@ def add_http_breadcrumb(status_code, data):
219219
elif 400 <= status_code <= 499:
220220
level = "warning"
221221

222-
kwargs = {"type": "http", "category": "httplib", "data": data}
222+
kwargs: "dict[str, Any]" = {"type": "http", "category": "httplib", "data": data}
223223
if level:
224224
kwargs["level"] = level
225225
sentry_sdk.add_breadcrumb(**kwargs)
226226

227227

228-
def maybe_create_breadcrumbs_from_span(
229-
scope: "sentry_sdk.Scope", span: "sentry_sdk.tracing.Span"
230-
) -> None:
231-
if span.op == OP.HTTP_CLIENT:
232-
level = None
233-
status_code = span._data.get(SPANDATA.HTTP_STATUS_CODE)
234-
if status_code:
235-
if 500 <= status_code <= 599:
236-
level = "error"
237-
elif 400 <= status_code <= 499:
238-
level = "warning"
239-
240-
if level:
241-
scope.add_breadcrumb(
242-
type="http", category="httplib", data=span._data, level=level
243-
)
244-
else:
245-
scope.add_breadcrumb(type="http", category="httplib", data=span._data)
246-
247-
248228
def _get_frame_module_abs_path(frame: "FrameType") -> "Optional[str]":
249229
try:
250230
return frame.f_code.co_filename

0 commit comments

Comments
 (0)