@@ -124,6 +124,70 @@ def test_parent(self):
124
124
self .assertEqual (child_span .name , "GET" )
125
125
126
126
127
+ class TestRedisClusterInstrument (TestBase ):
128
+ def setUp (self ):
129
+ super ().setUp ()
130
+ self .redis_client = redis .cluster .RedisCluster (host = "localhost" , port = 7000 )
131
+ self .redis_client .flushall ()
132
+ RedisInstrumentor ().instrument (tracer_provider = self .tracer_provider )
133
+
134
+ def tearDown (self ):
135
+ super ().tearDown ()
136
+ RedisInstrumentor ().uninstrument ()
137
+
138
+ def _check_span (self , span , name ):
139
+ self .assertEqual (span .name , name )
140
+ self .assertIs (span .status .status_code , trace .StatusCode .UNSET )
141
+
142
+ def test_basics (self ):
143
+ self .assertIsNone (self .redis_client .get ("cheese" ))
144
+ spans = self .memory_exporter .get_finished_spans ()
145
+ self .assertEqual (len (spans ), 1 )
146
+ span = spans [0 ]
147
+ self ._check_span (span , "GET" )
148
+ self .assertEqual (
149
+ span .attributes .get (SpanAttributes .DB_STATEMENT ), "GET cheese"
150
+ )
151
+ self .assertEqual (span .attributes .get ("db.redis.args_length" ), 2 )
152
+
153
+ def test_pipeline_traced (self ):
154
+ with self .redis_client .pipeline (transaction = False ) as pipeline :
155
+ pipeline .set ("blah" , 32 )
156
+ pipeline .rpush ("foo" , "éé" )
157
+ pipeline .hgetall ("xxx" )
158
+ pipeline .execute ()
159
+
160
+ spans = self .memory_exporter .get_finished_spans ()
161
+ self .assertEqual (len (spans ), 1 )
162
+ span = spans [0 ]
163
+ self ._check_span (span , "SET RPUSH HGETALL" )
164
+ self .assertEqual (
165
+ span .attributes .get (SpanAttributes .DB_STATEMENT ),
166
+ "SET blah 32\n RPUSH foo éé\n HGETALL xxx" ,
167
+ )
168
+ self .assertEqual (span .attributes .get ("db.redis.pipeline_length" ), 3 )
169
+
170
+ def test_parent (self ):
171
+ """Ensure OpenTelemetry works with redis."""
172
+ ot_tracer = trace .get_tracer ("redis_svc" )
173
+
174
+ with ot_tracer .start_as_current_span ("redis_get" ):
175
+ self .assertIsNone (self .redis_client .get ("cheese" ))
176
+
177
+ spans = self .memory_exporter .get_finished_spans ()
178
+ self .assertEqual (len (spans ), 2 )
179
+ child_span , parent_span = spans [0 ], spans [1 ]
180
+
181
+ # confirm the parenting
182
+ self .assertIsNone (parent_span .parent )
183
+ self .assertIs (child_span .parent , parent_span .get_span_context ())
184
+
185
+ self .assertEqual (parent_span .name , "redis_get" )
186
+ self .assertEqual (parent_span .instrumentation_info .name , "redis_svc" )
187
+
188
+ self .assertEqual (child_span .name , "GET" )
189
+
190
+
127
191
def async_call (coro ):
128
192
loop = asyncio .get_event_loop ()
129
193
return loop .run_until_complete (coro )
@@ -238,6 +302,75 @@ def test_parent(self):
238
302
self .assertEqual (child_span .name , "GET" )
239
303
240
304
305
+ class TestAsyncRedisClusterInstrument (TestBase ):
306
+ def setUp (self ):
307
+ super ().setUp ()
308
+ self .redis_client = redis .asyncio .cluster .RedisCluster (host = "localhost" , port = 7000 )
309
+ async_call (self .redis_client .flushall ())
310
+ RedisInstrumentor ().instrument (tracer_provider = self .tracer_provider )
311
+
312
+ def tearDown (self ):
313
+ super ().tearDown ()
314
+ RedisInstrumentor ().uninstrument ()
315
+
316
+ def _check_span (self , span , name ):
317
+ self .assertEqual (span .name , name )
318
+ self .assertIs (span .status .status_code , trace .StatusCode .UNSET )
319
+
320
+ def test_basics (self ):
321
+ self .assertIsNone (async_call (self .redis_client .get ("cheese" )))
322
+ spans = self .memory_exporter .get_finished_spans ()
323
+ self .assertEqual (len (spans ), 1 )
324
+ span = spans [0 ]
325
+ self ._check_span (span , "GET" )
326
+ self .assertEqual (
327
+ span .attributes .get (SpanAttributes .DB_STATEMENT ), "GET cheese"
328
+ )
329
+ self .assertEqual (span .attributes .get ("db.redis.args_length" ), 2 )
330
+
331
+ def test_pipeline_traced (self ):
332
+ async def pipeline_simple ():
333
+ async with self .redis_client .pipeline (
334
+ transaction = False
335
+ ) as pipeline :
336
+ pipeline .set ("blah" , 32 )
337
+ pipeline .rpush ("foo" , "éé" )
338
+ pipeline .hgetall ("xxx" )
339
+ await pipeline .execute ()
340
+
341
+ async_call (pipeline_simple ())
342
+
343
+ spans = self .memory_exporter .get_finished_spans ()
344
+ self .assertEqual (len (spans ), 1 )
345
+ span = spans [0 ]
346
+ self ._check_span (span , "SET RPUSH HGETALL" )
347
+ self .assertEqual (
348
+ span .attributes .get (SpanAttributes .DB_STATEMENT ),
349
+ "SET blah 32\n RPUSH foo éé\n HGETALL xxx" ,
350
+ )
351
+ self .assertEqual (span .attributes .get ("db.redis.pipeline_length" ), 3 )
352
+
353
+ def test_parent (self ):
354
+ """Ensure OpenTelemetry works with redis."""
355
+ ot_tracer = trace .get_tracer ("redis_svc" )
356
+
357
+ with ot_tracer .start_as_current_span ("redis_get" ):
358
+ self .assertIsNone (async_call (self .redis_client .get ("cheese" )))
359
+
360
+ spans = self .memory_exporter .get_finished_spans ()
361
+ self .assertEqual (len (spans ), 2 )
362
+ child_span , parent_span = spans [0 ], spans [1 ]
363
+
364
+ # confirm the parenting
365
+ self .assertIsNone (parent_span .parent )
366
+ self .assertIs (child_span .parent , parent_span .get_span_context ())
367
+
368
+ self .assertEqual (parent_span .name , "redis_get" )
369
+ self .assertEqual (parent_span .instrumentation_info .name , "redis_svc" )
370
+
371
+ self .assertEqual (child_span .name , "GET" )
372
+
373
+
241
374
class TestRedisDBIndexInstrument (TestBase ):
242
375
def setUp (self ):
243
376
super ().setUp ()
0 commit comments