Skip to content

fix(tracer): properly return DynamoDB.DocumentClient #528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/tracing/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ class Tracer implements TracerInterface {
return this.provider.captureAWSClient(service);
} catch (error) {
try {
return this.provider.captureAWSClient((service as unknown as T & { service: T }).service);
this.provider.captureAWSClient((service as unknown as T & { service: T }).service);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed investigation!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dreamorosi question just for myself.

Why do we cast to unknown first and as T in the 2nd step?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, it's a leftover from a previous experiment and it's not needed since T is already a generic. Fixed in latest commit.


return service;
} catch {
throw error;
}
Expand Down
28 changes: 11 additions & 17 deletions packages/tracing/tests/unit/Tracer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { Tracer } from '../../src';
import { Callback, Context, Handler } from 'aws-lambda/handler';
import { Segment, setContextMissingStrategy, Subsegment } from 'aws-xray-sdk-core';
import { DynamoDB } from 'aws-sdk';

interface LambdaInterface {
handler: Handler
Expand Down Expand Up @@ -1057,29 +1058,27 @@ describe('Class: Tracer', () => {
const captureAWSClientSpy = jest.spyOn(tracer.provider, 'captureAWSClient');

// Act
tracer.captureAWSClient({});
const client = tracer.captureAWSClient(new DynamoDB());

// Assess
expect(captureAWSClientSpy).toBeCalledTimes(0);
expect(client).toBeInstanceOf(DynamoDB);

});

test('when called with a simple AWS SDK v2 client, it returns it back instrumented', () => {
test('when called with a base AWS SDK v2 client, it returns it back instrumented', () => {

// Prepare
const tracer: Tracer = new Tracer();
const captureAWSClientSpy = jest.spyOn(tracer.provider, 'captureAWSClient');
// Minimum shape required for a regular AWS v2 client (i.e. AWS.S3) to be instrumented
const dummyClient = {
customizeRequests: () => null,
};

// Act
tracer.captureAWSClient(dummyClient);
const client = tracer.captureAWSClient(new DynamoDB());

// Assess
expect(captureAWSClientSpy).toBeCalledTimes(1);
expect(captureAWSClientSpy).toBeCalledWith(dummyClient);
expect(captureAWSClientSpy).toBeCalledWith(client);
expect(client).toBeInstanceOf(DynamoDB);

});

Expand All @@ -1088,20 +1087,15 @@ describe('Class: Tracer', () => {
// Prepare
const tracer: Tracer = new Tracer();
const captureAWSClientSpy = jest.spyOn(tracer.provider, 'captureAWSClient');
// Minimum shape required for a complex AWS v2 client (i.e. AWS.DocumentClient) to be instrumented
const dummyClient = {
service: {
customizeRequests: () => null,
}
};

// Act
tracer.captureAWSClient(dummyClient);
const client = tracer.captureAWSClient(new DynamoDB.DocumentClient());

// Assess
expect(captureAWSClientSpy).toBeCalledTimes(2);
expect(captureAWSClientSpy).toHaveBeenNthCalledWith(1, dummyClient);
expect(captureAWSClientSpy).toHaveBeenNthCalledWith(2, dummyClient.service);
expect(captureAWSClientSpy).toHaveBeenNthCalledWith(1, client);
expect(captureAWSClientSpy).toHaveBeenNthCalledWith(2, (client as unknown as DynamoDB & { service: DynamoDB }).service);
expect(client).toBeInstanceOf(DynamoDB.DocumentClient);

});

Expand Down