Skip to content
Merged
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
19 changes: 7 additions & 12 deletions packages/event-handler/src/http/middleware/metrics.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Metrics } from '@aws-lambda-powertools/metrics';
import { MetricUnit } from '@aws-lambda-powertools/metrics';
import type { Middleware, RequestContext } from '../../types/http.js';
import { HttpError } from '../errors.js';

Expand Down Expand Up @@ -99,19 +98,15 @@ const metrics = (metrics: Metrics): Middleware => {
for (const [key, value] of Object.entries(metadata)) {
metrics.addMetadata(key, value);
}
// The metric units are inlined as string literals rather than imported
// from the `MetricUnit` value of `@aws-lambda-powertools/metrics`. A value
// import would be retained by bundlers and force a runtime dependency on
// the metrics package even for consumers that never use this middleware.
metrics
.addDimension('route', reqCtx.route ?? 'NOT_FOUND')
.addMetric(
'latency',
MetricUnit.Milliseconds,
performance.now() - start
)
.addMetric('fault', MetricUnit.Count, status >= 500 ? 1 : 0)
.addMetric(
'error',
MetricUnit.Count,
status >= 400 && status < 500 ? 1 : 0
)
.addMetric('latency', 'Milliseconds', performance.now() - start)
.addMetric('fault', 'Count', status >= 500 ? 1 : 0)
.addMetric('error', 'Count', status >= 400 && status < 500 ? 1 : 0)
.publishStoredMetrics();
}
};
Expand Down
Loading