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