Skip to content

Commit 7a8b35d

Browse files
committed
feat: WIP of redis integration
1 parent 05d8a41 commit 7a8b35d

File tree

6 files changed

+60
-16
lines changed

6 files changed

+60
-16
lines changed

sentry_sdk/hub.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from sentry_sdk._compat import with_metaclass
1111
from sentry_sdk.scope import Scope
1212
from sentry_sdk.client import Client
13-
from sentry_sdk.tracing import Span
13+
from sentry_sdk.tracing import Span, maybe_create_breadcrumbs_from_span
1414
from sentry_sdk.utils import (
1515
exc_info_from_error,
1616
event_from_exception,
@@ -385,6 +385,7 @@ def span(self, span=None, **kwargs):
385385
span.set_tag("error", False)
386386
finally:
387387
span.finish()
388+
maybe_create_breadcrumbs_from_span(self, span)
388389
self.finish_trace(span)
389390
scope.span = old_span
390391

@@ -396,7 +397,7 @@ def start_span(self, **kwargs):
396397
span = scope.span
397398
if span is not None:
398399
return span.new_span(**kwargs)
399-
return None
400+
return Span.start_trace(**kwargs)
400401

401402
def start_trace(self, span=None, **kwargs):
402403
if span is None:

sentry_sdk/integrations/redis.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from sentry_sdk import Hub
2+
from sentry_sdk.utils import capture_internal_exceptions
3+
from sentry_sdk.integrations import Integration
4+
5+
6+
class RedisIntegration(Integration):
7+
identifier = "redis"
8+
9+
@staticmethod
10+
def setup_once():
11+
import redis
12+
13+
old_execute_command = redis.StrictRedis.execute_command
14+
15+
def sentry_patched_execute_command(self, name, *args, **kwargs):
16+
hub = Hub.current
17+
18+
if hub.get_integration(RedisIntegration) is None:
19+
return old_execute_command(self, name, *args, **kwargs)
20+
21+
description = name
22+
23+
with capture_internal_exceptions():
24+
description_parts = [name]
25+
for i, arg in enumerate(args):
26+
if i > 10:
27+
break
28+
29+
description_parts.append(repr(arg))
30+
31+
description = " ".join(description_parts)
32+
33+
with hub.span(op="redis", description=description) as span:
34+
if name and args and name.lower() in ("get", "set", "setex", "setnx"):
35+
span.set_tag("redis.key", args[0])
36+
37+
return old_execute_command(self, name, *args, **kwargs)
38+
39+
redis.StrictRedis.execute_command = sentry_patched_execute_command

sentry_sdk/integrations/stdlib.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def getresponse(self, *args, **kwargs):
8282
raise
8383
except Exception:
8484
recorder.__exit__(*sys.exc_info())
85-
pass
85+
raise
8686
else:
8787
recorder.__exit__(None, None, None)
8888

sentry_sdk/tracing.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ def record_sql_queries(hub, queries, label=""):
199199
strings.append(query)
200200

201201
description = concat_strings(strings)
202-
with hub.span(op="db.statement", description=description) as span:
202+
with hub.span(op="db", description=description) as span:
203203
yield span
204204

205205

@@ -210,21 +210,21 @@ def record_http_request(hub, url, method):
210210
with hub.span(op="http", description="%s %s" % (url, method)) as span:
211211
try:
212212
yield data_dict
213-
except Exception:
214-
httplib_response = data_dict.pop("httplib_response", None)
215-
raise
216-
else:
217-
httplib_response = data_dict.pop("httplib_response", None)
218-
219-
hub.add_breadcrumb(
220-
type="http",
221-
category="httplib",
222-
data=data_dict,
223-
hint={"httplib_response": httplib_response},
224-
)
225213
finally:
226214
if span is not None:
227215
if "status_code" in data_dict:
228216
span.set_tag("http.status_code", data_dict["status_code"])
229217
for k, v in data_dict.items():
230218
span.set_data(k, v)
219+
220+
221+
def maybe_create_breadcrumbs_from_span(hub, span):
222+
if span.op == "redis":
223+
hub.add_breadcrumb(type="redis", category="redis", data=span._tags)
224+
elif span.op == "http" and not span._tags.get("error"):
225+
hub.add_breadcrumb(
226+
type="http",
227+
category="httplib",
228+
data=span._data,
229+
hint={"httplib_response": span._data.get("httplib_response")},
230+
)

tests/integrations/requests/test_requests.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ def test_crumb_capture(sentry_init, capture_events):
2323
"method": "GET",
2424
"status_code": 418,
2525
"reason": "I'M A TEAPOT",
26+
"httplib_response": crumb["data"]["httplib_response"],
2627
}

tests/integrations/stdlib/test_httplib.py

+3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ def test_crumb_capture(sentry_init, capture_events):
3232
"method": "GET",
3333
"status_code": 200,
3434
"reason": "OK",
35+
"httplib_response": crumb["data"]["httplib_response"],
3536
}
3637

3738

@@ -61,6 +62,7 @@ def before_breadcrumb(crumb, hint):
6162
"status_code": 200,
6263
"reason": "OK",
6364
"extra": "foo",
65+
"httplib_response": crumb["data"]["httplib_response"],
6466
}
6567

6668

@@ -102,4 +104,5 @@ def test_httplib_misuse(sentry_init, capture_events):
102104
"method": "GET",
103105
"status_code": 200,
104106
"reason": "OK",
107+
"httplib_response": crumb["data"]["httplib_response"],
105108
}

0 commit comments

Comments
 (0)