1
1
import pytest
2
+
2
3
from sentry_sdk import capture_message
3
- from sentry_sdk .consts import SPANDATA
4
4
from sentry_sdk .api import start_transaction
5
+ from sentry_sdk .consts import SPANDATA
5
6
from sentry_sdk .integrations .redis import RedisIntegration
6
7
8
+ try :
9
+ from unittest import mock
10
+ except ImportError :
11
+ import mock
12
+
7
13
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
+
9
23
10
24
rediscluster_classes = [rediscluster .RedisCluster ]
11
25
@@ -20,7 +34,7 @@ def monkeypatch_rediscluster_classes(reset_integrations):
20
34
except AttributeError :
21
35
pipeline_cls = rediscluster .StrictClusterPipeline
22
36
rediscluster .RedisCluster .pipeline = lambda * _ , ** __ : pipeline_cls (
23
- connection_pool = True
37
+ connection_pool = MOCK_CONNECTION_POOL
24
38
)
25
39
pipeline_cls .execute = lambda * _ , ** __ : None
26
40
for cls in rediscluster_classes :
@@ -32,7 +46,7 @@ def test_rediscluster_basic(rediscluster_cls, sentry_init, capture_events):
32
46
sentry_init (integrations = [RedisIntegration ()])
33
47
events = capture_events ()
34
48
35
- rc = rediscluster_cls (connection_pool = True )
49
+ rc = rediscluster_cls (connection_pool = MOCK_CONNECTION_POOL )
36
50
rc .get ("foobar" )
37
51
capture_message ("hi" )
38
52
@@ -70,17 +84,7 @@ def test_rediscluster_pipeline(
70
84
)
71
85
events = capture_events ()
72
86
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 )
84
88
with start_transaction ():
85
89
pipeline = rc .pipeline ()
86
90
pipeline .get ("foo" )
@@ -98,11 +102,66 @@ def test_rediscluster_pipeline(
98
102
"first_ten" : expected_first_ten ,
99
103
},
100
104
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 ,
104
108
}
105
109
assert span ["tags" ] == {
106
110
"redis.transaction" : False , # For Cluster, this is always False
107
111
"redis.is_cluster" : True ,
108
112
}
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