Skip to content

Commit bb1dbaf

Browse files
api call after suppressing tracing
1 parent faac9b6 commit bb1dbaf

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/src/patches/instrumentation-patch.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import type {
3939
Command as AwsV3Command,
4040
HandlerExecutionContext,
4141
Handler as AwsV3MiddlewareHandler,
42+
AwsCredentialIdentity,
4243
} from '@aws-sdk/types';
4344
import { suppressTracing } from '@opentelemetry/core';
4445

@@ -406,6 +407,7 @@ function patchAwsSdkInstrumentation(instrumentation: Instrumentation): void {
406407
}
407408
}
408409

410+
let invocationCount = 0;
409411
function patchAwsSdkCredentialExtraction(instrumentation: Instrumentation): void {
410412
if (instrumentation) {
411413
const originalPatch = (instrumentation as AwsInstrumentation)['_getV3MiddlewareStackResolvePatch'];
@@ -418,32 +420,41 @@ function patchAwsSdkCredentialExtraction(instrumentation: Instrumentation): void
418420

419421
return function (this: any, _handler: any, awsExecutionContext: HandlerExecutionContext) {
420422
const origPatchedHandler = originalHandler.call(this, _handler, awsExecutionContext);
421-
422423
return async function (this: any, command: any): Promise<any> {
424+
invocationCount++;
425+
diag.info(`[patchAwsSdkCredentialExtraction] Invocation #${invocationCount}`);
423426
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;
425430

426-
await otelContext.with(suppressTracing(otelContext.active()), async () => {
431+
// 1. Suppress tracing while resolving credentials (avoids recursion)
432+
await otelContext.with(suppressTracing(activeCtx), async () => {
427433
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);
444440
}
445441
});
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;
447458
};
448459
};
449460
};

0 commit comments

Comments
 (0)