Skip to content

Commit 9978252

Browse files
authored
chore(maintenance): migrate tracer utility to biome (#2809)
1 parent d239d1c commit 9978252

27 files changed

+238
-412
lines changed

packages/tracer/package.json

+4-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
2121
"build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
2222
"build": "npm run build:esm & npm run build:cjs",
23-
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
24-
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
23+
"lint": "biome lint .",
24+
"lint:fix": "biome check --write .",
2525
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
2626
},
2727
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/tracer#readme",
@@ -69,17 +69,12 @@
6969
"lib/cjs/middleware/middy.d.ts",
7070
"lib/esm/middleware/middy.d.ts"
7171
],
72-
"types": [
73-
"lib/cjs/types/index.d.ts",
74-
"lib/esm/types/index.d.ts"
75-
]
72+
"types": ["lib/cjs/types/index.d.ts", "lib/esm/types/index.d.ts"]
7673
}
7774
},
7875
"types": "./lib/cjs/index.d.ts",
7976
"main": "./lib/cjs/index.js",
80-
"files": [
81-
"lib"
82-
],
77+
"files": ["lib"],
8378
"repository": {
8479
"type": "git",
8580
"url": "git+https://github.com/aws-powertools/powertools-lambda-typescript.git"

packages/tracer/src/Tracer.ts

+18-24
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import type { Handler } from 'aws-lambda';
21
import { Utility } from '@aws-lambda-powertools/commons';
32
import type {
43
AsyncHandler,
5-
SyncHandler,
64
HandlerMethodDecorator,
5+
SyncHandler,
76
} from '@aws-lambda-powertools/commons/types';
7+
import type { Handler } from 'aws-lambda';
8+
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
9+
import xraySdk from 'aws-xray-sdk-core';
810
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js';
11+
import { ProviderService } from './provider/ProviderService.js';
912
import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js';
13+
import type { ProviderServiceInterface } from './types/ProviderService.js';
1014
import type {
11-
TracerInterface,
12-
TracerOptions,
1315
AnyClass,
14-
MethodDecorator,
1516
CaptureLambdaHandlerOptions,
1617
CaptureMethodOptions,
18+
MethodDecorator,
19+
TracerInterface,
20+
TracerOptions,
1721
} from './types/Tracer.js';
18-
import { ProviderService } from './provider/ProviderService.js';
19-
import type { ProviderServiceInterface } from './types/ProviderService.js';
20-
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
21-
import xraySdk from 'aws-xray-sdk-core';
2222
const { Subsegment: XraySubsegment } = xraySdk;
2323

2424
/**
@@ -372,22 +372,16 @@ class Tracer extends Utility implements TracerInterface {
372372
options?: CaptureLambdaHandlerOptions
373373
): HandlerMethodDecorator {
374374
return (_target, _propertyKey, descriptor) => {
375-
/**
376-
* The descriptor.value is the method this decorator decorates, it cannot be undefined.
377-
*/
378-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
375+
// biome-ignore lint/style/noNonNullAssertion: The descriptor.value is the method this decorator decorates, it cannot be undefined.
379376
const originalMethod = descriptor.value!;
380377

381378
// eslint-disable-next-line @typescript-eslint/no-this-alias
382379
const tracerRef = this;
383380
// Use a function() {} instead of an () => {} arrow function so that we can
384381
// access `myClass` as `this` in a decorated `myClass.myMethod()`.
385382
descriptor.value = function (this: Handler, event, context, callback) {
386-
// eslint-disable-next-line @typescript-eslint/no-this-alias
387-
const handlerRef: Handler = this;
388-
389383
if (!tracerRef.isTracingEnabled()) {
390-
return originalMethod.apply(handlerRef, [event, context, callback]);
384+
return originalMethod.apply(this, [event, context, callback]);
391385
}
392386

393387
return tracerRef.provider.captureAsyncFunc(
@@ -397,7 +391,7 @@ class Tracer extends Utility implements TracerInterface {
397391
tracerRef.addServiceNameAnnotation();
398392
let result: unknown;
399393
try {
400-
result = await originalMethod.apply(handlerRef, [
394+
result = await originalMethod.apply(this, [
401395
event,
402396
context,
403397
callback,
@@ -413,7 +407,7 @@ class Tracer extends Utility implements TracerInterface {
413407
subsegment?.close();
414408
} catch (error) {
415409
console.warn(
416-
`Failed to close or serialize segment %s. We are catching the error but data might be lost.`,
410+
'Failed to close or serialize segment %s. We are catching the error but data might be lost.',
417411
subsegment?.name,
418412
error
419413
);
@@ -469,8 +463,7 @@ class Tracer extends Utility implements TracerInterface {
469463
options?: CaptureMethodOptions
470464
): MethodDecorator<T> {
471465
return (_target, propertyKey, descriptor) => {
472-
// The descriptor.value is the method this decorator decorates, it cannot be undefined.
473-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
466+
// biome-ignore lint/style/noNonNullAssertion: The descriptor.value is the method this decorator decorates, it cannot be undefined.
474467
const originalMethod = descriptor.value!;
475468

476469
// eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -490,7 +483,8 @@ class Tracer extends Utility implements TracerInterface {
490483
return tracerRef.provider.captureAsyncFunc(
491484
subsegmentName,
492485
async (subsegment) => {
493-
let result;
486+
// biome-ignore lint/suspicious/noExplicitAny: we don't know the type of the result because we're decorating arbitrary functions
487+
let result: any;
494488
try {
495489
result = await originalMethod.apply(this, [...args]);
496490
if (options?.captureResponse ?? true) {
@@ -505,7 +499,7 @@ class Tracer extends Utility implements TracerInterface {
505499
subsegment?.close();
506500
} catch (error) {
507501
console.warn(
508-
`Failed to close or serialize segment %s. We are catching the error but data might be lost.`,
502+
'Failed to close or serialize segment %s. We are catching the error but data might be lost.',
509503
subsegment?.name,
510504
error
511505
);
@@ -702,7 +696,7 @@ class Tracer extends Utility implements TracerInterface {
702696
public setSegment(segment: Segment | Subsegment): void {
703697
if (!this.isTracingEnabled()) return;
704698

705-
return this.provider.setSegment(segment);
699+
this.provider.setSegment(segment);
706700
}
707701

708702
/**

packages/tracer/src/config/EnvironmentVariablesService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';
21
import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons';
2+
import type { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';
33

44
class EnvironmentVariablesService
55
extends CommonEnvironmentVariablesService

packages/tracer/src/middleware/middy.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { TRACER_KEY } from '@aws-lambda-powertools/commons';
2-
import type { Tracer } from '../Tracer.js';
3-
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
4-
import type { CaptureLambdaHandlerOptions } from '../types/Tracer.js';
52
import type {
63
MiddlewareLikeObj,
74
MiddyLikeRequest,
85
} from '@aws-lambda-powertools/commons/types';
6+
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
7+
import type { Tracer } from '../Tracer.js';
8+
import type { CaptureLambdaHandlerOptions } from '../types/Tracer.js';
99

1010
/**
1111
* A middy middleware automating capture of metadata and annotations on segments or subsegments for a Lambda Handler.
@@ -75,7 +75,7 @@ const captureLambdaHandler = (
7575
handlerSegment.close();
7676
} catch (error) {
7777
console.warn(
78-
`Failed to close or serialize segment %s. We are catching the error but data might be lost.`,
78+
'Failed to close or serialize segment %s. We are catching the error but data might be lost.',
7979
handlerSegment.name,
8080
error
8181
);

packages/tracer/src/provider/ProviderService.ts

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
import type { Logger, Segment, Subsegment } from 'aws-xray-sdk-core';
2+
import xraySdk from 'aws-xray-sdk-core';
13
import type { Namespace } from 'cls-hooked';
24
import type {
3-
ProviderServiceInterface,
45
ContextMissingStrategy,
56
HttpSubsegment,
7+
ProviderServiceInterface,
68
} from '../types/ProviderService.js';
7-
import type { Segment, Subsegment, Logger } from 'aws-xray-sdk-core';
8-
import xraySdk from 'aws-xray-sdk-core';
99
const {
1010
captureAWS,
1111
captureAWSClient,
@@ -21,16 +21,16 @@ const {
2121
setDaemonAddress,
2222
setLogger,
2323
} = xraySdk;
24-
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';
2524
import { subscribe } from 'node:diagnostics_channel';
25+
import http from 'node:http';
26+
import https from 'node:https';
27+
import { addUserAgentMiddleware } from '@aws-lambda-powertools/commons';
28+
import type { DiagnosticsChannel } from 'undici-types';
2629
import {
2730
findHeaderAndDecode,
2831
getOriginURL,
2932
isHttpSubsegment,
3033
} from './utilities.js';
31-
import type { DiagnosticsChannel } from 'undici-types';
32-
import http from 'node:http';
33-
import https from 'node:https';
3434

3535
class ProviderService implements ProviderServiceInterface {
3636
/**
@@ -50,8 +50,7 @@ class ProviderService implements ProviderServiceInterface {
5050
public captureAWSv3Client<T>(service: T): T {
5151
addUserAgentMiddleware(service, 'tracer');
5252

53-
// Type must be aliased as any because of this https://github.com/aws/aws-xray-sdk-node/issues/439#issuecomment-859715660
54-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
53+
// biome-ignore lint/suspicious/noExplicitAny: Type must be aliased as any because of this https://github.com/aws/aws-xray-sdk-node/issues/439#issuecomment-859715660
5554
return captureAWSv3Client(service as any);
5655
}
5756

@@ -150,7 +149,7 @@ class ProviderService implements ProviderServiceInterface {
150149
response: {
151150
status,
152151
...(contentLenght && {
153-
content_length: parseInt(contentLenght),
152+
content_length: Number.parseInt(contentLenght),
154153
}),
155154
},
156155
};

packages/tracer/src/provider/utilities.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { HttpSubsegment } from '../types/ProviderService.js';
2-
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
31
import { URL } from 'node:url';
2+
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
3+
import type { HttpSubsegment } from '../types/ProviderService.js';
44

55
const decoder = new TextDecoder();
66

packages/tracer/src/types/ProviderService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { Namespace } from 'cls-hooked';
21
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
2+
import type { Namespace } from 'cls-hooked';
33

44
type ContextMissingStrategy =
55
| 'LOG_ERROR'

packages/tracer/src/types/Tracer.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { ConfigServiceInterface } from './ConfigServiceInterface.js';
21
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
32
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
3+
import type { ConfigServiceInterface } from './ConfigServiceInterface.js';
44

55
/**
66
* Options for the tracer class to be used during initialization.
@@ -99,9 +99,9 @@ type CaptureMethodOptions = {
9999
captureResponse?: boolean;
100100
};
101101

102-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
102+
// biome-ignore lint/suspicious/noExplicitAny: this is a generic type that is intentionally open
103103
type AnyClassMethod = (...args: any[]) => any;
104-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
104+
// biome-ignore lint/suspicious/noExplicitAny: this is a generic type that is intentionally open
105105
type AnyClass = new (...args: any[]) => any;
106106

107107
type MethodDecorator<T extends AnyClass> = (
@@ -115,9 +115,9 @@ interface TracerInterface {
115115
addResponseAsMetadata(data?: unknown, methodName?: string): void;
116116
addServiceNameAnnotation(): void;
117117
annotateColdStart(): void;
118-
captureAWS<T>(aws: T): void | T;
119-
captureAWSv3Client<T>(service: T): void | T;
120-
captureAWSClient<T>(service: T): void | T;
118+
captureAWS<T>(aws: T): undefined | T;
119+
captureAWSv3Client<T>(service: T): undefined | T;
120+
captureAWSClient<T>(service: T): undefined | T;
121121
captureLambdaHandler(
122122
options?: CaptureLambdaHandlerOptions
123123
): HandlerMethodDecorator;

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Tracer } from '../../src/Tracer.js';
21
import type { Callback, Context } from 'aws-lambda';
32
import AWS from 'aws-sdk';
3+
import { Tracer } from '../../src/Tracer.js';
44
import { httpRequest } from '../helpers/httpRequest.js';
55

66
const serviceName =
@@ -41,7 +41,7 @@ export class MyFunctionBase {
4141
event: CustomEvent,
4242
_context: Context,
4343
_callback: Callback<unknown>
44-
): void | Promise<unknown> {
44+
): unknown {
4545
tracer.putAnnotation(customAnnotationKey, customAnnotationValue);
4646
tracer.putMetadata(customMetadataKey, customMetadataValue);
4747

@@ -84,7 +84,7 @@ class MyFunctionWithDecorator extends MyFunctionBase {
8484
event: CustomEvent,
8585
_context: Context,
8686
_callback: Callback<unknown>
87-
): void | Promise<unknown> {
87+
): unknown {
8888
return super.handler(event, _context, _callback);
8989
}
9090

@@ -103,7 +103,7 @@ class MyFunctionWithDecoratorCaptureResponseFalse extends MyFunctionBase {
103103
event: CustomEvent,
104104
_context: Context,
105105
_callback: Callback<unknown>
106-
): void | Promise<unknown> {
106+
): unknown {
107107
return super.handler(event, _context, _callback);
108108
}
109109

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*
44
* @group e2e/tracer/decorator
55
*/
6+
import { join } from 'node:path';
67
import { TestStack } from '@aws-lambda-powertools/testing-utils';
78
import { TestDynamodbTable } from '@aws-lambda-powertools/testing-utils/resources/dynamodb';
8-
import { join } from 'node:path';
99
import { TracerTestNodejsFunction } from '../helpers/resources.js';
1010
import {
1111
assertAnnotation,
@@ -19,11 +19,11 @@ import {
1919
splitSegmentsByName,
2020
} from '../helpers/tracesUtils.js';
2121
import {
22-
commonEnvironmentVars,
2322
RESOURCE_NAME_PREFIX,
2423
SETUP_TIMEOUT,
2524
TEARDOWN_TIMEOUT,
2625
TEST_CASE_TIMEOUT,
26+
commonEnvironmentVars,
2727
} from './constants.js';
2828

2929
/**
@@ -35,7 +35,7 @@ import {
3535
* Each stack must use a unique `serviceName` as it's used to for retrieving the trace.
3636
* Using the same one will result in traces from different test cases mixing up.
3737
*/
38-
describe(`Tracer E2E tests, all features with decorator instantiation`, () => {
38+
describe('Tracer E2E tests, all features with decorator instantiation', () => {
3939
const testStack = new TestStack({
4040
stackNameProps: {
4141
stackNamePrefix: RESOURCE_NAME_PREFIX,
@@ -265,14 +265,14 @@ describe(`Tracer E2E tests, all features with decorator instantiation`, () => {
265265
if (!metadata) {
266266
fail('metadata is missing');
267267
}
268-
expect(metadata['AllFlagsOn'][expectedCustomMetadataKey]).toEqual(
268+
expect(metadata.AllFlagsOn[expectedCustomMetadataKey]).toEqual(
269269
expectedCustomMetadataValue
270270
);
271271

272272
const shouldThrowAnError = i === invocationCount - 1;
273273
if (!shouldThrowAnError) {
274274
// Assert that the metadata object contains the response
275-
expect(metadata['AllFlagsOn']['index.handler response']).toEqual(
275+
expect(metadata.AllFlagsOn['index.handler response']).toEqual(
276276
expectedCustomResponseValue
277277
);
278278
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Tracer } from '../../src/index.js';
21
import type { Context } from 'aws-lambda';
32
import AWS from 'aws-sdk';
43
import type { Subsegment } from 'aws-xray-sdk-core';
4+
import { Tracer } from '../../src/index.js';
55
import { httpRequest } from '../helpers/httpRequest.js';
66

77
const serviceName =

0 commit comments

Comments
 (0)