1
- import { Metrics , MetricUnits } from '@aws-lambda-powertools/metrics' ;
2
- import { Logger } from '@aws-lambda-powertools/logger' ;
3
- import { Tracer } from '@aws-lambda-powertools/tracer' ;
4
- import { Subsegment } from 'aws-xray-sdk-core' ;
5
-
6
-
1
+ import { Context } from 'aws-lambda' ;
2
+ import { Metrics , MetricUnits } from '@aws-lambda-powertools/metrics' ;
3
+ import { Logger } from '@aws-lambda-powertools/logger' ;
4
+ import { Tracer } from '@aws-lambda-powertools/tracer' ;
7
5
8
6
const namespace = 'CDKExample' ;
9
7
const serviceName = 'MyFunctionWithStandardHandler' ;
@@ -12,7 +10,14 @@ const metrics = new Metrics({ namespace: namespace, service: serviceName });
12
10
const logger = new Logger ( { logLevel : 'INFO' , serviceName : serviceName } ) ;
13
11
const tracer = new Tracer ( { serviceName : serviceName } ) ;
14
12
15
- export const handler = async ( event : any , context : any ) => {
13
+ export const handler = async ( _event : unknown , context : Context ) : Promise < unknown > => {
14
+ // 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)
15
+ // we do it at the beginning because we want to trace the whole duration of the handler
16
+ const segment = tracer . getSegment ( ) ; // This is the facade segment (the one that is created by Lambda & that can't be manipulated)
17
+ const handlerSegment = segment . addNewSubsegment ( `## ${ context . functionName } ` ) ;
18
+ // TODO: expose tracer.annotateColdStart()
19
+ tracer . putAnnotation ( 'ColdStart' , Tracer . coldStart ) ;
20
+
16
21
// ### Experiment logger
17
22
logger . addPersistentLogAttributes ( {
18
23
testKey : 'testValue' ,
@@ -36,13 +41,11 @@ export const handler = async (event: any, context: any) => {
36
41
metrics . raiseOnEmptyMetrics ( ) ;
37
42
38
43
// ### Experiment tracer
44
+
39
45
tracer . putAnnotation ( 'Myannotation' , 'My annotation\'s value' ) ;
40
46
41
47
// Create subsegment & set it as active
42
- const subsegment = new Subsegment ( 'MySubSegment' ) ;
43
- tracer . setSegment ( subsegment ) ;
44
- // TODO: Add the ColdStart annotation !!! NOT POSSIBLE
45
- // tracer.putAnnotation('ColdStart', tracer);
48
+ const subsegment = handlerSegment . addNewSubsegment ( 'MySubSegment' ) ;
46
49
47
50
try {
48
51
throw new Error ( 'test' ) ;
@@ -54,4 +57,5 @@ export const handler = async (event: any, context: any) => {
54
57
55
58
// Close subsegment
56
59
subsegment . close ( ) ;
60
+ handlerSegment . close ( ) ;
57
61
} ;
0 commit comments