forked from aws-powertools/powertools-lambda-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-function.MyFunction.ts
61 lines (49 loc) · 2.35 KB
/
example-function.MyFunction.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
import { Context } from 'aws-lambda';
import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
import { Logger } from '@aws-lambda-powertools/logger';
import { Tracer } from '@aws-lambda-powertools/tracer';
const namespace = 'CDKExample';
const serviceName = 'MyFunctionWithStandardHandler';
const metrics = new Metrics({ namespace: namespace, service: serviceName });
const logger = new Logger({ logLevel: 'INFO', serviceName: serviceName });
const tracer = new Tracer({ serviceName: serviceName });
export const handler = async (_event: unknown, context: Context): Promise<void> => {
// Since we are in manual mode we need to create the handler segment (the 4 lines below would be done for you by decorator/middleware)
// we do it at the beginning because we want to trace the whole duration of the handler
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by Lambda & that can't be manipulated)
const handlerSegment = segment.addNewSubsegment(`## ${context.functionName}`);
// TODO: expose tracer.annotateColdStart()
tracer.putAnnotation('ColdStart', Tracer.coldStart);
// ### Experiment logger
logger.addPersistentLogAttributes({
testKey: 'testValue',
});
logger.debug('This is an DEBUG log'); // Won't show by default
logger.info('This is an INFO log');
logger.warn('This is an WARN log');
logger.error('This is an ERROR log');
// ### Experiment metrics
metrics.captureColdStartMetric();
metrics.throwOnEmptyMetrics();
metrics.setDefaultDimensions({ environment: 'example', type: 'standardFunction' });
metrics.addMetric('test-metric', MetricUnits.Count, 10);
const metricWithItsOwnDimensions = metrics.singleMetric();
metricWithItsOwnDimensions.addDimension('InnerDimension', 'true');
metricWithItsOwnDimensions.addMetric('single-metric', MetricUnits.Percent, 50);
metrics.purgeStoredMetrics();
metrics.throwOnEmptyMetrics();
// ### Experiment tracer
tracer.putAnnotation('Myannotation', 'My annotation\'s value');
// Create subsegment & set it as active
const subsegment = handlerSegment.addNewSubsegment('MySubSegment');
try {
throw new Error('test');
// Add the response as metadata
} catch (err) {
// Add the error as metadata
subsegment.addError(err as Error, false);
}
// Close subsegment
subsegment.close();
handlerSegment.close();
};