-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathexample-function.MyFunctionWithMiddy.ts
75 lines (61 loc) · 2.85 KB
/
example-function.MyFunctionWithMiddy.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import middy from '@middy/core';
import { Context } from 'aws-lambda';
import { Events } from '@aws-lambda-powertools/commons';
import { Metrics, MetricUnits, logMetrics } from '@aws-lambda-powertools/metrics';
import { Tracer, captureLambdaHandler } from '@aws-lambda-powertools/tracer';
import { Logger, injectLambdaContext } from '@aws-lambda-powertools/logger';
const namespace = 'CDKExample';
const serviceName = 'MyFunctionWithMiddyMiddleware';
const metrics = new Metrics({ namespace: namespace, serviceName: serviceName });
const logger = new Logger({ logLevel: 'INFO', serviceName: serviceName });
const tracer = new Tracer({ serviceName: serviceName });
const lambdaHandler = async (event: typeof Events.Custom.CustomEvent, context: Context): Promise<unknown> => {
// ### Experiment with Logger
// AWS Lambda context is automatically injected by the middleware
logger.addPersistentLogAttributes({
testKey: 'testValue',
});
logger.debug('This is an DEBUG log'); // Won't show because we pass logLevel: 'INFO' in the constructor.
logger.info('This is an INFO log');
logger.warn('This is an WARN log');
logger.error('This is an ERROR log');
// ### Experiment with Metrics
// Default metrics, cold start, and throwOnEmptyMetrics are enabled by the middleware
metrics.addMetric('test-metric', MetricUnits.Count, 10);
const metricWithItsOwnDimensions = metrics.singleMetric();
metricWithItsOwnDimensions.addDimension('InnerDimension', 'true');
metricWithItsOwnDimensions.addMetric('single-metric', MetricUnits.Percent, 50);
// ### Experiment with Tracer
// Service & Cold Start annotations will be added for you by the decorator/middleware
// These traces will be added to the main segment (## index.handler)
tracer.putAnnotation('awsRequestId', context.awsRequestId);
tracer.putMetadata('eventPayload', event);
// Create another subsegment & set it as active
const handlerSegment = tracer.getSegment(); // This is the custom segment created by Tracer for you (## index.handler)
const subsegment = handlerSegment.addNewSubsegment('### MySubSegment');
tracer.setSegment(subsegment);
let res;
try {
res = { foo: 'bar' };
} catch (err) {
throw err;
} finally {
// Close the subsegment you created (### MySubSegment)
subsegment.close();
// Set back the original segment as active (## index.handler)
tracer.setSegment(handlerSegment);
// The main segment (facade) will be closed for you at the end by the decorator/middleware
}
return res;
};
// We instrument the handler with the various Middy middlewares
export const handler = middy(lambdaHandler)
.use(captureLambdaHandler(tracer))
.use(
logMetrics(metrics, {
captureColdStartMetric: true,
throwOnEmptyMetrics: true,
defaultDimensions: { environment: 'example', type: 'withDecorator' },
})
)
.use(injectLambdaContext(logger));