Skip to content

Commit d710109

Browse files
committed
fix: Restore breadcrumb for http calls, fix tests
1 parent 322a8f4 commit d710109

4 files changed

Lines changed: 24 additions & 11 deletions

File tree

sentry_sdk/integrations/django/__init__.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import
33

4-
import contextlib
54
import sys
65
import weakref
6+
import contextlib
77

88
from django import VERSION as DJANGO_VERSION # type: ignore
99
from django.db.models.query import QuerySet # type: ignore
@@ -332,11 +332,12 @@ def format_sql(sql, params):
332332
return sql, rv
333333

334334

335+
@contextlib.contextmanager
335336
def record_sql(sql, param_list, cursor=None):
336337
# type: (Any, Any, Any) -> Generator
337338
hub = Hub.current
338339
if hub.get_integration(DjangoIntegration) is None:
339-
yield
340+
yield None
340341
return
341342

342343
formatted_queries = []
@@ -374,7 +375,8 @@ def record_sql(sql, param_list, cursor=None):
374375
if real_sql:
375376
formatted_queries.append(real_sql)
376377

377-
return record_sql_queries(hub, formatted_queries)
378+
with record_sql_query(hub, formatted_queries):
379+
yield
378380

379381

380382
def install_sql_hook():
@@ -397,7 +399,7 @@ def execute(self, sql, params=None):
397399
return real_execute(self, sql, params)
398400

399401
def executemany(self, sql, param_list):
400-
with record_many_sql(sql, param_list, self.cursor):
402+
with record_sql(sql, param_list, self.cursor):
401403
return real_executemany(self, sql, param_list)
402404

403405
CursorWrapper.execute = execute

sentry_sdk/integrations/stdlib.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13
from sentry_sdk.hub import Hub
24
from sentry_sdk.integrations import Integration
35
from sentry_sdk.tracing import record_http_request

sentry_sdk/tracing.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,15 @@ def record_http_request(hub, url, method):
180180
yield data_dict
181181
finally:
182182
httplib_response = data_dict.pop("httplib_response", None)
183-
if "status_code" in data_dict:
184-
span.set_tag("http.status_code", data_dict["status_code"])
185-
for k, v in data_dict.items():
186-
span.set_data(k, v)
183+
if span is not None:
184+
if "status_code" in data_dict:
185+
span.set_tag("http.status_code", data_dict["status_code"])
186+
for k, v in data_dict.items():
187+
span.set_data(k, v)
188+
189+
hub.add_breadcrumb(
190+
type="http",
191+
category="httplib",
192+
data=data_dict,
193+
hint={"httplib_response": httplib_response},
194+
)

tests/test_tracing.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ def test_basic(sentry_init, capture_events, sample_rate):
1919
if sample_rate:
2020
event, = events
2121

22-
span1, span2, parent_span = event["spans"]
23-
assert not span1["tags"]["success"]
24-
assert span2["tags"]["success"]
22+
span1, span2 = event["spans"]
23+
parent_span = event
24+
assert span1["tags"]["error"]
25+
assert not span2["tags"]["error"]
2526
assert parent_span["transaction"] == "hi"
2627
else:
2728
assert not events

0 commit comments

Comments
 (0)