@@ -157,6 +157,44 @@ def _set_connection_attributes(span, conn):
157
157
span .set_attribute (key , value )
158
158
159
159
160
+ def _build_span_name (instance , cmd_args ):
161
+ if len (cmd_args ) > 0 and cmd_args [0 ]:
162
+ name = cmd_args [0 ]
163
+ else :
164
+ name = instance .connection_pool .connection_kwargs .get ("db" , 0 )
165
+ return name
166
+
167
+
168
+ def _build_span_meta_data_for_pipeline (instance , sanitize_query ):
169
+ try :
170
+ command_stack = (
171
+ instance .command_stack
172
+ if hasattr (instance , "command_stack" )
173
+ else instance ._command_stack
174
+ )
175
+
176
+ cmds = [
177
+ _format_command_args (
178
+ c .args if hasattr (c , "args" ) else c [0 ], sanitize_query
179
+ )
180
+ for c in command_stack
181
+ ]
182
+ resource = "\n " .join (cmds )
183
+
184
+ span_name = " " .join (
185
+ [
186
+ (c .args [0 ] if hasattr (c , "args" ) else c [0 ][0 ])
187
+ for c in command_stack
188
+ ]
189
+ )
190
+ except (AttributeError , IndexError ):
191
+ command_stack = []
192
+ resource = ""
193
+ span_name = ""
194
+
195
+ return command_stack , resource , span_name
196
+
197
+
160
198
def _instrument (
161
199
tracer ,
162
200
request_hook : _RequestHookT = None ,
@@ -165,11 +203,8 @@ def _instrument(
165
203
):
166
204
def _traced_execute_command (func , instance , args , kwargs ):
167
205
query = _format_command_args (args , sanitize_query )
206
+ name = _build_span_name (instance , args )
168
207
169
- if len (args ) > 0 and args [0 ]:
170
- name = args [0 ]
171
- else :
172
- name = instance .connection_pool .connection_kwargs .get ("db" , 0 )
173
208
with tracer .start_as_current_span (
174
209
name , kind = trace .SpanKind .CLIENT
175
210
) as span :
@@ -185,31 +220,11 @@ def _traced_execute_command(func, instance, args, kwargs):
185
220
return response
186
221
187
222
def _traced_execute_pipeline (func , instance , args , kwargs ):
188
- try :
189
- command_stack = (
190
- instance .command_stack
191
- if hasattr (instance , "command_stack" )
192
- else instance ._command_stack
193
- )
194
-
195
- cmds = [
196
- _format_command_args (
197
- c .args if hasattr (c , "args" ) else c [0 ], sanitize_query
198
- )
199
- for c in command_stack
200
- ]
201
- resource = "\n " .join (cmds )
202
-
203
- span_name = " " .join (
204
- [
205
- (c .args [0 ] if hasattr (c , "args" ) else c [0 ][0 ])
206
- for c in command_stack
207
- ]
208
- )
209
- except (AttributeError , IndexError ):
210
- command_stack = []
211
- resource = ""
212
- span_name = ""
223
+ (
224
+ command_stack ,
225
+ resource ,
226
+ span_name ,
227
+ ) = _build_span_meta_data_for_pipeline (instance , sanitize_query )
213
228
214
229
with tracer .start_as_current_span (
215
230
span_name , kind = trace .SpanKind .CLIENT
@@ -254,32 +269,72 @@ def _traced_execute_pipeline(func, instance, args, kwargs):
254
269
"ClusterPipeline.execute" ,
255
270
_traced_execute_pipeline ,
256
271
)
272
+
273
+ async def _async_traced_execute_command (func , instance , args , kwargs ):
274
+ query = _format_command_args (args , sanitize_query )
275
+ name = _build_span_name (instance , args )
276
+
277
+ with tracer .start_as_current_span (
278
+ name , kind = trace .SpanKind .CLIENT
279
+ ) as span :
280
+ if span .is_recording ():
281
+ span .set_attribute (SpanAttributes .DB_STATEMENT , query )
282
+ _set_connection_attributes (span , instance )
283
+ span .set_attribute ("db.redis.args_length" , len (args ))
284
+ if callable (request_hook ):
285
+ request_hook (span , instance , args , kwargs )
286
+ response = await func (* args , ** kwargs )
287
+ if callable (response_hook ):
288
+ response_hook (span , instance , response )
289
+ return response
290
+
291
+ async def _async_traced_execute_pipeline (func , instance , args , kwargs ):
292
+ (
293
+ command_stack ,
294
+ resource ,
295
+ span_name ,
296
+ ) = _build_span_meta_data_for_pipeline (instance , sanitize_query )
297
+
298
+ with tracer .start_as_current_span (
299
+ span_name , kind = trace .SpanKind .CLIENT
300
+ ) as span :
301
+ if span .is_recording ():
302
+ span .set_attribute (SpanAttributes .DB_STATEMENT , resource )
303
+ _set_connection_attributes (span , instance )
304
+ span .set_attribute (
305
+ "db.redis.pipeline_length" , len (command_stack )
306
+ )
307
+ response = await func (* args , ** kwargs )
308
+ if callable (response_hook ):
309
+ response_hook (span , instance , response )
310
+ return response
311
+
257
312
if redis .VERSION >= _REDIS_ASYNCIO_VERSION :
258
313
wrap_function_wrapper (
259
314
"redis.asyncio" ,
260
315
f"{ redis_class } .execute_command" ,
261
- _traced_execute_command ,
316
+ _async_traced_execute_command ,
262
317
)
263
318
wrap_function_wrapper (
264
319
"redis.asyncio.client" ,
265
320
f"{ pipeline_class } .execute" ,
266
- _traced_execute_pipeline ,
321
+ _async_traced_execute_pipeline ,
267
322
)
268
323
wrap_function_wrapper (
269
324
"redis.asyncio.client" ,
270
325
f"{ pipeline_class } .immediate_execute_command" ,
271
- _traced_execute_command ,
326
+ _async_traced_execute_command ,
272
327
)
273
328
if redis .VERSION >= _REDIS_ASYNCIO_CLUSTER_VERSION :
274
329
wrap_function_wrapper (
275
330
"redis.asyncio.cluster" ,
276
331
"RedisCluster.execute_command" ,
277
- _traced_execute_command ,
332
+ _async_traced_execute_command ,
278
333
)
279
334
wrap_function_wrapper (
280
335
"redis.asyncio.cluster" ,
281
336
"ClusterPipeline.execute" ,
282
- _traced_execute_pipeline ,
337
+ _async_traced_execute_pipeline ,
283
338
)
284
339
285
340
0 commit comments