Skip to content

Commit 231c39e

Browse files
daschaadreamorosi
andauthored
chore(maintenance): migrate metrics utility to biome (#2816)
Co-authored-by: Andrea Amorosi <[email protected]>
1 parent 8000d99 commit 231c39e

18 files changed

+125
-132
lines changed

packages/metrics/package.json

+5-17
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"build:cjs": "tsc --build tsconfig.json && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
2020
"build:esm": "tsc --build tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
2121
"build": "npm run build:esm & npm run build:cjs",
22-
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
23-
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
22+
"lint": "biome lint .",
23+
"lint:fix": "biome check --write .",
2424
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
2525
},
2626
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/metrics#readme",
@@ -52,10 +52,7 @@
5252
"lib/cjs/middleware/middy.d.ts",
5353
"lib/esm/middleware/middy.d.ts"
5454
],
55-
"types": [
56-
"lib/cjs/types/index.d.ts",
57-
"lib/esm/types/index.d.ts"
58-
]
55+
"types": ["lib/cjs/types/index.d.ts", "lib/esm/types/index.d.ts"]
5956
}
6057
},
6158
"types": "./lib/cjs/index.d.ts",
@@ -74,9 +71,7 @@
7471
"optional": true
7572
}
7673
},
77-
"files": [
78-
"lib"
79-
],
74+
"files": ["lib"],
8075
"repository": {
8176
"type": "git",
8277
"url": "git+https://github.com/aws-powertools/powertools-lambda-typescript.git"
@@ -87,12 +82,5 @@
8782
"dependencies": {
8883
"@aws-lambda-powertools/commons": "^2.5.0"
8984
},
90-
"keywords": [
91-
"aws",
92-
"lambda",
93-
"powertools",
94-
"metrics",
95-
"serverless",
96-
"nodejs"
97-
]
85+
"keywords": ["aws", "lambda", "powertools", "metrics", "serverless", "nodejs"]
9886
}

packages/metrics/src/Metrics.ts

