Skip to content

Commit

Permalink
Merge pull request #17 from jorgevrgs/development
Browse files Browse the repository at this point in the history
test(metrics): add more testing for cold start
  • Loading branch information
jorgevrgs authored Sep 24, 2024
2 parents b6b3f39 + 2f13436 commit 819b3c2
Show file tree
Hide file tree
Showing 3 changed files with 420 additions and 10 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fastify-aws-powertools",
"version": "0.0.15",
"version": "0.0.16",
"description": "",
"exports": {
"node": {
Expand Down
11 changes: 11 additions & 0 deletions src/services/metrics.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { Metrics } from '@aws-lambda-powertools/metrics';
import type { ExtraOptions } from '@aws-lambda-powertools/metrics/types';
import type {
FastifyRequest,
onErrorAsyncHookHandler,
onRequestAsyncHookHandler,
onResponseAsyncHookHandler,
} from 'fastify';
import { METRICS_KEY, POWERTOOLS_REQUEST_KEY } from '../constants';
import { isAwsLambdaRequest } from '../helpers';

export function metricsService(
Expand All @@ -16,6 +18,13 @@ export function metricsService(
const { throwOnEmptyMetrics, defaultDimensions, captureColdStartMetric } =
options;

const setCleanupFunction = (request: FastifyRequest) => {
request[POWERTOOLS_REQUEST_KEY] = {
...request[POWERTOOLS_REQUEST_KEY],
[METRICS_KEY]: onResponseOrErrorHandler,
};
};

const onRequestHook: onRequestAsyncHookHandler = async (request, _reply) => {
if (!isAwsLambdaRequest(request)) {
request.log.warn('Request does not contain AWS Lambda object');
Expand All @@ -37,6 +46,8 @@ export function metricsService(
metrics.captureColdStartMetric();
}
}

setCleanupFunction(request);
};

const onResponseOrErrorHandler = () => {
Expand Down
Loading

0 comments on commit 819b3c2

Please sign in to comment.