Skip to content

Commit 63da336

Browse files
Add test for Sdk instrumentation patch
1 parent 0640d9f commit 63da336

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

aws-distro-opentelemetry-node-autoinstrumentation/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"@aws-sdk/client-s3": "3.632.0",
7979
"@aws-sdk/client-secrets-manager": "3.632.0",
8080
"@aws-sdk/client-sfn": "3.632.0",
81+
"@aws-sdk/client-sts": "3.632.0",
8182
"@aws-sdk/client-sns": "3.632.0",
8283
"@opentelemetry/contrib-test-utils": "^0.45.0",
8384
"@smithy/protocol-http": "^5.0.1",

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import * as nock from 'nock';
3737
import { ReadableSpan, Span as SDKSpan } from '@opentelemetry/sdk-trace-base';
3838
import { getTestSpans } from '@opentelemetry/contrib-test-utils';
3939
import { instrumentationConfigs } from '../../src/register';
40+
import { STS } from '@aws-sdk/client-sts';
4041

4142
// It is assumed that bedrock.test.ts has already registered the
4243
// necessary instrumentations for testing by calling:
@@ -692,6 +693,47 @@ describe('InstrumentationPatchTest', () => {
692693
});
693694
});
694695

696+
it('prevents recursion when credentials provider makes STS calls', async () => {
697+
let credentialsCallCount = 0;
698+
699+
// Create separate STS client for credential fetching
700+
const credentialsStsClient = new STS({ region: 'us-east-1' });
701+
702+
// Create main client with credentials provider that calls STS
703+
const mainClient = new Lambda({
704+
region: 'us-east-1',
705+
credentials: async () => {
706+
credentialsCallCount++;
707+
// Simulate STS call for credentials (this should be skipped on recursion)
708+
await credentialsStsClient.getCallerIdentity({}).catch(() => {});
709+
return { accessKeyId: 'sts-access-key', secretAccessKey: 'secret' };
710+
},
711+
});
712+
713+
// Mock HTTP responses
714+
nock('https://sts.us-east-1.amazonaws.com')
715+
.post('/')
716+
.reply(200, '<GetCallerIdentityResponse></GetCallerIdentityResponse>');
717+
718+
nock('https://lambda.us-east-1.amazonaws.com').post('/2015-03-31/functions/test/invocations').reply(200, 'null');
719+
720+
// Make Lambda call - this triggers credential extraction which calls STS
721+
await mainClient.invoke({ FunctionName: 'test' }).catch((err: any) => {});
722+
723+
const testSpans = getTestSpans();
724+
const lambdaSpans = testSpans.filter(s => s.name.includes('test Invoke'));
725+
726+
// Verify recursion was prevented - only one credentials call
727+
expect(credentialsCallCount).toBe(1);
728+
expect(lambdaSpans.length).toBe(1);
729+
730+
// Verify span has the extracted credentials attribute
731+
const spanWithCredentials = lambdaSpans.find(
732+
span => span.attributes[AWS_ATTRIBUTE_KEYS.AWS_AUTH_ACCOUNT_ACCESS_KEY] === 'sts-access-key'
733+
);
734+
expect(spanWithCredentials).toBeDefined();
735+
});
736+
695737
it('injects trace context header into request via propagator', async () => {
696738
lambda = new Lambda({
697739
region: region,

0 commit comments

Comments
 (0)