Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/metrics/src/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ import type {
* These metrics can be visualized through Amazon CloudWatch Console.
*
* **Key features**
* * Aggregating up to 100 metrics using a single CloudWatch EMF object (large JSON blob).
* * Validating your metrics against common metric definitions mistakes (for example, metric unit, values, max dimensions, max metrics).
* * Metrics are created asynchronously by the CloudWatch service. You do not need any custom stacks, and there is no impact to Lambda function latency.
* * Creating a one-off metric with different dimensions.
* - Aggregating up to 100 metrics using a single CloudWatch EMF object (large JSON blob).
* - Validating your metrics against common metric definitions mistakes (for example, metric unit, values, max dimensions, max metrics).
* - Metrics are created asynchronously by the CloudWatch service. You do not need any custom stacks, and there is no impact to Lambda function latency.
* - Creating a one-off metric with different dimensions.
*
* After initializing the Metrics class, you can add metrics using the {@link Metrics.addMetric | `addMetric()`} method.
* The metrics are stored in a buffer and are flushed when calling {@link Metrics.publishStoredMetrics | `publishStoredMetrics()`}.
Expand Down Expand Up @@ -1164,7 +1164,7 @@ class Metrics extends Utility implements MetricsInterface {
* If this point is reached, it indicates timestamp was neither a valid number nor Date
* Returning zero represents the initial date of epoch time,
* which will be skipped by Amazon CloudWatch.
**/
*/
return 0;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/metrics/src/middleware/middy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const logMetrics = (
};
};

const logMetricsBefore = async (request: MiddyLikeRequest): Promise<void> => {
const logMetricsBefore = (request: MiddyLikeRequest) => {
for (const metrics of metricsInstances) {
const { throwOnEmptyMetrics, defaultDimensions, captureColdStartMetric } =
options;
Expand All @@ -78,7 +78,7 @@ const logMetrics = (
setCleanupFunction(request);
};

const logMetricsAfterOrError = async (): Promise<void> => {
const logMetricsAfterOrError = () => {
for (const metrics of metricsInstances) {
metrics.publishStoredMetrics();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ class Lambda implements LambdaInterface {
defaultDimensions: JSON.parse(defaultDimensions),
throwOnEmptyMetrics: true,
})
public async handler(_event: unknown, _context: Context): Promise<void> {
metrics.addMetric(metricName, metricUnit, Number.parseInt(metricValue));
public handler(_event: unknown, _context: Context) {
metrics.addMetric(metricName, metricUnit, Number.parseInt(metricValue, 10));
metrics.addDimension(
Object.entries(JSON.parse(extraDimension))[0][0],
Object.entries(JSON.parse(extraDimension))[0][1] as string
Expand All @@ -52,7 +52,7 @@ class Lambda implements LambdaInterface {
metricWithItsOwnDimensions.addMetric(
singleMetricName,
singleMetricUnit,
Number.parseInt(singleMetricValue)
Number.parseInt(singleMetricValue, 10)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ describe('Metrics E2E tests, basic features decorator usage', () => {
? metricStat.Datapoints[0]
: {};
expect(singleDataPoint?.Sum).toBeGreaterThanOrEqual(
Number.parseInt(expectedMetricValue) * invocations
Number.parseInt(expectedMetricValue, 10) * invocations
);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,11 @@ const singleMetricValue = process.env.EXPECTED_SINGLE_METRIC_VALUE ?? '2';

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

export const handler = async (
_event: unknown,
_context: Context
): Promise<void> => {
export const handler = (_event: unknown, _context: Context) => {
metrics.captureColdStartMetric();
metrics.throwOnEmptyMetrics();
metrics.setDefaultDimensions(JSON.parse(defaultDimensions));
metrics.addMetric(metricName, metricUnit, Number.parseInt(metricValue));
metrics.addMetric(metricName, metricUnit, Number.parseInt(metricValue, 10));
metrics.addDimension(
Object.entries(JSON.parse(extraDimension))[0][0],
Object.entries(JSON.parse(extraDimension))[0][1] as string
Expand All @@ -46,7 +43,7 @@ export const handler = async (
metricWithItsOwnDimensions.addMetric(
singleMetricName,
singleMetricUnit,
Number.parseInt(singleMetricValue)
Number.parseInt(singleMetricValue, 10)
);

metrics.publishStoredMetrics();
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/tests/e2e/basicFeatures.manual.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('Metrics E2E tests, manual usage', () => {
? metricStat.Datapoints[0]
: {};
expect(singleDataPoint.Sum).toBeGreaterThanOrEqual(
Number.parseInt(expectedMetricValue) * invocations
Number.parseInt(expectedMetricValue, 10) * invocations
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/metrics/tests/helpers/metricsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
} from '@aws-sdk/client-cloudwatch';
import promiseRetry from 'promise-retry';

const getMetrics = async (
const getMetrics = (
cloudWatchClient: CloudWatchClient,
namespace: string,
metric: string,
Expand Down
21 changes: 15 additions & 6 deletions packages/metrics/tests/unit/logMetrics.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setTimeout } from 'node:timers/promises';
import { cleanupMiddlewares } from '@aws-lambda-powertools/commons';
import middy from '@middy/core';
import type { Context } from 'aws-lambda';
Expand Down Expand Up @@ -39,6 +40,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {

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

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

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

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

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

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

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

vi.spyOn(metrics, 'publishStoredMetrics');
const handler = middy(async () => {
const handler = middy(() => {
metrics.addMetric('greetings', MetricUnit.Count, 1);
}).use(logMetrics(metrics, { captureColdStartMetric: true }));

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

vi.spyOn(metrics, 'publishStoredMetrics');
const handler = middy(async () => {
const handler = middy(() => {
metrics.addMetric('greetings', MetricUnit.Count, 1);
}).use(logMetrics(metrics, { captureColdStartMetric: true }));

Expand Down Expand Up @@ -266,6 +271,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
class Test {
@metrics.logMetrics({ defaultDimensions: { environment: 'test' } })
async handler(_event: unknown, _context: Context) {
await setTimeout(0); // Simulate some async operation
metrics.addMetric('test', MetricUnit.Count, 1);
}
}
Expand Down Expand Up @@ -295,6 +301,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
});
vi.spyOn(metrics, 'publishStoredMetrics');
const handler = middy(async () => {
await setTimeout(0); // Simulate some async operation
metrics.addMetric('greetings', MetricUnit.Count, 1);
}).use(
logMetrics(metrics, {
Expand Down Expand Up @@ -328,6 +335,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
class Test {
@metrics.logMetrics()
async handler(_event: unknown, _context: Context) {
await setTimeout(0); // Simulate some async operation
throw new Error('Something went wrong');
}
}
Expand All @@ -349,6 +357,7 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
class Test {
@metrics.logMetrics({ throwOnEmptyMetrics: true })
async handler(_event: unknown, _context: Context) {
await setTimeout(0); // Simulate some async operation
return 'Hello, world!';
}
}
Expand All @@ -367,12 +376,12 @@ describe('LogMetrics decorator & Middy.js middleware', () => {
singleMetric: false,
namespace: DEFAULT_NAMESPACE,
});
const handler = middy(async () => {}).use(
logMetrics([metrics], { throwOnEmptyMetrics: true })
);
const handler = middy(async () => {
await setTimeout(0); // Simulate some async operation
}).use(logMetrics([metrics], { throwOnEmptyMetrics: true }));

// Act & Assess
expect(() => handler({}, {} as Context)).rejects.toThrowError(
await expect(() => handler({}, {} as Context)).rejects.toThrowError(
'The number of metrics recorded must be higher than zero'
);
});
Expand Down
Loading