diff --git a/docs/features/tracer.md b/docs/features/tracer.md index b30e1080b1..94c1081dd2 100644 --- a/docs/features/tracer.md +++ b/docs/features/tracer.md @@ -363,7 +363,7 @@ Tracer exposes a `getRootXrayTraceId()` method that allows you to retrieve the [ === "index.ts" - ```typescript hl_lines="9" + ```typescript hl_lines="13" --8<-- "examples/snippets/tracer/accessRootTraceId.ts" ``` diff --git a/examples/snippets/tracer/accessRootTraceId.ts b/examples/snippets/tracer/accessRootTraceId.ts index c09acd2c3c..badd7873ea 100644 --- a/examples/snippets/tracer/accessRootTraceId.ts +++ b/examples/snippets/tracer/accessRootTraceId.ts @@ -1,11 +1,15 @@ +import { Logger } from '@aws-lambda-powertools/logger'; import { Tracer } from '@aws-lambda-powertools/tracer'; const tracer = new Tracer({ serviceName: 'serverlessAirline' }); +const logger = new Logger({ serviceName: 'serverlessAirline' }); export const handler = async (): Promise => { try { throw new Error('Something went wrong'); - } catch (_error) { + } catch (error) { + logger.error('An error occurred', { error }); + const rootTraceId = tracer.getRootXrayTraceId(); // Example of returning an error response diff --git a/packages/tracer/src/Tracer.ts b/packages/tracer/src/Tracer.ts index 05006833a9..590bb2de95 100644 --- a/packages/tracer/src/Tracer.ts +++ b/packages/tracer/src/Tracer.ts @@ -287,6 +287,8 @@ class Tracer extends Utility implements TracerInterface { } /** + * @deprecated Use {@link captureAWSv3Client | `captureAWSv3Client()`} instead. + * * Patch all AWS SDK v2 clients and create traces when your application makes calls to AWS services. * * If you want to patch a specific client use {@link captureAWSClient} and if you are using AWS SDK v3 use {@link captureAWSv3Client} instead. @@ -305,16 +307,17 @@ class Tracer extends Utility implements TracerInterface { * } * ``` * - * @deprecated Use {@link captureAWSv3Client} instead. * @param aws - AWS SDK v2 import */ - public captureAWS(aws: T): T { + /* v8 ignore start */ public captureAWS(aws: T): T { if (!this.isTracingEnabled()) return aws; return this.provider.captureAWS(aws); - } + } /* v8 ignore stop */ /** + * @deprecated Use {@link captureAWSv3Client | `captureAWSv3Client()`} instead. + * * Patch a specific AWS SDK v2 client and create traces when your application makes calls to that AWS service. * * If you want to patch all clients use {@link captureAWS} and if you are using AWS SDK v3 use {@link captureAWSv3Client} instead. @@ -333,7 +336,7 @@ class Tracer extends Utility implements TracerInterface { * ... * } * ``` - * @deprecated Use {@link captureAWSv3Client} instead. + * * @param service - AWS SDK v2 client */ /* v8 ignore start */ public captureAWSClient(service: T): T { diff --git a/packages/tracer/src/provider/ProviderService.ts b/packages/tracer/src/provider/ProviderService.ts index 7839808919..a17331e3f1 100644 --- a/packages/tracer/src/provider/ProviderService.ts +++ b/packages/tracer/src/provider/ProviderService.ts @@ -48,18 +48,18 @@ import { */ class ProviderService implements ProviderServiceInterface { /** - * @deprecated + * @deprecated Use {@link captureAWSv3Client} instead. */ - public captureAWS(awssdk: T): T { + /* v8 ignore start */ public captureAWS(awssdk: T): T { return captureAWS(awssdk); - } + } /* v8 ignore stop */ /** - * @deprecated + * @deprecated Use {@link captureAWSv3Client} instead. */ - public captureAWSClient(service: T): T { + /* v8 ignore start */ public captureAWSClient(service: T): T { return captureAWSClient(service); - } + } /* v8 ignore stop */ public captureAWSv3Client(service: T): T { addUserAgentMiddleware(service, 'tracer'); @@ -119,12 +119,12 @@ class ProviderService implements ProviderServiceInterface { */ const onRequestStart = (message: unknown): void => { const { request } = message as DiagnosticsChannel.RequestCreateMessage; + const method = request.method; + if (method === 'CONNECT') return; const parentSubsegment = this.getSegment(); const requestURL = getRequestURL(request); if (parentSubsegment && requestURL) { - const method = request.method; - const subsegment = parentSubsegment.addNewSubsegment( requestURL.hostname ); diff --git a/packages/tracer/tests/unit/ProviderService.test.ts b/packages/tracer/tests/unit/ProviderService.test.ts index 04134bde59..d10ddd4eb1 100644 --- a/packages/tracer/tests/unit/ProviderService.test.ts +++ b/packages/tracer/tests/unit/ProviderService.test.ts @@ -47,34 +47,6 @@ describe('Class: ProviderService', () => { vi.clearAllMocks(); }); - describe('Method: captureAWS', () => { - it('calls the correct underlying function with proper arguments', () => { - // Prepare - const provider: ProviderService = new ProviderService(); - - // Act - provider.captureAWS({}); - - // Assess - expect(mocks.captureAWS).toHaveBeenCalledTimes(1); - expect(mocks.captureAWS).toHaveBeenCalledWith({}); - }); - }); - - describe('Method: captureAWSClient', () => { - it('calls the correct underlying function with proper arguments', () => { - // Prepare - const provider: ProviderService = new ProviderService(); - - // Act - provider.captureAWSClient({}); - - // Assess - expect(mocks.captureAWSClient).toHaveBeenCalledTimes(1); - expect(mocks.captureAWSClient).toHaveBeenCalledWith({}); - }); - }); - describe('Method: captureAWSv3Client', () => { it('calls the correct underlying function with proper arguments', () => { // Prepare @@ -657,4 +629,22 @@ describe('Class: ProviderService', () => { ) ); }); + + it('skips requests with CONNECT method', () => { + // Prepare + const provider: ProviderService = new ProviderService(); + const segment = new Subsegment('## dummySegment'); + vi.spyOn(provider, 'getSegment').mockImplementation(() => segment); + vi.spyOn(segment, 'addNewSubsegment'); + + // Act + provider.instrumentFetch(); + mockFetch({ + origin: 'https://aws.amazon.com', + method: 'CONNECT', + }); + + // Assess + expect(segment.addNewSubsegment).toHaveBeenCalledTimes(0); + }); }); diff --git a/packages/tracer/tests/unit/Tracer.test.ts b/packages/tracer/tests/unit/Tracer.test.ts index 1909b24e5c..042cead86c 100644 --- a/packages/tracer/tests/unit/Tracer.test.ts +++ b/packages/tracer/tests/unit/Tracer.test.ts @@ -1456,41 +1456,8 @@ describe('Class: Tracer', () => { new Error('dummy error') ); }); - }); - - describe('Method: captureAWS', () => { - it('does nothing when called while tracing is disabled', () => { - // Prepare - const tracer: Tracer = new Tracer({ enabled: false }); - const captureAWSSpy = vi - .spyOn(tracer.provider, 'captureAWS') - .mockImplementation(() => null); - // Act - tracer.captureAWS({}); - - // Assess - expect(captureAWSSpy).toBeCalledTimes(0); - }); - - it('returns the decorated object that was passed to it', () => { - // Prepare - const tracer: Tracer = new Tracer(); - const captureAWSSpy = vi - .spyOn(tracer.provider, 'captureAWS') - .mockImplementation(() => null); - - // Act - tracer.captureAWS({}); - - // Assess - expect(captureAWSSpy).toBeCalledTimes(1); - expect(captureAWSSpy).toBeCalledWith({}); - }); - }); - - describe('Method: captureAWSv3Client', () => { - it('does nothing when tracing is disabled', () => { + it('skips tracing AWS SDK clients when tracing is disabled', () => { // Prepare const tracer: Tracer = new Tracer({ enabled: false }); const captureAWSv3ClientSpy = vi @@ -1504,7 +1471,7 @@ describe('Class: Tracer', () => { expect(captureAWSv3ClientSpy).toBeCalledTimes(0); }); - it('returns the decorated object that was passed to it', () => { + it('returns the instrumented AWS SDK client when called', () => { // Prepare const tracer: Tracer = new Tracer(); const captureAWSv3ClientSpy = vi