10000 fix: Auto-enabling Redis and Pyramid integration (#737) · tinylambda/sentry-python@67c0279 · GitHub
[go: up one dir, main page]

Skip to content

Commit 67c0279

Browse files
fix: Auto-enabling Redis and Pyramid integration (getsentry#737)
* fix: Auto-enabling Redis and Pyramid integration * fix(tests): fixed getting right span * fix(tests): Fixing check for redis, because it is a dependency for runnings tests and therefore always enabled * fix(tests): Fix for Flask not pinning requirements Co-authored-by: Anton Pirker <anton.pirker@sentry.io>
1 parent b449fff commit 67c0279

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

sentry_sdk/integrations/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ def iter_default_integrations(with_auto_enabling_integrations):
6262
"sentry_sdk.integrations.aiohttp.AioHttpIntegration",
6363
"sentry_sdk.integrations.tornado.TornadoIntegration",
6464
"sentry_sdk.integrations.sqlalchemy.SqlalchemyIntegration",
65+
"sentry_sdk.integrations.redis.RedisIntegration",
66+
"sentry_sdk.integrations.pyramid.PyramidIntegration",
6567
"sentry_sdk.integrations.boto3.Boto3Integration",
6668
)
6769

sentry_sdk/integrations/pyramid.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
import sys
55
import weakref
66

7-
from pyramid.httpexceptions import HTTPException
8-
from pyramid.request import Request
9-
107
from sentry_sdk.hub import Hub, _should_send_default_pii
118
from sentry_sdk.utils import capture_internal_exceptions, event_from_exception
129
from sentry_sdk._compat import reraise, iteritems
1310

14-
from sentry_sdk.integrations import Integration
11+
from sentry_sdk.integrations import Integration, DidNotEnable
1512
from sentry_sdk.integrations._wsgi_common import RequestExtractor
1613
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
1714

15+
try:
16+
from pyramid.httpexceptions import HTTPException
17+
from pyramid.request import Request
18+
except ImportError:
19+
raise DidNotEnable("Pyramid not installed")
20+
1821
from sentry_sdk._types import MYPY
1922

2023
if MYPY:
@@ -64,7 +67,6 @@ def __init__(self, transaction_style="route_name"):
6467
def setup_once():
6568
# type: () -> None
6669
from pyramid import router
67-
from pyramid.request import Request
6870

6971
old_call_view = router._call_view
7072

sentry_sdk/integrations/redis.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from sentry_sdk import Hub
44
from sentry_sdk.utils import capture_internal_exceptions, logger
5-
from sentry_sdk.integrations import Integration
5+
from sentry_sdk.integrations import Integration, DidNotEnable
66

77
from sentry_sdk._types import MYPY
88

@@ -40,7 +40,10 @@ class RedisIntegration(Integration):
4040
@staticmethod
4141
def setup_once():
4242
# type: () -> None
43-
import redis
43+
try:
44+
import redis
45+
except ImportError:
46+
raise DidNotEnable("Redis client not installed")
4447

4548
patch_redis_client(redis.StrictRedis)
4649

tests/integrations/celery/test_celery.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ def dummy_task(x, y):
171171
assert execution_event["spans"] == []
172172
assert submission_event["spans"] == [
173173
{
174-
u"description": u"dummy_task",
175-
u"op": "celery.submit",
176-
u"parent_span_id": submission_event["contexts"]["trace"]["span_id"],
177-
u"same_process_as_parent": True,
178-
u"span_id": submission_event["spans"][0]["span_id"],
179-
u"start_timestamp": submission_event["spans"][0]["start_timestamp"],
180-
u"timestamp": submission_event["spans"][0]["timestamp"],
181-
u"trace_id": text_type(transaction.trace_id),
174+
"description": "dummy_task",
175+
"op": "celery.submit",
176+
"parent_span_id": submission_event["contexts"]["trace"]["span_id"],
177+
"same_process_as_parent": True,
178+
"span_id": submission_event["spans"][0]["span_id"],
179+
"start_timestamp": submission_event["spans"][0]["start_timestamp"],
180+
"timestamp": submission_event["spans"][0]["timestamp"],
181+
"trace_id": text_type(transaction.trace_id),
182182
}
183183
]
184184

@@ -338,7 +338,11 @@ def dummy_task(self):
338338
submit_transaction = events.read_event()
339339
assert submit_transaction["type"] == "transaction"
340340
assert submit_transaction["transaction"] == "submit_celery"
341-
(span,) = submit_transaction["spans"]
341+
342+
assert len(
343+
submit_transaction["spans"]
344+
), 4 # Because redis integration was auto enabled
345+
span = submit_transaction["spans"][0]
342346
assert span["op"] == "celery.submit"
343347
assert span["description"] == "dummy_task"
344348

tests/test_basics.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,16 @@ def error_processor(event, exc_info):
5050

5151
def test_auto_enabling_integrations_catches_import_error(sentry_init, caplog):
5252
caplog.set_level(logging.DEBUG)
53+
REDIS = 10 # noqa: N806
5354

5455
sentry_init(auto_enabling_integrations=True, debug=True)
5556

5657
for import_string in _AUTO_ENABLING_INTEGRATIONS:
58+
# Ignore redis in the test case, because it is installed as a
59+
# dependency for running tests, and therefore always enabled.
60+
if _AUTO_ENABLING_INTEGRATIONS[REDIS] == import_string:
61+
continue
62+
5763
assert any(
5864
record.message.startswith(
5965
"Did not import default integration {}:".format(import_string)

0 commit comments

Comments
 (0)
0