+19-25
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
import type { Callback, Context, Handler } from 'aws-lambda';
21
import { Console } from 'node:console';
32
import { Utility } from '@aws-lambda-powertools/commons';
43
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
4+
import type { Callback, Context, Handler } from 'aws-lambda';
55
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js';
66
import {
7+
COLD_START_METRIC,
8+
DEFAULT_NAMESPACE,
79
MAX_DIMENSION_COUNT,
810
MAX_METRICS_SIZE,
9-
DEFAULT_NAMESPACE,
10-
COLD_START_METRIC,
1111
MAX_METRIC_VALUES_SIZE,
12-
MetricUnit as MetricUnits,
1312
MetricResolution as MetricResolutions,
13+
MetricUnit as MetricUnits,
1414
} from './constants.js';
15-
import {
16-
type MetricsOptions,
17-
type Dimensions,
18-
type EmfOutput,
19-
type StoredMetrics,
20-
type ExtraOptions,
21-
type MetricDefinition,
22-
type ConfigServiceInterface,
23-
type MetricsInterface,
24-
type MetricUnit,
25-
type MetricResolution,
15+
import type {
16+
ConfigServiceInterface,
17+
Dimensions,
18+
EmfOutput,
19+
ExtraOptions,
20+
MetricDefinition,
21+
MetricResolution,
22+
MetricUnit,
23+
MetricsInterface,
24+
MetricsOptions,
25+
StoredMetrics,
2626
} from './types/index.js';
2727

2828
/**
@@ -167,9 +167,9 @@ class Metrics extends Utility implements MetricsInterface {
167167
*/
168168
public addDimensions(dimensions: { [key: string]: string }): void {
169169
const newDimensions = { ...this.dimensions };
170-
Object.keys(dimensions).forEach((dimensionName) => {
170+
for (const dimensionName of Object.keys(dimensions)) {
171171
newDimensions[dimensionName] = dimensions[dimensionName];
172-
});
172+
}
173173
if (Object.keys(newDimensions).length > MAX_DIMENSION_COUNT) {
174174
throw new RangeError(
175175
`Unable to add ${
@@ -337,10 +337,7 @@ class Metrics extends Utility implements MetricsInterface {
337337
}
338338

339339
return (_target, _propertyKey, descriptor) => {
340-
/**
341-
* The descriptor.value is the method this decorator decorates, it cannot be undefined.
342-
*/
343-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
340+
// biome-ignore lint/style/noNonNullAssertion: The descriptor.value is the method this decorator decorates, it cannot be undefined.
344341
const originalMethod = descriptor.value!;
345342

346343
// eslint-disable-next-line @typescript-eslint/no-this-alias
@@ -359,8 +356,6 @@ class Metrics extends Utility implements MetricsInterface {
359356
let result: unknown;
360357
try {
361358
result = await originalMethod.apply(this, [event, context, callback]);
362-
} catch (error) {
363-
throw error;
364359
} finally {
365360
metricsRef.publishStoredMetrics();
366361
}
@@ -598,9 +593,8 @@ class Metrics extends Utility implements MetricsInterface {
598593
}
599594

600595
return false;
601-
} else {
602-
return true;
603596
}
597+
return true;
604598
}
605599

606600
/**

packages/metrics/src/config/EnvironmentVariablesService.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { 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/metrics/src/middleware/middy.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { METRICS_KEY } from '@aws-lambda-powertools/commons';
2-
import type { Metrics } from '../Metrics.js';
3-
import type { ExtraOptions } from '../types/Metrics.js';
42
import type {
53
MiddlewareLikeObj,
64
MiddyLikeRequest,
75
} from '@aws-lambda-powertools/commons/types';
6+
import type { Metrics } from '../Metrics.js';
7+
import type { ExtraOptions } from '../types/Metrics.js';
88

99
/**
1010
* A middy middleware automating capture of metadata and annotations on segments or subsegments for a Lambda Handler.
@@ -38,7 +38,7 @@ const logMetrics = (
3838
target: Metrics | Metrics[],
3939
options: ExtraOptions = {}
4040
): MiddlewareLikeObj => {
41-
const metricsInstances = target instanceof Array ? target : [target];
41+
const metricsInstances = Array.isArray(target) ? target : [target];
4242

4343
/**
4444
* Set the cleanup function to be called in case other middlewares return early.
@@ -53,7 +53,7 @@ const logMetrics = (
5353
};
5454

5555
const logMetricsBefore = async (request: MiddyLikeRequest): Promise<void> => {
56-
metricsInstances.forEach((metrics: Metrics) => {
56+
for (const metrics of metricsInstances) {
5757
metrics.setFunctionName(request.context.functionName);
5858
const { throwOnEmptyMetrics, defaultDimensions, captureColdStartMetric } =
5959
options;
@@ -66,15 +66,15 @@ const logMetrics = (
6666
if (captureColdStartMetric) {
6767
metrics.captureColdStartMetric();
6868
}
69-
});
69+
}
7070

7171
setCleanupFunction(request);
7272
};
7373

7474
const logMetricsAfterOrError = async (): Promise<void> => {
75-
metricsInstances.forEach((metrics: Metrics) => {
75+
for (const metrics of metricsInstances) {
7676
metrics.publishStoredMetrics();
77-
});
77+
}
7878
};
7979

8080
return {

packages/metrics/src/types/Metrics.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import type {
2+
MetricResolution as MetricResolutionList,
3+
MetricUnit as MetricUnitList,
4+
} from '../constants.js';
15
import type { ConfigServiceInterface } from './ConfigServiceInterface.js';
2-
import { MetricResolution, MetricUnit } from '../constants.js';
36

47
type Dimensions = Record<string, string>;
58

@@ -49,9 +52,9 @@ type ExtraOptions = {
4952
};
5053

5154
type MetricResolution =
52-
(typeof MetricResolution)[keyof typeof MetricResolution];
55+
(typeof MetricResolutionList)[keyof typeof MetricResolutionList];
5356

54-
type MetricUnit = (typeof MetricUnit)[keyof typeof MetricUnit];
57+
type MetricUnit = (typeof MetricUnitList)[keyof typeof MetricUnitList];
5558

5659
type StoredMetric = {
5760
name: string;

packages/metrics/src/types/MetricsInterface.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type { Metrics } from '../Metrics.js';
21
import type { HandlerMethodDecorator } from '@aws-lambda-powertools/commons/types';
2+
import type { Metrics } from '../Metrics.js';
33
import type {
4-
EmfOutput,
54
Dimensions,
6-
MetricsOptions,
5+
EmfOutput,
76
MetricResolution,
87
MetricUnit,
8+
MetricsOptions,
99
} from './Metrics.js';
1010

1111
interface MetricsInterface {

packages/metrics/tests/e2e/basicFeatures.decorator.test.functionCode.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { Metrics, MetricUnit } from '../../src/index.js';
2-
import type { MetricUnit as MetricUnitType } from '../../src/types/index.js';
3-
import type { Context } from 'aws-lambda';
41
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
2+
import type { Context } from 'aws-lambda';
3+
import { MetricUnit, Metrics } from '../../src/index.js';
4+
import type { MetricUnit as MetricUnitType } from '../../src/types/index.js';
55

66
const namespace = process.env.EXPECTED_NAMESPACE ?? 'CdkExample';
77
const serviceName =
@@ -35,7 +35,7 @@ class Lambda implements LambdaInterface {
3535
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
3636
// @ts-ignore
3737
public async handler(_event: unknown, _context: Context): Promise<void> {
38-
metrics.addMetric(metricName, metricUnit, parseInt(metricValue));
38+
metrics.addMetric(metricName, metricUnit, Number.parseInt(metricValue));
3939
metrics.addDimension(
4040
Object.entries(JSON.parse(extraDimension))[0][0],
4141
Object.entries(JSON.parse(extraDimension))[0][1] as string
@@ -54,7 +54,7 @@ class Lambda implements LambdaInterface {
5454
metricWithItsOwnDimensions.addMetric(
5555
singleMetricName,
5656
singleMetricUnit,
57-
parseInt(singleMetricValue)
57+
Number.parseInt(singleMetricValue)
5858
);
5959
}
6060
}

packages/metrics/tests/e2e/basicFeatures.decorators.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
*
44
* @group e2e/metrics/decorator
55
*/
6+
import { join } from 'node:path';
67
import {
7-
invokeFunction,
88
TestStack,
9+
invokeFunction,
910
} from '@aws-lambda-powertools/testing-utils';
1011
import {
1112
CloudWatchClient,
1213
GetMetricStatisticsCommand,
1314
} from '@aws-sdk/client-cloudwatch';
14-
import { join } from 'node:path';
1515
import { getMetrics, sortDimensions } from '../helpers/metricsUtils.js';
1616
import { MetricsTestNodejsFunction } from '../helpers/resources.js';
1717
import {
18-
commonEnvironmentVars,
1918
ONE_MINUTE,
2019
RESOURCE_NAME_PREFIX,
2120
SETUP_TIMEOUT,
2221
TEARDOWN_TIMEOUT,
2322
TEST_CASE_TIMEOUT,
23+
commonEnvironmentVars,
2424
} from './constants.js';
2525

26-
describe(`Metrics E2E tests, basic features decorator usage`, () => {
26+
describe('Metrics E2E tests, basic features decorator usage', () => {
2727
const testStack = new TestStack({
2828
stackNameProps: {
2929
stackNamePrefix: RESOURCE_NAME_PREFIX,
@@ -205,7 +205,7 @@ describe(`Metrics E2E tests, basic features decorator usage`, () => {
205205
? metricStat.Datapoints[0]
206206
: {};
207207
expect(singleDataPoint?.Sum).toBeGreaterThanOrEqual(
208-
parseInt(expectedMetricValue) * invocations
208+
Number.parseInt(expectedMetricValue) * invocations
209209
);
210210
},
211211
TEST_CASE_TIMEOUT

packages/metrics/tests/e2e/basicFeatures.manual.test.functionCode.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Metrics, MetricUnit } from '../../src/index.js';
2-
import type { MetricUnit as MetricUnitType } from '../../src/types/index.js';
31
import type { Context } from 'aws-lambda';
2+
import { MetricUnit, Metrics } from '../../src/index.js';
3+
import type { MetricUnit as MetricUnitType } from '../../src/types/index.js';
44

55
const namespace = process.env.EXPECTED_NAMESPACE ?? 'CdkExample';
66
const serviceName =
@@ -32,7 +32,7 @@ export const handler = async (
3232
metrics.captureColdStartMetric();
3333
metrics.throwOnEmptyMetrics();
3434
metrics.setDefaultDimensions(JSON.parse(defaultDimensions));
35-
metrics.addMetric(metricName, metricUnit, parseInt(metricValue));
35+
metrics.addMetric(metricName, metricUnit, Number.parseInt(metricValue));
3636
metrics.addDimension(
3737
Object.entries(JSON.parse(extraDimension))[0][0],
3838
Object.entries(JSON.parse(extraDimension))[0][1] as string
@@ -46,7 +46,7 @@ export const handler = async (
4646
metricWithItsOwnDimensions.addMetric(
4747
singleMetricName,
4848
singleMetricUnit,
49-
parseInt(singleMetricValue)
49+
Number.parseInt(singleMetricValue)
5050
);
5151

5252
metrics.publishStoredMetrics();

packages/metrics/tests/e2e/basicFeatures.manual.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@
33
*
44
* @group e2e/metrics/standardFunctions
55
*/
6+
import { join } from 'node:path';
67
import {
7-
invokeFunction,
88
TestStack,
9+
invokeFunction,
910
} from '@aws-lambda-powertools/testing-utils';
1011
import {
1112
CloudWatchClient,
1213
GetMetricStatisticsCommand,
1314
} from '@aws-sdk/client-cloudwatch';
14-
import { join } from 'node:path';
1515
import { getMetrics, sortDimensions } from '../helpers/metricsUtils.js';
1616
import { MetricsTestNodejsFunction } from '../helpers/resources.js';
1717
import {
18-
commonEnvironmentVars,
1918
ONE_MINUTE,
2019
RESOURCE_NAME_PREFIX,
2120
SETUP_TIMEOUT,
2221
TEARDOWN_TIMEOUT,
2322
TEST_CASE_TIMEOUT,
23+
commonEnvironmentVars,
2424
} from './constants.js';
2525

26-
describe(`Metrics E2E tests, manual usage`, () => {
26+
describe('Metrics E2E tests, manual usage', () => {
2727
const testStack = new TestStack({
2828
stackNameProps: {
2929
stackNamePrefix: RESOURCE_NAME_PREFIX,
@@ -191,7 +191,7 @@ describe(`Metrics E2E tests, manual usage`, () => {
191191
? metricStat.Datapoints[0]
192192
: {};
193193
expect(singleDataPoint.Sum).toBeGreaterThanOrEqual(
194-
parseInt(expectedMetricValue) * invocations
194+
Number.parseInt(expectedMetricValue) * invocations
195195
);
196196
},
197197
TEST_CASE_TIMEOUT

packages/metrics/tests/helpers/metricsUtils.ts

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
import promiseRetry from 'promise-retry';
2-
import { Metrics, MetricUnit } from '../../src/index.js';
3-
import { ExtraOptions } from '../../src/types/index.js';
1+
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
42
import {
5-
CloudWatchClient,
3+
type CloudWatchClient,
4+
type Dimension,
65
ListMetricsCommand,
7-
} from '@aws-sdk/client-cloudwatch';
8-
import type {
9-
Dimension,
10-
ListMetricsCommandOutput,
6+
type ListMetricsCommandOutput,
117
} from '@aws-sdk/client-cloudwatch';
128
import type { Context, Handler } from 'aws-lambda';
13-
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
9+
import promiseRetry from 'promise-retry';
10+
import { MetricUnit, type Metrics } from '../../src/index.js';
11+
import type { ExtraOptions } from '../../src/types/index.js';
1412

1513
const getMetrics = async (
1614
cloudWatchClient: CloudWatchClient,

0 commit comments

Comments
 (0)