forked from aws-powertools/powertools-lambda-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcold-start.ts
26 lines (19 loc) · 996 Bytes
/
cold-start.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
import { populateEnvironmentVariables } from '../tests/helpers';
import { Metrics, MetricUnits } from '../src';
import middy from '@middy/core';
import { logMetrics } from '../src';
// Populate runtime
populateEnvironmentVariables();
// Additional runtime variables
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';
const metrics = new Metrics();
const lambdaHandler = async (): Promise<void> => {
metrics.addDimension('custom-dimension', 'true');
metrics.addMetric('test-metric', MetricUnits.Count, 10);
};
const handlerWithMiddleware = middy(lambdaHandler)
.use(logMetrics(metrics, { captureColdStartMetric: true }));
handlerWithMiddleware(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
handlerWithMiddleware(dummyEvent, dummyContext, () => console.log('Lambda invoked again!'));