@@ -33,6 +33,7 @@ import { defaultDbStatementSerializer } from '@opentelemetry/redis-common';
33
33
import { RedisInstrumentationConfig } from './types' ;
34
34
import { VERSION } from './version' ;
35
35
import { SemanticAttributes } from '@opentelemetry/semantic-conventions' ;
36
+ import type { MultiErrorReply } from './internal-types' ;
36
37
37
38
const OTEL_OPEN_SPANS = Symbol (
38
39
'opentelemetry.instruemntation.redis.open_spans'
@@ -289,55 +290,21 @@ export class RedisInstrumentation extends InstrumentationBase<any> {
289
290
return execRes
290
291
. then ( ( redisRes : unknown [ ] ) => {
291
292
const openSpans = this [ OTEL_OPEN_SPANS ] ;
292
- if ( ! openSpans ) {
293
- return plugin . _diag . error (
294
- 'cannot find open spans to end for redis multi command'
295
- ) ;
296
- }
297
- if ( redisRes . length !== openSpans . length ) {
298
- return plugin . _diag . error (
299
- 'number of multi command spans does not match response from redis'
300
- ) ;
301
- }
302
- for ( let i = 0 ; i < openSpans . length ; i ++ ) {
303
- const { span, commandName, commandArgs } = openSpans [ i ] ;
304
- const currCommandRes = redisRes [ i ] ;
305
- if ( currCommandRes instanceof Error ) {
306
- plugin . _endSpanWithResponse (
307
- span ,
308
- commandName ,
309
- commandArgs ,
310
- null ,
311
- currCommandRes
312
- ) ;
313
- } else {
314
- plugin . _endSpanWithResponse (
315
- span ,
316
- commandName ,
317
- commandArgs ,
318
- currCommandRes ,
319
- undefined
320
- ) ;
321
- }
322
- }
293
+ plugin . _endSpansWithRedisReplies ( openSpans , redisRes ) ;
323
294
return redisRes ;
324
295
} )
325
296
. catch ( ( err : Error ) => {
326
297
const openSpans = this [ OTEL_OPEN_SPANS ] ;
327
298
if ( ! openSpans ) {
328
- return plugin . _diag . error (
299
+ plugin . _diag . error (
329
300
'cannot find open spans to end for redis multi command'
330
301
) ;
331
- }
332
- for ( let i = 0 ; i < openSpans . length ; i ++ ) {
333
- const { span, commandName, commandArgs } = openSpans [ i ] ;
334
- plugin . _endSpanWithResponse (
335
- span ,
336
- commandName ,
337
- commandArgs ,
338
- null ,
339
- err
340
- ) ;
302
+ } else {
303
+ const replies =
304
+ err . constructor . name === 'MultiErrorReply'
305
+ ? ( err as MultiErrorReply ) . replies
306
+ : new Array ( openSpans . length ) . fill ( err ) ;
307
+ plugin . _endSpansWithRedisReplies ( openSpans , replies ) ;
341
308
}
342
309
return Promise . reject ( err ) ;
343
310
} ) ;
@@ -487,6 +454,31 @@ export class RedisInstrumentation extends InstrumentationBase<any> {
487
454
return res ;
488
455
}
489
456
457
+ private _endSpansWithRedisReplies (
458
+ openSpans : Array < MutliCommandInfo > ,
459
+ replies : unknown [ ]
460
+ ) {
461
+ if ( ! openSpans ) {
462
+ return this . _diag . error (
463
+ 'cannot find open spans to end for redis multi command'
464
+ ) ;
465
+ }
466
+ if ( replies . length !== openSpans . length ) {
467
+ return this . _diag . error (
468
+ 'number of multi command spans does not match response from redis'
469
+ ) ;
470
+ }
471
+ for ( let i = 0 ; i < openSpans . length ; i ++ ) {
472
+ const { span, commandName, commandArgs } = openSpans [ i ] ;
473
+ const currCommandRes = replies [ i ] ;
474
+ const [ res , err ] =
475
+ currCommandRes instanceof Error
476
+ ? [ null , currCommandRes ]
477
+ : [ currCommandRes , undefined ] ;
478
+ this . _endSpanWithResponse ( span , commandName , commandArgs , res , err ) ;
479
+ }
480
+ }
481
+
490
482
private _endSpanWithResponse (
491
483
span : Span ,
492
484
commandName : string ,
0 commit comments