Skip to content

Commit 05d8a41

Browse files
committed
fix: Fix bug in requests instrumentation
1 parent 82d7058 commit 05d8a41

2 files changed

Lines changed: 26 additions & 14 deletions

File tree

sentry_sdk/integrations/stdlib.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,29 @@ def putrequest(self, method, url, *args, **kwargs):
6262

6363
def getresponse(self, *args, **kwargs):
6464
recorder = getattr(self, "_sentrysdk_recorder", None)
65+
66+
if recorder is None:
67+
return real_getresponse(self, *args, **kwargs)
68+
6569
data_dict = getattr(self, "_sentrysdk_data_dict", None)
6670

6771
try:
6872
rv = real_getresponse(self, *args, **kwargs)
6973

70-
if recorder is not None and data_dict is not None:
74+
if data_dict is not None:
7175
data_dict["httplib_response"] = rv
7276
data_dict["status_code"] = rv.status
7377
data_dict["reason"] = rv.reason
74-
finally:
75-
if recorder is not None:
76-
recorder.__exit__(*sys.exc_info())
78+
except TypeError:
79+
# python-requests provokes a typeerror to discover py3 vs py2 differences
80+
#
81+
# > TypeError("getresponse() got an unexpected keyword argument 'buffering'")
82+
raise
83+
except Exception:
84+
recorder.__exit__(*sys.exc_info())
85+
pass
86+
else:
87+
recorder.__exit__(None, None, None)
7788

7889
return rv
7990

sentry_sdk/tracing.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import re
2-
import sys
32
import uuid
43
import contextlib
54

@@ -211,19 +210,21 @@ def record_http_request(hub, url, method):
211210
with hub.span(op="http", description="%s %s" % (url, method)) as span:
212211
try:
213212
yield data_dict
214-
finally:
213+
except Exception:
214+
httplib_response = data_dict.pop("httplib_response", None)
215+
raise
216+
else:
215217
httplib_response = data_dict.pop("httplib_response", None)
216218

219+
hub.add_breadcrumb(
220+
type="http",
221+
category="httplib",
222+
data=data_dict,
223+
hint={"httplib_response": httplib_response},
224+
)
225+
finally:
217226
if span is not None:
218227
if "status_code" in data_dict:
219228
span.set_tag("http.status_code", data_dict["status_code"])
220229
for k, v in data_dict.items():
221230
span.set_data(k, v)
222-
223-
if sys.exc_info()[1] is None:
224-
hub.add_breadcrumb(
225-
type="http",
226-
category="httplib",
227-
data=data_dict,
228-
hint={"httplib_response": httplib_response},
229-
)

0 commit comments

Comments
 (0)