@@ -39,6 +39,7 @@ import type {
39
39
Command as AwsV3Command ,
40
40
HandlerExecutionContext ,
41
41
Handler as AwsV3MiddlewareHandler ,
42
+ AwsCredentialIdentity ,
42
43
} from '@aws-sdk/types' ;
43
44
import { suppressTracing } from '@opentelemetry/core' ;
44
45
@@ -406,6 +407,7 @@ function patchAwsSdkInstrumentation(instrumentation: Instrumentation): void {
406
407
}
407
408
}
408
409
410
+ let invocationCount = 0 ;
409
411
function patchAwsSdkCredentialExtraction ( instrumentation : Instrumentation ) : void {
410
412
if ( instrumentation ) {
411
413
const originalPatch = ( instrumentation as AwsInstrumentation ) [ '_getV3MiddlewareStackResolvePatch' ] ;
@@ -418,32 +420,41 @@ function patchAwsSdkCredentialExtraction(instrumentation: Instrumentation): void
418
420
419
421
return function ( this : any , _handler : any , awsExecutionContext : HandlerExecutionContext ) {
420
422
const origPatchedHandler = originalHandler . call ( this , _handler , awsExecutionContext ) ;
421
-
422
423
return async function ( this : any , command : any ) : Promise < any > {
424
+ invocationCount ++ ;
425
+ diag . info ( `[patchAwsSdkCredentialExtraction] Invocation #${ invocationCount } ` ) ;
423
426
const clientConfig = command [ V3_CLIENT_CONFIG_KEY ] ;
424
- const originalPromise = origPatchedHandler . call ( this , command ) ;
427
+ const activeCtx = otelContext . active ( ) ;
428
+ let credentials : AwsCredentialIdentity | undefined ;
429
+ let region : string | undefined ;
425
430
426
- await otelContext . with ( suppressTracing ( otelContext . active ( ) ) , async ( ) => {
431
+ // 1. Suppress tracing while resolving credentials (avoids recursion)
432
+ await otelContext . with ( suppressTracing ( activeCtx ) , async ( ) => {
427
433
try {
428
- const [ credentials , region ] = await Promise . all ( [
429
- clientConfig ?. credentials ?.( ) ,
430
- clientConfig ?. region ?.( ) ,
431
- ] ) ;
432
-
433
- const span = trace . getActiveSpan ( ) ;
434
- if ( span ) {
435
- if ( credentials ?. accessKeyId ) {
436
- span . setAttribute ( AWS_ATTRIBUTE_KEYS . AWS_AUTH_ACCOUNT_ACCESS_KEY , credentials . accessKeyId ) ;
437
- }
438
- if ( region ) {
439
- span . setAttribute ( AWS_ATTRIBUTE_KEYS . AWS_AUTH_REGION , region ) ;
440
- }
441
- }
442
- } catch ( e ) {
443
- // Don't crash or log unless debugging
434
+ diag . info ( '[patchAwsSdkCredentialExtraction] Resolving credentials...' ) ;
435
+ credentials = ( await clientConfig ?. credentials ?.( ) ) as AwsCredentialIdentity | undefined ;
436
+ diag . info ( '[patchAwsSdkCredentialExtraction] Credentials resolved' ) ;
437
+ region = await clientConfig ?. region ?.( ) ;
438
+ } catch ( err ) {
439
+ diag . error ( '[patchAwsSdkCredentialExtraction] Error during credential/region resolution:' , err ) ;
444
440
}
445
441
} ) ;
446
- return originalPromise ;
442
+
443
+ // 2. Now call the actual SDK operation (span will be created here)
444
+ const result = await origPatchedHandler . call ( this , command ) ;
445
+
446
+ // 3. Annotate the current span (which was created just now)
447
+ const span = trace . getSpan ( activeCtx ) ;
448
+ if ( span ) {
449
+ if ( credentials ?. accessKeyId ) {
450
+ span . setAttribute ( AWS_ATTRIBUTE_KEYS . AWS_AUTH_ACCOUNT_ACCESS_KEY , credentials . accessKeyId ) ;
451
+ }
452
+ if ( region ) {
453
+ span . setAttribute ( AWS_ATTRIBUTE_KEYS . AWS_AUTH_REGION , region ) ;
454
+ }
455
+ }
456
+
457
+ return result ;
447
458
} ;
448
459
} ;
449
460
} ;
0 commit comments