Skip to content

Commit 53fd206

Browse files
authored
style(metrics): apply stricter linting (#4571)
1 parent 3c48d9d commit 53fd206

File tree

8 files changed

+31
-25
lines changed

8 files changed

+31
-25
lines changed

packages/metrics/src/Metrics.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ import type {
4949
* These metrics can be visualized through Amazon CloudWatch Console.
5050
*
5151
* **Key features**
52-
* * Aggregating up to 100 metrics using a single CloudWatch EMF object (large JSON blob).
53-
* * Validating your metrics against common metric definitions mistakes (for example, metric unit, values, max dimensions, max metrics).
54-
* * Metrics are created asynchronously by the CloudWatch service. You do not need any custom stacks, and there is no impact to Lambda function latency.
55-
* * Creating a one-off metric with different dimensions.
52+
* - Aggregating up to 100 metrics using a single CloudWatch EMF object (large JSON blob).
53+
* - Validating your metrics against common metric definitions mistakes (for example, metric unit, values, max dimensions, max metrics).
54+
* - Metrics are created asynchronously by the CloudWatch service. You do not need any custom stacks, and there is no impact to Lambda function latency.
55+
* - Creating a one-off metric with different dimensions.
5656
*
5757
* After initializing the Metrics class, you can add metrics using the {@link Metrics.addMetric | `addMetric()`} method.
5858
* The metrics are stored in a buffer and are flushed when calling {@link Metrics.publishStoredMetrics | `publishStoredMetrics()`}.
@@ -1164,7 +1164,7 @@ class Metrics extends Utility implements MetricsInterface {
11641164
* If this point is reached, it indicates timestamp was neither a valid number nor Date
11651165
* Returning zero represents the initial date of epoch time,
11661166
* which will be skipped by Amazon CloudWatch.
1167-
**/
1167+
*/
11681168
return 0;
11691169
}
11701170

packages/metrics/src/middleware/middy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const logMetrics = (
6060
};
6161
};
6262

63-
const logMetricsBefore = async (request: MiddyLikeRequest): Promise<void> => {
63+
const logMetricsBefore = (request: MiddyLikeRequest) => {
6464
for (const metrics of metricsInstances) {
6565
const { throwOnEmptyMetrics, defaultDimensions, captureColdStartMetric } =
6666
options;
@@ -78,7 +78,7 @@ const logMetrics = (
7878
setCleanupFunction(request);
7979
};
8080

81-
const logMetricsAfterOrError = async (): Promise<void> => {
81+
const logMetricsAfterOrError = () => {
8282
for (const metrics of metricsInstances) {
8383
metrics.publishStoredMetrics();
8484
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ class Lambda implements LambdaInterface {
3232
defaultDimensions: JSON.parse(defaultDimensions),
3333
throwOnEmptyMetrics: true,
3434
})
35-
public async handler(_event: unknown, _context: Context): Promise<void> {
36-
metrics.addMetric(metricName, metricUnit, Number.parseInt(metricValue));
35+
public handler(_event: unknown, _context: Context) {
36+
metrics.addMetric(metricName, metricUnit, Number.parseInt(metricValue, 10));
3737
metrics.addDimension(
3838
Object.entries(JSON.parse(extraDimension))[0][0],
3939
Object.entries(JSON.parse(extraDimension))[0][1] as string
@@ -52,7 +52,7 @@ class Lambda implements LambdaInterface {
5252
metricWithItsOwnDimensions.addMetric(
5353
singleMetricName,
5454
singleMetricUnit,
55-
Number.parseInt(singleMetricValue)
55+
Number.parseInt(singleMetricValue, 10)
5656
);
5757
}
5858
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ describe('Metrics E2E tests, basic features decorator usage', () => {
168168
? metricStat.Datapoints[0]
169169
: {};
170170
expect(singleDataPoint?.Sum).toBeGreaterThanOrEqual(
171-
Number.parseInt(expectedMetricValue) * invocations
171+
Number.parseInt(expectedMetricValue, 10) * invocations
172172
);
173173
});
174174
});

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,11 @@ const singleMetricValue = process.env.EXPECTED_SINGLE_METRIC_VALUE ?? '2';
2525

2626
const metrics = new Metrics({ namespace: namespace, serviceName: serviceName });
2727

28-
export const handler = async (
29-
_event: unknown,
30-
_context: Context
31-
): Promise<void> => {
28+
export const handler = (_event: unknown, _context: Context) => {
3229
metrics.captureColdStartMetric();
3330
metrics.throwOnEmptyMetrics();
3431
metrics.setDefaultDimensions(JSON.parse(defaultDimensions));
35-
metrics.addMetric(metricName, metricUnit, Number.parseInt(metricValue));
32+
metrics.addMetric(metricName, metricUnit, Number.parseInt(metricValue, 10));
3633
metrics.addDimension(
3734
Object.entries(JSON.parse(extraDimension))[0][0],
3835
Object.entries(JSON.parse(extraDimension))[0][1] as string
@@ -46,7 +43,7 @@ export const handler = async (
4643
metricWithItsOwnDimensions.addMetric(
4744
singleMetricName,
4845
singleMetricUnit,
49-
Number.parseInt(singleMetricValue)
46+
Number.parseInt(singleMetricValue, 10)
5047
);
5148

5249
metrics.publishStoredMetrics();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe('Metrics E2E tests, manual usage', () => {
154154
? metricStat.Datapoints[0]
155155
: {};
156156
expect(singleDataPoint.Sum).toBeGreaterThanOrEqual(
157-
Number.parseInt(expectedMetricValue) * invocations
157+
Number.parseInt(expectedMetricValue, 10) * invocations
158158
);
159159
});
160160
});

packages/metrics/tests/helpers/metricsUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
} from '@aws-sdk/client-cloudwatch';
77
import promiseRetry from 'promise-retry';
88

9-
const getMetrics = async (
9+
const getMetrics = (
1010
cloudWatchClient: CloudWatchClient,
1111
namespace: string,
1212
metric: string,

packages/metrics/tests/unit/logMetrics.test.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { setTimeout } from 'node:timers/promises';
12
import { cleanupMiddlewares } from '@aws-lambda-powertools/commons';
23
import middy from '@middy/core';
34
import type { Context } from 'aws-lambda';
@@ -39,6 +40,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
3940

4041
@metrics.logMetrics({ captureColdStartMetric: true })
4142
async handler(_event: unknown, _context: Context) {
43+
await setTimeout(0); // Simulate some async operation
4244
this.addGreetingMetric();
4345
}
4446

@@ -105,6 +107,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
105107

106108
@metrics.logMetrics({ captureColdStartMetric: true })
107109
async handler(_event: unknown, _context: Context) {
110+
await setTimeout(0); // Simulate some async operation
108111
this.addGreetingMetric();
109112
}
110113

@@ -150,6 +153,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
150153

151154
@metrics.logMetrics({ captureColdStartMetric: true })
152155
async handler(_event: unknown, _context: Context) {
156+
await setTimeout(0); // Simulate some async operation
153157
this.addGreetingMetric();
154158
}
155159

@@ -184,6 +188,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
184188
});
185189
vi.spyOn(metrics, 'publishStoredMetrics');
186190
const handler = middy(async () => {
191+
await setTimeout(0); // Simulate some async operation
187192
metrics.addMetric('greetings', MetricUnit.Count, 1);
188193
}).use(logMetrics(metrics, { captureColdStartMetric: true }));
189194

@@ -210,7 +215,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
210215
});
211216

212217
vi.spyOn(metrics, 'publishStoredMetrics');
213-
const handler = middy(async () => {
218+
const handler = middy(() => {
214219
metrics.addMetric('greetings', MetricUnit.Count, 1);
215220
}).use(logMetrics(metrics, { captureColdStartMetric: true }));
216221

@@ -238,7 +243,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
238243
});
239244

240245
vi.spyOn(metrics, 'publishStoredMetrics');
241-
const handler = middy(async () => {
246+
const handler = middy(() => {
242247
metrics.addMetric('greetings', MetricUnit.Count, 1);
243248
}).use(logMetrics(metrics, { captureColdStartMetric: true }));
244249

@@ -266,6 +271,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
266271
class Test {
267272
@metrics.logMetrics({ defaultDimensions: { environment: 'test' } })
268273
async handler(_event: unknown, _context: Context) {
274+
await setTimeout(0); // Simulate some async operation
269275
metrics.addMetric('test', MetricUnit.Count, 1);
270276
}
271277
}
@@ -295,6 +301,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
295301
});
296302
vi.spyOn(metrics, 'publishStoredMetrics');
297303
const handler = middy(async () => {
304+
await setTimeout(0); // Simulate some async operation
298305
metrics.addMetric('greetings', MetricUnit.Count, 1);
299306
}).use(
300307
logMetrics(metrics, {
@@ -328,6 +335,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
328335
class Test {
329336
@metrics.logMetrics()
330337
async handler(_event: unknown, _context: Context) {
338+
await setTimeout(0); // Simulate some async operation
331339
throw new Error('Something went wrong');
332340
}
333341
}
@@ -349,6 +357,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
349357
class Test {
350358
@metrics.logMetrics({ throwOnEmptyMetrics: true })
351359
async handler(_event: unknown, _context: Context) {
360+
await setTimeout(0); // Simulate some async operation
352361
return 'Hello, world!';
353362
}
354363
}
@@ -367,12 +376,12 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
367376
singleMetric: false,
368377
namespace: DEFAULT_NAMESPACE,
369378
});
370-
const handler = middy(async () => {}).use(
371-
logMetrics([metrics], { throwOnEmptyMetrics: true })
372-
);
379+
const handler = middy(async () => {
380+
await setTimeout(0); // Simulate some async operation
381+
}).use(logMetrics([metrics], { throwOnEmptyMetrics: true }));
373382

374383
// Act & Assess
375-
expect(() => handler({}, {} as Context)).rejects.toThrowError(
384+
await expect(() => handler({}, {} as Context)).rejects.toThrowError(
376385
'The number of metrics recorded must be higher than zero'
377386
);
378387
});

0 commit comments

Comments
 (0)