Skip to content

Commit d1bf63f

Browse files
mitsuhikountitaker
authored andcommitted
feat: Added breadcrumbs for httplib (#52)
* fix: Removed a stray print * feat: Added breadcrumbs for httplib * fix: Remove requests integration in favor of httplib
1 parent 18a0c91 commit d1bf63f

File tree

5 files changed

+83
-50
lines changed

5 files changed

+83
-50
lines changed

sentry_sdk/integrations/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ def get_default_integrations():
1919
- `AtexitIntegration`
2020
"""
2121
from .logging import LoggingIntegration
22+
from .stdlib import StdlibIntegration
2223
from .excepthook import ExcepthookIntegration
2324
from .dedupe import DedupeIntegration
2425
from .atexit import AtexitIntegration
2526

2627
yield LoggingIntegration()
28+
yield StdlibIntegration()
2729
yield ExcepthookIntegration()
2830
yield DedupeIntegration()
2931
yield AtexitIntegration()

sentry_sdk/integrations/requests.py

Lines changed: 0 additions & 47 deletions
This file was deleted.

sentry_sdk/integrations/stdlib.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from sentry_sdk import add_breadcrumb
2+
from . import Integration
3+
4+
5+
class StdlibIntegration(Integration):
6+
identifier = "stdlib"
7+
8+
def __init__(self):
9+
try:
10+
from httplib import HTTPConnection
11+
except ImportError:
12+
from http.client import HTTPConnection
13+
self.httplib_connection_cls = HTTPConnection
14+
15+
def install_httplib(self):
16+
real_putrequest = self.httplib_connection_cls.putrequest
17+
real_getresponse = self.httplib_connection_cls.getresponse
18+
19+
def putrequest(self, method, url, *args, **kwargs):
20+
self._sentrysdk_data_dict = data = {}
21+
host = self.host
22+
port = self.port
23+
default_port = self.default_port
24+
25+
real_url = url
26+
if not real_url.startswith(("http://", "https://")):
27+
real_url = "%s://%s%s%s" % (
28+
default_port == 443 and "https" or "http",
29+
host,
30+
port != default_port and ":%s" % port or "",
31+
url,
32+
)
33+
data["url"] = real_url
34+
data["method"] = method
35+
return real_putrequest(self, method, url, *args, **kwargs)
36+
37+
def getresponse(self, *args, **kwargs):
38+
rv = real_getresponse(self, *args, **kwargs)
39+
data = getattr(self, "_sentrysdk_data_dict", None) or {}
40+
if "status_code" not in data:
41+
data["status_code"] = rv.status
42+
data["reason"] = rv.reason
43+
add_breadcrumb(type="http", category="httplib", data=data)
44+
return rv
45+
46+
self.httplib_connection_cls.putrequest = putrequest
47+
self.httplib_connection_cls.getresponse = getresponse
48+
49+
def install(self):
50+
self.install_httplib()

tests/integrations/requests/test_requests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
requests = pytest.importorskip("requests")
44

55
from sentry_sdk import capture_message
6-
from sentry_sdk.integrations.requests import RequestsIntegration
6+
from sentry_sdk.integrations.stdlib import StdlibIntegration
77

88

99
def test_crumb_capture(sentry_init, capture_events):
10-
sentry_init(integrations=[RequestsIntegration()])
10+
sentry_init(integrations=[StdlibIntegration()])
1111
events = capture_events()
1212

1313
response = requests.get("https://httpbin.org/status/418")
@@ -17,7 +17,7 @@ def test_crumb_capture(sentry_init, capture_events):
1717
event, = events
1818
crumb, = event["breadcrumbs"]
1919
assert crumb["type"] == "http"
20-
assert crumb["category"] == "requests"
20+
assert crumb["category"] == "httplib"
2121
assert crumb["data"] == {
2222
"url": "https://httpbin.org/status/418",
2323
"method": "GET",
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
try:
2+
from urllib.request import urlopen
3+
except ImportError:
4+
from urllib import urlopen
5+
6+
from sentry_sdk import capture_message
7+
from sentry_sdk.integrations.stdlib import StdlibIntegration
8+
9+
10+
def test_crumb_capture(sentry_init, capture_events):
11+
sentry_init(integrations=[StdlibIntegration()])
12+
events = capture_events()
13+
14+
url = "https://httpbin.org/status/200"
15+
response = urlopen(url)
16+
assert response.getcode() == 200
17+
capture_message("Testing!")
18+
19+
event, = events
20+
crumb, = event["breadcrumbs"]
21+
assert crumb["type"] == "http"
22+
assert crumb["category"] == "httplib"
23+
assert crumb["data"] == {
24+
"url": url,
25+
"method": "GET",
26+
"status_code": 200,
27+
"reason": "OK",
28+
}

0 commit comments

Comments
 (0)