Skip to content

Commit a147c3b

Browse files
authored
fix(tracer): skip tracing CONNECT requests (#4148)
1 parent 51d9b98 commit a147c3b

File tree

6 files changed

+41
-77
lines changed

6 files changed

+41
-77
lines changed

docs/features/tracer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ Tracer exposes a `getRootXrayTraceId()` method that allows you to retrieve the [
363363

364364
=== "index.ts"
365365

366-
```typescript hl_lines="9"
366+
```typescript hl_lines="13"
367367
--8<-- "examples/snippets/tracer/accessRootTraceId.ts"
368368
```
369369

examples/snippets/tracer/accessRootTraceId.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import { Logger } from '@aws-lambda-powertools/logger';
12
import { Tracer } from '@aws-lambda-powertools/tracer';
23

34
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
5+
const logger = new Logger({ serviceName: 'serverlessAirline' });
46

57
export const handler = async (): Promise<unknown> => {
68
try {
79
throw new Error('Something went wrong');
8-
} catch (_error) {
10+
} catch (error) {
11+
logger.error('An error occurred', { error });
12+
913
const rootTraceId = tracer.getRootXrayTraceId();
1014

1115
// Example of returning an error response

packages/tracer/src/Tracer.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ class Tracer extends Utility implements TracerInterface {
287287
}
288288

289289
/**
290+
* @deprecated Use {@link captureAWSv3Client | `captureAWSv3Client()`} instead.
291+
*
290292
* Patch all AWS SDK v2 clients and create traces when your application makes calls to AWS services.
291293
*
292294
* 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 {
305307
* }
306308
* ```
307309
*
308-
* @deprecated Use {@link captureAWSv3Client} instead.
309310
* @param aws - AWS SDK v2 import
310311
*/
311-
public captureAWS<T>(aws: T): T {
312+
/* v8 ignore start */ public captureAWS<T>(aws: T): T {
312313
if (!this.isTracingEnabled()) return aws;
313314

314315
return this.provider.captureAWS(aws);
315-
}
316+
} /* v8 ignore stop */
316317

317318
/**
319+
* @deprecated Use {@link captureAWSv3Client | `captureAWSv3Client()`} instead.
320+
*
318321
* Patch a specific AWS SDK v2 client and create traces when your application makes calls to that AWS service.
319322
*
320323
* 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 {
333336
* ...
334337
* }
335338
* ```
336-
* @deprecated Use {@link captureAWSv3Client} instead.
339+
*
337340
* @param service - AWS SDK v2 client
338341
*/
339342
/* v8 ignore start */ public captureAWSClient<T>(service: T): T {

packages/tracer/src/provider/ProviderService.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,18 @@ import {
4848
*/
4949
class ProviderService implements ProviderServiceInterface {
5050
/**
51-
* @deprecated
51+
* @deprecated Use {@link captureAWSv3Client} instead.
5252
*/
53-
public captureAWS<T>(awssdk: T): T {
53+
/* v8 ignore start */ public captureAWS<T>(awssdk: T): T {
5454
return captureAWS(awssdk);
55-
}
55+
} /* v8 ignore stop */
5656

5757
/**
58-
* @deprecated
58+
* @deprecated Use {@link captureAWSv3Client} instead.
5959
*/
60-
public captureAWSClient<T>(service: T): T {
60+
/* v8 ignore start */ public captureAWSClient<T>(service: T): T {
6161
return captureAWSClient(service);
62-
}
62+
} /* v8 ignore stop */
6363

6464
public captureAWSv3Client<T>(service: T): T {
6565
addUserAgentMiddleware(service, 'tracer');
@@ -119,12 +119,12 @@ class ProviderService implements ProviderServiceInterface {
119119
*/
120120
const onRequestStart = (message: unknown): void => {
121121
const { request } = message as DiagnosticsChannel.RequestCreateMessage;
122+
const method = request.method;
123+
if (method === 'CONNECT') return;
122124

123125
const parentSubsegment = this.getSegment();
124126
const requestURL = getRequestURL(request);
125127
if (parentSubsegment && requestURL) {
126-
const method = request.method;
127-
128128
const subsegment = parentSubsegment.addNewSubsegment(
129129
requestURL.hostname
130130
);

packages/tracer/tests/unit/ProviderService.test.ts

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,34 +47,6 @@ describe('Class: ProviderService', () => {
4747
vi.clearAllMocks();
4848
});
4949

50-
describe('Method: captureAWS', () => {
51-
it('calls the correct underlying function with proper arguments', () => {
52-
// Prepare
53-
const provider: ProviderService = new ProviderService();
54-
55-
// Act
56-
provider.captureAWS({});
57-
58-
// Assess
59-
expect(mocks.captureAWS).toHaveBeenCalledTimes(1);
60-
expect(mocks.captureAWS).toHaveBeenCalledWith({});
61-
});
62-
});
63-
64-
describe('Method: captureAWSClient', () => {
65-
it('calls the correct underlying function with proper arguments', () => {
66-
// Prepare
67-
const provider: ProviderService = new ProviderService();
68-
69-
// Act
70-
provider.captureAWSClient({});
71-
72-
// Assess
73-
expect(mocks.captureAWSClient).toHaveBeenCalledTimes(1);
74-
expect(mocks.captureAWSClient).toHaveBeenCalledWith({});
75-
});
76-
});
77-
7850
describe('Method: captureAWSv3Client', () => {
7951
it('calls the correct underlying function with proper arguments', () => {
8052
// Prepare
@@ -657,4 +629,22 @@ describe('Class: ProviderService', () => {
657629
)
658630
);
659631
});
632+
633+
it('skips requests with CONNECT method', () => {
634+
// Prepare
635+
const provider: ProviderService = new ProviderService();
636+
const segment = new Subsegment('## dummySegment');
637+
vi.spyOn(provider, 'getSegment').mockImplementation(() => segment);
638+
vi.spyOn(segment, 'addNewSubsegment');
639+
640+
// Act
641+
provider.instrumentFetch();
642+
mockFetch({
643+
origin: 'https://aws.amazon.com',
644+
method: 'CONNECT',
645+
});
646+
647+
// Assess
648+
expect(segment.addNewSubsegment).toHaveBeenCalledTimes(0);
649+
});
660650
});

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

Lines changed: 2 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,41 +1456,8 @@ describe('Class: Tracer', () => {
14561456
new Error('dummy error')
14571457
);
14581458
});
1459-
});
1460-
1461-
describe('Method: captureAWS', () => {
1462-
it('does nothing when called while tracing is disabled', () => {
1463-
// Prepare
1464-
const tracer: Tracer = new Tracer({ enabled: false });
1465-
const captureAWSSpy = vi
1466-
.spyOn(tracer.provider, 'captureAWS')
1467-
.mockImplementation(() => null);
14681459

1469-
// Act
1470-
tracer.captureAWS({});
1471-
1472-
// Assess
1473-
expect(captureAWSSpy).toBeCalledTimes(0);
1474-
});
1475-
1476-
it('returns the decorated object that was passed to it', () => {
1477-
// Prepare
1478-
const tracer: Tracer = new Tracer();
1479-
const captureAWSSpy = vi
1480-
.spyOn(tracer.provider, 'captureAWS')
1481-
.mockImplementation(() => null);
1482-
1483-
// Act
1484-
tracer.captureAWS({});
1485-
1486-
// Assess
1487-
expect(captureAWSSpy).toBeCalledTimes(1);
1488-
expect(captureAWSSpy).toBeCalledWith({});
1489-
});
1490-
});
1491-
1492-
describe('Method: captureAWSv3Client', () => {
1493-
it('does nothing when tracing is disabled', () => {
1460+
it('skips tracing AWS SDK clients when tracing is disabled', () => {
14941461
// Prepare
14951462
const tracer: Tracer = new Tracer({ enabled: false });
14961463
const captureAWSv3ClientSpy = vi
@@ -1504,7 +1471,7 @@ describe('Class: Tracer', () => {
15041471
expect(captureAWSv3ClientSpy).toBeCalledTimes(0);
15051472
});
15061473

1507-
it('returns the decorated object that was passed to it', () => {
1474+
it('returns the instrumented AWS SDK client when called', () => {
15081475
// Prepare
15091476
const tracer: Tracer = new Tracer();
15101477
const captureAWSv3ClientSpy = vi

0 commit comments

Comments
 (0)