Skip to content

Commit 120f389

Browse files
committed
feat: add support for capturing DynamoDB DocumentClient
1 parent 014c5bd commit 120f389

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

packages/tracing/src/Tracer.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,15 @@ class Tracer implements TracerInterface {
257257
public captureAWSClient<T>(service: T): T {
258258
if (!this.isTracingEnabled()) return service;
259259

260-
return this.provider.captureAWSClient(service);
260+
try {
261+
return this.provider.captureAWSClient(service);
262+
} catch (error) {
263+
try {
264+
return this.provider.captureAWSClient((service as unknown as T & { service: T }).service);
265+
} catch (errorTwo) {
266+
throw error;
267+
}
268+
}
261269
}
262270

263271
/**

packages/tracing/tests/unit/Tracer.test.ts

+48-7
Original file line numberDiff line numberDiff line change
@@ -1054,8 +1054,7 @@ describe('Class: Tracer', () => {
10541054

10551055
// Prepare
10561056
const tracer: Tracer = new Tracer({ enabled: false });
1057-
const captureAWSClientSpy = jest.spyOn(tracer.provider, 'captureAWSClient')
1058-
.mockImplementation(() => null);
1057+
const captureAWSClientSpy = jest.spyOn(tracer.provider, 'captureAWSClient');
10591058

10601059
// Act
10611060
tracer.captureAWSClient({});
@@ -1065,20 +1064,62 @@ describe('Class: Tracer', () => {
10651064

10661065
});
10671066

1068-
test('when called it returns the decorated object that was passed to it', () => {
1067+
test('when called with a simple AWS SDK v2 client, it returns it back instrumented', () => {
10691068

10701069
// Prepare
10711070
const tracer: Tracer = new Tracer();
1072-
const captureAWSClientSpy = jest.spyOn(tracer.provider, 'captureAWSClient')
1073-
.mockImplementation(() => null);
1071+
const captureAWSClientSpy = jest.spyOn(tracer.provider, 'captureAWSClient');
1072+
// Minimum shape required for a regular AWS v2 client (i.e. AWS.S3) to be instrumented
1073+
const dummyClient = {
1074+
customizeRequests: () => null,
1075+
};
10741076

10751077
// Act
1076-
tracer.captureAWSClient({});
1078+
tracer.captureAWSClient(dummyClient);
10771079

10781080
// Assess
10791081
expect(captureAWSClientSpy).toBeCalledTimes(1);
1080-
expect(captureAWSClientSpy).toBeCalledWith({});
1082+
expect(captureAWSClientSpy).toBeCalledWith(dummyClient);
1083+
1084+
});
1085+
1086+
test('when called with a complex AWS SDK v2 client, it returns it back instrumented', () => {
1087+
1088+
// Prepare
1089+
const tracer: Tracer = new Tracer();
1090+
const captureAWSClientSpy = jest.spyOn(tracer.provider, 'captureAWSClient');
1091+
// Minimum shape required for a complex AWS v2 client (i.e. AWS.DocumentClient) to be instrumented
1092+
const dummyClient = {
1093+
service: {
1094+
customizeRequests: () => null,
1095+
}
1096+
};
1097+
1098+
// Act
1099+
tracer.captureAWSClient(dummyClient);
1100+
1101+
// Assess
1102+
expect(captureAWSClientSpy).toBeCalledTimes(2);
1103+
expect(captureAWSClientSpy).toHaveBeenNthCalledWith(1, dummyClient);
1104+
expect(captureAWSClientSpy).toHaveBeenNthCalledWith(2, dummyClient.service);
1105+
1106+
});
1107+
1108+
test('when called with an uncompatible object, it throws an error', () => {
10811109

1110+
// Prepare
1111+
const tracer: Tracer = new Tracer();
1112+
const captureAWSClientSpy = jest.spyOn(tracer.provider, 'captureAWSClient');
1113+
1114+
// Act / Assess
1115+
expect(() => {
1116+
tracer.captureAWSClient({});
1117+
}).toThrow('service.customizeRequests is not a function');
1118+
expect(captureAWSClientSpy).toBeCalledTimes(2);
1119+
expect(captureAWSClientSpy).toHaveBeenNthCalledWith(1, {});
1120+
expect(captureAWSClientSpy).toHaveBeenNthCalledWith(2, undefined);
1121+
expect.assertions(4);
1122+
10821123
});
10831124

10841125
});

0 commit comments

Comments
 (0)