forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstdlib.py
More file actions
70 lines (53 loc) · 1.94 KB
/
stdlib.py
File metadata and controls
70 lines (53 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from sentry_sdk.hub import Hub
from sentry_sdk.integrations import Integration
try:
from httplib import HTTPConnection # type: ignore
except ImportError:
from http.client import HTTPConnection
class StdlibIntegration(Integration):
identifier = "stdlib"
@staticmethod
def setup_once():
# type: () -> None
install_httplib()
def install_httplib():
# type: () -> None
real_putrequest = HTTPConnection.putrequest
real_getresponse = HTTPConnection.getresponse
def putrequest(self, method, url, *args, **kwargs):
rv = real_putrequest(self, method, url, *args, **kwargs)
hub = Hub.current
if hub.get_integration(StdlibIntegration) is None:
return rv
self._sentrysdk_data_dict = data = {}
host = self.host
port = self.port
default_port = self.default_port
real_url = url
if not real_url.startswith(("http://", "https://")):
real_url = "%s://%s%s%s" % (
default_port == 443 and "https" or "http",
host,
port != default_port and ":%s" % port or "",
url,
)
for key, value in hub.iter_trace_propagation_headers():
self.putheader(key, value)
data["url"] = real_url
data["method"] = method
return rv
def getresponse(self, *args, **kwargs):
rv = real_getresponse(self, *args, **kwargs)
hub = Hub.current
if hub.get_integration(StdlibIntegration) is None:
return rv
data = getattr(self, "_sentrysdk_data_dict", None) or {}
if "status_code" not in data:
data["status_code"] = rv.status
data["reason"] = rv.reason
hub.add_breadcrumb(
type="http", category="httplib", data=data, hint={"httplib_response": rv}
)
return rv
HTTPConnection.putrequest = putrequest
HTTPConnection.getresponse = getresponse