Skip to content

Commit 3550016

Browse files
committed
Updated tests
1 parent 50ff60b commit 3550016

File tree

3 files changed

+144
-30
lines changed

3 files changed

+144
-30
lines changed

tests/integrations/redis/asyncio/test_redis_asyncio.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from sentry_sdk import capture_message, start_transaction
4+
from sentry_sdk.consts import SPANDATA
45
from sentry_sdk.integrations.redis import RedisIntegration
56

67
from fakeredis.aioredis import FakeRedis
@@ -67,7 +68,13 @@ async def test_async_redis_pipeline(
6768
"redis.commands": {
6869
"count": 3,
6970
"first_ten": expected_first_ten,
70-
}
71+
},
72+
SPANDATA.DB_SYSTEM: "redis",
73+
SPANDATA.DB_NAME: "0",
74+
SPANDATA.SERVER_ADDRESS: connection.connection_pool.connection_kwargs.get(
75+
"host"
76+
),
77+
SPANDATA.SERVER_PORT: 6379,
7178
}
7279
assert span["tags"] == {
7380
"redis.transaction": is_transaction,

tests/integrations/redis/test_redis.py

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212
import mock # python < 3.3
1313

1414

15+
MOCK_CONNECTION_POOL = mock.MagicMock()
16+
MOCK_CONNECTION_POOL.connection_kwargs = {
17+
"host": "localhost",
18+
"port": 63791,
19+
"db": 1,
20+
}
21+
22+
1523
def test_basic(sentry_init, capture_events):
1624
sentry_init(integrations=[RedisIntegration()])
1725
events = capture_events()
@@ -67,17 +75,9 @@ def test_redis_pipeline(
6775
(span,) = event["spans"]
6876
assert span["op"] == "db.redis"
6977
assert span["description"] == "redis.pipeline.execute"
70-
assert span["data"] == {
71-
"redis.commands": {
72-
"count": 3,
73-
"first_ten": expected_first_ten,
74-
},
75-
SPANDATA.DB_SYSTEM: "redis",
76-
SPANDATA.DB_NAME: "0",
77-
SPANDATA.SERVER_ADDRESS: connection.connection_pool.connection_kwargs.get(
78-
"host"
79-
),
80-
SPANDATA.SERVER_PORT: 6379,
78+
assert span["data"]["redis.commands"] == {
79+
"count": 3,
80+
"first_ten": expected_first_ten,
8181
}
8282
assert span["tags"] == {
8383
"redis.transaction": is_transaction,
@@ -247,3 +247,51 @@ def test_breadcrumbs(sentry_init, capture_events):
247247
},
248248
"timestamp": crumbs[1]["timestamp"],
249249
}
250+
251+
252+
def test_db_connection_attributes_client(sentry_init, capture_events):
253+
sentry_init(
254+
traces_sample_rate=1.0,
255+
integrations=[RedisIntegration()],
256+
)
257+
events = capture_events()
258+
259+
with start_transaction():
260+
connection = FakeStrictRedis(connection_pool=MOCK_CONNECTION_POOL)
261+
connection.get("foobar")
262+
263+
(event,) = events
264+
(span,) = event["spans"]
265+
266+
assert span["op"] == "db.redis"
267+
assert span["description"] == "GET 'foobar'"
268+
assert span["data"][SPANDATA.DB_SYSTEM] == "redis"
269+
assert span["data"][SPANDATA.DB_NAME] == "1"
270+
assert span["data"][SPANDATA.SERVER_ADDRESS] == "localhost"
271+
assert span["data"][SPANDATA.SERVER_PORT] == 63791
272+
273+
274+
def test_db_connection_attributes_pipeline(sentry_init, capture_events):
275+
sentry_init(
276+
traces_sample_rate=1.0,
277+
integrations=[RedisIntegration()],
278+
)
279+
events = capture_events()
280+
281+
with start_transaction():
282+
connection = FakeStrictRedis(connection_pool=MOCK_CONNECTION_POOL)
283+
pipeline = connection.pipeline(transaction=False)
284+
pipeline.get("foo")
285+
pipeline.set("bar", 1)
286+
pipeline.set("baz", 2)
287+
pipeline.execute()
288+
289+
(event,) = events
290+
(span,) = event["spans"]
291+
292+
assert span["op"] == "db.redis"
293+
assert span["description"] == "redis.pipeline.execute"
294+
assert span["data"][SPANDATA.DB_SYSTEM] == "redis"
295+
assert span["data"][SPANDATA.DB_NAME] == "1"
296+
assert span["data"][SPANDATA.SERVER_ADDRESS] == "localhost"
297+
assert span["data"][SPANDATA.SERVER_PORT] == 63791

tests/integrations/rediscluster/test_rediscluster.py

Lines changed: 77 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
import pytest
2+
23
from sentry_sdk import capture_message
3-
from sentry_sdk.consts import SPANDATA
44
from sentry_sdk.api import start_transaction
5+
from sentry_sdk.consts import SPANDATA
56
from sentry_sdk.integrations.redis import RedisIntegration
67

8+
try:
9+
from unittest import mock
10+
except ImportError:
11+
import mock
12+
713
import rediscluster
8-
from fakeredis import FakeStrictRedis
14+
15+
16+
MOCK_CONNECTION_POOL = mock.MagicMock()
17+
MOCK_CONNECTION_POOL.connection_kwargs = {
18+
"host": "localhost",
19+
"port": 63791,
20+
"db": 1,
21+
}
22+
923

1024
rediscluster_classes = [rediscluster.RedisCluster]
1125

@@ -20,7 +34,7 @@ def monkeypatch_rediscluster_classes(reset_integrations):
2034
except AttributeError:
2135
pipeline_cls = rediscluster.StrictClusterPipeline
2236
rediscluster.RedisCluster.pipeline = lambda *_, **__: pipeline_cls(
23-
connection_pool=True
37+
connection_pool=MOCK_CONNECTION_POOL
2438
)
2539
pipeline_cls.execute = lambda *_, **__: None
2640
for cls in rediscluster_classes:
@@ -32,7 +46,7 @@ def test_rediscluster_basic(rediscluster_cls, sentry_init, capture_events):
3246
sentry_init(integrations=[RedisIntegration()])
3347
events = capture_events()
3448

35-
rc = rediscluster_cls(connection_pool=True)
49+
rc = rediscluster_cls(connection_pool=MOCK_CONNECTION_POOL)
3650
rc.get("foobar")
3751
capture_message("hi")
3852

@@ -70,17 +84,7 @@ def test_rediscluster_pipeline(
7084
)
7185
events = capture_events()
7286

73-
connection = FakeStrictRedis()
74-
startup_nodes = [
75-
{
76-
"host": connection.connection_pool.connection_kwargs.get("host"),
77-
"port": connection.connection_pool.connection_kwargs.get("port"),
78-
}
79-
]
80-
print("////////////")
81-
print(startup_nodes)
82-
83-
rc = rediscluster.RedisCluster(startup_nodes=startup_nodes)
87+
rc = rediscluster.RedisCluster(connection_pool=MOCK_CONNECTION_POOL)
8488
with start_transaction():
8589
pipeline = rc.pipeline()
8690
pipeline.get("foo")
@@ -98,11 +102,66 @@ def test_rediscluster_pipeline(
98102
"first_ten": expected_first_ten,
99103
},
100104
SPANDATA.DB_SYSTEM: "redis",
101-
SPANDATA.DB_NAME: "0",
102-
SPANDATA.SERVER_ADDRESS: rc.connection_pool.connection_kwargs.get("host"),
103-
SPANDATA.SERVER_PORT: 6379,
105+
SPANDATA.DB_NAME: "1",
106+
SPANDATA.SERVER_ADDRESS: "localhost",
107+
SPANDATA.SERVER_PORT: 63791,
104108
}
105109
assert span["tags"] == {
106110
"redis.transaction": False, # For Cluster, this is always False
107111
"redis.is_cluster": True,
108112
}
113+
114+
115+
@pytest.mark.parametrize("rediscluster_cls", rediscluster_classes)
116+
def test_db_connection_attributes_client(sentry_init, capture_events, rediscluster_cls):
117+
sentry_init(
118+
traces_sample_rate=1.0,
119+
integrations=[RedisIntegration()],
120+
)
121+
events = capture_events()
122+
123+
rc = rediscluster_cls(connection_pool=MOCK_CONNECTION_POOL)
124+
with start_transaction():
125+
rc.get("foobar")
126+
127+
(event,) = events
128+
(span,) = event["spans"]
129+
130+
assert span["data"] == {
131+
SPANDATA.DB_SYSTEM: "redis",
132+
SPANDATA.DB_NAME: "1",
133+
SPANDATA.SERVER_ADDRESS: "localhost",
134+
SPANDATA.SERVER_PORT: 63791,
135+
}
136+
137+
138+
@pytest.mark.parametrize("rediscluster_cls", rediscluster_classes)
139+
def test_db_connection_attributes_pipeline(
140+
sentry_init, capture_events, rediscluster_cls
141+
):
142+
sentry_init(
143+
traces_sample_rate=1.0,
144+
integrations=[RedisIntegration()],
145+
)
146+
events = capture_events()
147+
148+
rc = rediscluster.RedisCluster(connection_pool=MOCK_CONNECTION_POOL)
149+
with start_transaction():
150+
pipeline = rc.pipeline()
151+
pipeline.get("foo")
152+
pipeline.execute()
153+
154+
(event,) = events
155+
(span,) = event["spans"]
156+
assert span["op"] == "db.redis"
157+
assert span["description"] == "redis.pipeline.execute"
158+
assert span["data"] == {
159+
"redis.commands": {
160+
"count": 1,
161+
"first_ten": ["GET 'foo'"],
162+
},
163+
SPANDATA.DB_SYSTEM: "redis",
164+
SPANDATA.DB_NAME: "1",
165+
SPANDATA.SERVER_ADDRESS: "localhost",
166+
SPANDATA.SERVER_PORT: 63791,
167+
}

0 commit comments

Comments
 (0)