Skip to content

Commit 3c48d9d

Browse files
authored
style(tracer): apply stricter linting (#4570)
1 parent 6ee70e1 commit 3c48d9d

File tree

11 files changed

+82
-96
lines changed

11 files changed

+82
-96
lines changed

packages/tracer/src/Tracer.ts

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,24 @@ import type {
4141
const { Subsegment: XraySubsegment } = xraySdk;
4242

4343
/**
44-
* ## Intro
4544
* Tracer is an opinionated thin wrapper for [AWS X-Ray SDK for Node.js](https://github.com/aws/aws-xray-sdk-node).
4645
*
4746
* Tracing data can be visualized through AWS X-Ray Console.
4847
*
49-
* ## Key features
50-
* * Auto capture cold start as annotation, and responses or full exceptions as metadata
51-
* * Auto-disable when not running in AWS Lambda environment
52-
* * Automatically trace HTTP(s) clients and generate segments for each request
53-
* * Support tracing functions via decorators, middleware, and manual instrumentation
54-
* * Support tracing AWS SDK v2 and v3 via AWS X-Ray SDK for Node.js
48+
* **Key features**
49+
* - Auto capture cold start as annotation, and responses or full exceptions as metadata
50+
* - Auto-disable when not running in AWS Lambda environment
51+
* - Automatically trace HTTP(s) clients and generate segments for each request
52+
* - Support tracing functions via decorators, middleware, and manual instrumentation
53+
* - Support tracing AWS SDK v2 and v3 via AWS X-Ray SDK for Node.js
5554
*
56-
* ## Usage
57-
*
58-
* For more usage examples, see [our documentation](https://docs.powertools.aws.dev/lambda/typescript/latest/core/tracer/).
59-
*
60-
* ### Functions usage with middleware
55+
* **Functions usage with middleware**
6156
*
6257
* If you use function-based Lambda handlers you can use the {@link Tracer.captureLambdaHandler} middy middleware to automatically:
63-
* * handle the subsegment lifecycle
64-
* * add the `ServiceName` and `ColdStart` annotations
65-
* * add the function response as metadata
66-
* * add the function error as metadata (if any)
58+
* - handle the subsegment lifecycle
59+
* - add the `ServiceName` and `ColdStart` annotations
60+
* - add the function response as metadata
61+
* - add the function error as metadata (if any)
6762
*
6863
* @example
6964
* ```typescript
@@ -80,13 +75,13 @@ const { Subsegment: XraySubsegment } = xraySdk;
8075
* export const handler = middy(lambdaHandler).use(captureLambdaHandler(tracer));
8176
* ```
8277
*
83-
* ### Object oriented usage with decorators
78+
* **Object oriented usage with decorators**
8479
*
8580
* If instead you use TypeScript Classes to wrap your Lambda handler you can use the {@link Tracer.captureLambdaHandler} decorator to automatically:
86-
* * handle the subsegment lifecycle
87-
* * add the `ServiceName` and `ColdStart` annotations
88-
* * add the function response as metadata
89-
* * add the function error as metadata (if any)
81+
* - handle the subsegment lifecycle
82+
* - add the `ServiceName` and `ColdStart` annotations
83+
* - add the function response as metadata
84+
* - add the function error as metadata (if any)
9085
*
9186
* @example
9287
* ```typescript
@@ -106,7 +101,7 @@ const { Subsegment: XraySubsegment } = xraySdk;
106101
* export const handler = handlerClass.handler.bind(handlerClass);
107102
* ```
108103
*
109-
* ### Functions usage with manual instrumentation
104+
* **Functions usage with manual instrumentation**
110105
*
111106
* If you prefer to manually instrument your Lambda handler you can use the methods in the tracer class directly.
112107
*
@@ -391,10 +386,10 @@ class Tracer extends Utility implements TracerInterface {
391386
* A decorator automating capture of metadata and annotations on segments or subsegments for a Lambda Handler.
392387
*
393388
* Using this decorator on your handler function will automatically:
394-
* * handle the subsegment lifecycle
395-
* * add the `ColdStart` annotation
396-
* * add the function response as metadata
397-
* * add the function error as metadata (if any)
389+
* - handle the subsegment lifecycle
390+
* - add the `ColdStart` annotation
391+
* - add the function response as metadata
392+
* - add the function error as metadata (if any)
398393
*
399394
* Note: Currently TypeScript only supports decorators on classes and methods. If you are using the
400395
* function syntax, you should use the middleware instead.
@@ -475,9 +470,9 @@ class Tracer extends Utility implements TracerInterface {
475470
* A decorator automating capture of metadata and annotations on segments or subsegments for an arbitrary function.
476471
*
477472
* Using this decorator on your function will automatically:
478-
* * handle the subsegment lifecycle
479-
* * add the function response as metadata
480-
* * add the function error as metadata (if any)
473+
* - handle the subsegment lifecycle
474+
* - add the function response as metadata
475+
* - add the function error as metadata (if any)
481476
*
482477
* Note: Currently TypeScript only supports decorators on classes and methods. If you are using the
483478
* function syntax, you should use the middleware instead.

packages/tracer/src/middleware/middy.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import type { CaptureLambdaHandlerOptions } from '../types/Tracer.js';
1111
* A middy middleware automating capture of metadata and annotations on segments or subsegments for a Lambda Handler.
1212
*
1313
* Using this middleware on your handler function will automatically:
14-
* * handle the subsegment lifecycle
15-
* * add the `ColdStart` annotation
16-
* * add the function response as metadata
17-
* * add the function error as metadata (if any)
14+
* - handle the subsegment lifecycle
15+
* - add the `ColdStart` annotation
16+
* - add the function response as metadata
17+
* - add the function error as metadata (if any)
1818
*
1919
* @example
2020
* ```typescript
@@ -33,7 +33,6 @@ import type { CaptureLambdaHandlerOptions } from '../types/Tracer.js';
3333
*
3434
* @param target - The Tracer instance to use for tracing
3535
* @param options - (_optional_) Options for the middleware
36-
* @returns middleware - The middy middleware object
3736
*/
3837
const captureLambdaHandler = (
3938
target: Tracer,
@@ -83,9 +82,7 @@ const captureLambdaHandler = (
8382
target.setSegment(lambdaSegment);
8483
};
8584

86-
const captureLambdaHandlerBefore = async (
87-
request: MiddyLikeRequest
88-
): Promise<void> => {
85+
const before = (request: MiddyLikeRequest) => {
8986
if (target.isTracingEnabled()) {
9087
open();
9188
setCleanupFunction(request);
@@ -94,9 +91,7 @@ const captureLambdaHandler = (
9491
}
9592
};
9693

97-
const captureLambdaHandlerAfter = async (
98-
request: MiddyLikeRequest
99-
): Promise<void> => {
94+
const after = (request: MiddyLikeRequest) => {
10095
if (target.isTracingEnabled()) {
10196
if (options?.captureResponse ?? true) {
10297
target.addResponseAsMetadata(request.response, process.env._HANDLER);
@@ -105,19 +100,17 @@ const captureLambdaHandler = (
105100
}
106101
};
107102

108-
const captureLambdaHandlerError = async (
109-
request: MiddyLikeRequest
110-
): Promise<void> => {
103+
const onError = (request: MiddyLikeRequest) => {
111104
if (target.isTracingEnabled()) {
112105
target.addErrorAsMetadata(request.error as Error);
113106
close();
114107
}
115108
};
116109

117110
return {
118-
before: captureLambdaHandlerBefore,
119-
after: captureLambdaHandlerAfter,
120-
onError: captureLambdaHandlerError,
111+
before,
112+
after,
113+
onError,
121114
};
122115
};
123116

packages/tracer/src/provider/ProviderService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import { subscribe } from 'node:diagnostics_channel';
2727
import http from 'node:http';
2828
import https from 'node:https';
2929
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';
30-
import type { DiagnosticsChannel } from 'undici-types';
3130
import { getXRayTraceIdFromEnv } from '@aws-lambda-powertools/commons/utils/env';
31+
import type { DiagnosticsChannel } from 'undici-types';
3232
import {
3333
findHeaderAndDecode,
3434
getRequestURL,
@@ -172,7 +172,7 @@ class ProviderService implements ProviderServiceInterface {
172172
response: {
173173
status,
174174
...(contentLenght && {
175-
content_length: Number.parseInt(contentLenght),
175+
content_length: Number.parseInt(contentLenght, 10),
176176
}),
177177
},
178178
};

packages/tracer/src/types/Tracer.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ type TracerOptions = {
3232
/**
3333
* Options for handler decorators and middleware.
3434
*
35-
* Options supported:
36-
* * `captureResponse` - (_optional_) - Disable response serialization as subsegment metadata
35+
* **Middleware usage**
3736
*
38-
* Middleware usage:
3937
* @example
4038
* ```typescript
4139
* import middy from '@middy/core';
@@ -48,7 +46,8 @@ type TracerOptions = {
4846
* .use(captureLambdaHandler(tracer, { captureResponse: false }));
4947
* ```
5048
*
51-
* Decorator usage:
49+
* **Decorator usage**
50+
*
5251
* @example
5352
* ```typescript
5453
* const tracer = new Tracer();
@@ -61,6 +60,8 @@ type TracerOptions = {
6160
* const handlerClass = new Lambda();
6261
* export const handler = handlerClass.handler.bind(handlerClass);
6362
* ```
63+
*
64+
* @property captureResponse - Whether to capture the Lambda handler response as subsegment metadata (default: true)
6465
*/
6566
type CaptureLambdaHandlerOptions = {
6667
captureResponse?: boolean;
@@ -69,22 +70,18 @@ type CaptureLambdaHandlerOptions = {
6970
/**
7071
* Options for method decorators.
7172
*
72-
* Options supported:
73-
* * `subSegmentName` - (_optional_) - Set a custom name for the subsegment
74-
* * `captureResponse` - (_optional_) - Disable response serialization as subsegment metadata
75-
*
7673
* Usage:
7774
* @example
7875
* ```typescript
7976
* const tracer = new Tracer();
8077
*
8178
* class Lambda implements LambdaInterface {
82-
* @tracer.captureMethod({ subSegmentName: 'gettingChargeId', captureResponse: false })
79+
* @tracer.captureMethod({ subSegmentName: 'gettingChargeId', captureResponse: false })
8380
* private getChargeId(): string {
8481
* return 'foo bar';
8582
* }
8683
*
87-
* @tracer.captureLambdaHandler({ captureResponse: false })
84+
* @tracer.captureLambdaHandler({ captureResponse: false })
8885
* public async handler(_event: any, _context: any): Promise<void> {
8986
* this.getChargeId();
9087
* }
@@ -93,6 +90,9 @@ type CaptureLambdaHandlerOptions = {
9390
* const handlerClass = new Lambda();
9491
* export const handler = handlerClass.handler.bind(handlerClass);
9592
* ```
93+
*
94+
* @property subSegmentName - (_optional_) - Set a custom name for the subsegment
95+
* @property captureResponse - (_optional_) - Disable response serialization as subsegment metadata (default: true)
9696
*/
9797
type CaptureMethodOptions = {
9898
subSegmentName?: string;

packages/tracer/tests/e2e/decorator.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ describe('Tracer E2E tests, decorator instrumentation', () => {
8888
}
8989
});
9090

91-
it('should generate all trace data correctly', async () => {
91+
it('should generate all trace data correctly', () => {
9292
// Assess
9393
const mainSubsegment = traceData[0];
9494
const { subsegments, annotations, metadata } = mainSubsegment;

packages/tracer/tests/e2e/manual.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ describe('Tracer E2E tests, manual instantiation', () => {
8585
}
8686
});
8787

88-
it('should generate all trace data correctly', async () => {
88+
it('should generate all trace data correctly', () => {
8989
// Assess
9090
const mainSubsegment = traceData[0];
9191
const { subsegments, annotations, metadata } = mainSubsegment;

packages/tracer/tests/helpers/mockRequests.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const mockFetch = ({
4949
});
5050

5151
if (throwError) {
52-
const error = new AggregateError('Mock fetch error');
52+
const error = new AggregateError([], 'Mock fetch error');
5353

5454
errorChannel.publish({
5555
request,
@@ -62,8 +62,7 @@ const mockFetch = ({
6262
const encoder = new TextEncoder();
6363
const encodedHeaders = [];
6464
for (const [key, value] of Object.entries(headers ?? {})) {
65-
encodedHeaders.push(encoder.encode(key));
66-
encodedHeaders.push(encoder.encode(value));
65+
encodedHeaders.push(encoder.encode(key), encoder.encode(value));
6766
}
6867
responseHeadersChannel.publish({
6968
request,

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ describe('Class: ProviderService', () => {
306306
});
307307

308308
describe('Method: instrumentFetch', () => {
309-
it('subscribes to the diagnostics channel', async () => {
309+
it('subscribes to the diagnostics channel', () => {
310310
// Prepare
311311
const provider: ProviderService = new ProviderService();
312312

@@ -319,7 +319,7 @@ describe('Class: ProviderService', () => {
319319
expect(channel('undici:request:error').hasSubscribers).toBe(true);
320320
});
321321

322-
it('traces a successful request', async () => {
322+
it('traces a successful request', () => {
323323
// Prepare
324324
const provider: ProviderService = new ProviderService();
325325
const segment = new Subsegment('## dummySegment');
@@ -367,7 +367,7 @@ describe('Class: ProviderService', () => {
367367
);
368368
});
369369

370-
it('excludes the content_length header when invalid or not found', async () => {
370+
it('excludes the content_length header when invalid or not found', () => {
371371
// Prepare
372372
const provider: ProviderService = new ProviderService();
373373
const segment = new Subsegment('## dummySegment');
@@ -406,7 +406,7 @@ describe('Class: ProviderService', () => {
406406
expect(provider.setSegment).toHaveBeenLastCalledWith(segment);
407407
});
408408

409-
it('adds a throttle flag to the segment when the status code is 429', async () => {
409+
it('adds a throttle flag to the segment when the status code is 429', () => {
410410
// Prepare
411411
const provider: ProviderService = new ProviderService();
412412
const segment = new Subsegment('## dummySegment');
@@ -442,7 +442,7 @@ describe('Class: ProviderService', () => {
442442
expect(provider.setSegment).toHaveBeenLastCalledWith(segment);
443443
});
444444

445-
it('adds an error flag to the segment when the status code is 4xx', async () => {
445+
it('adds an error flag to the segment when the status code is 4xx', () => {
446446
// Prepare
447447
const provider: ProviderService = new ProviderService();
448448
const segment = new Subsegment('## dummySegment');
@@ -478,7 +478,7 @@ describe('Class: ProviderService', () => {
478478
expect(provider.setSegment).toHaveBeenLastCalledWith(segment);
479479
});
480480

481-
it('adds a fault flag to the segment when the status code is 5xx', async () => {
481+
it('adds a fault flag to the segment when the status code is 5xx', () => {
482482
// Prepare
483483
const provider: ProviderService = new ProviderService();
484484
const segment = new Subsegment('## dummySegment');
@@ -514,7 +514,7 @@ describe('Class: ProviderService', () => {
514514
expect(provider.setSegment).toHaveBeenLastCalledWith(segment);
515515
});
516516

517-
it('skips the segment creation when the request has no origin', async () => {
517+
it('skips the segment creation when the request has no origin', () => {
518518
// Prepare
519519
const provider: ProviderService = new ProviderService();
520520
const segment = new Subsegment('## dummySegment');
@@ -531,7 +531,7 @@ describe('Class: ProviderService', () => {
531531
expect(provider.setSegment).toHaveBeenCalledTimes(0);
532532
});
533533

534-
it('does not add any path to the segment when the request has no path', async () => {
534+
it('does not add any path to the segment when the request has no path', () => {
535535
// Prepare
536536
const provider: ProviderService = new ProviderService();
537537
const segment = new Subsegment('## dummySegment');
@@ -564,7 +564,7 @@ describe('Class: ProviderService', () => {
564564
});
565565
});
566566

567-
it('closes the segment and adds a fault flag when the connection fails', async () => {
567+
it('closes the segment and adds a fault flag when the connection fails', () => {
568568
// Prepare
569569
const provider: ProviderService = new ProviderService();
570570
const segment = new Subsegment('## dummySegment');
@@ -595,7 +595,7 @@ describe('Class: ProviderService', () => {
595595
expect(provider.setSegment).toHaveBeenLastCalledWith(segment);
596596
});
597597

598-
it('forwards the correct sampling decision in the request header', async () => {
598+
it('forwards the correct sampling decision in the request header', () => {
599599
// Prepare
600600
const provider: ProviderService = new ProviderService();
601601
const segment = new Subsegment('## dummySegment');

0 commit comments

Comments
 (0)