-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathexample-function.Tracer.Middleware.ts
30 lines (25 loc) · 1.3 KB
/
example-function.Tracer.Middleware.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
27
28
29
30
import middy from '@middy/core';
import { Context } from 'aws-lambda';
import { Events } from '@aws-lambda-powertools/commons';
import { captureLambdaHandler, Tracer } from '@aws-lambda-powertools/tracer';
const tracer = new Tracer({ serviceName: 'tracerMiddlewareFn' });
// Alternatively, you can also set the service name using the POWERTOOLS_SERVICE_NAME environment variable
// Learn more at: https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html
const lambdaHandler = async (event: typeof Events.Custom.CustomEvent, context: Context) => {
// Add custom annotation & metadata
tracer.putAnnotation('awsRequestId', context.awsRequestId);
tracer.putMetadata('eventPayload', event);
let res;
try {
res = { foo: 'bar' };
} catch (err) {
throw err;
}
return res;
}
// We instrument the handler with the Middy middleware and the Tracer will automatically:
// * handle the lifecycle of the subsegment
// * create a ColdStart annotation to easily filter traces that have had an initialization overhead
// * create a Service annotation to easily filter traces that have a specific service name
// * captures any response, or full exceptions generated by the handler, and include them as tracing metadata
export const handler = middy(lambdaHandler).use(captureLambdaHandler(tracer));