@@ -5,7 +5,7 @@ import { Tracer } from '@aws-lambda-powertools/tracer';
5
5
// process.env.POWERTOOLS_SERVICE_NAME = 'tracerManualFn'; // Alternative to setting the service name in the constructor
6
6
const tracer = new Tracer ( { serviceName : 'tracerManualFn' } ) ;
7
7
8
- export const handler = async ( event : typeof Events . Custom . CustomEvent , context : Context ) => {
8
+ export const handler = async ( event : typeof Events . Custom . CustomEvent , context : Context ) : Promise < unknown > => {
9
9
const segment = tracer . getSegment ( ) ; // This is the facade segment (the one that is created by AWS Lambda)
10
10
// Create subsegment for the function & set it as active
11
11
const subsegment = segment . addNewSubsegment ( `## ${ process . env . _HANDLER } ` ) ;
@@ -21,19 +21,19 @@ export const handler = async (event: typeof Events.Custom.CustomEvent, context:
21
21
22
22
let res ;
23
23
try {
24
- res = { foo : 'bar' } ;
25
- // Add the response as metadata
26
- tracer . addResponseAsMetadata ( res , process . env . _HANDLER ) ;
24
+ res = { foo : 'bar' } ;
25
+ // Add the response as metadata
26
+ tracer . addResponseAsMetadata ( res , process . env . _HANDLER ) ;
27
27
} catch ( err ) {
28
- // Add the error as metadata
29
- tracer . addErrorAsMetadata ( err as Error ) ;
30
- throw err ;
28
+ // Add the error as metadata
29
+ tracer . addErrorAsMetadata ( err as Error ) ;
30
+ throw err ;
31
+ } finally {
32
+ // Close subsegment (the AWS Lambda one is closed automatically)
33
+ subsegment . close ( ) ;
34
+ // Set the facade segment as active again
35
+ tracer . setSegment ( segment ) ;
31
36
}
32
37
33
- // Close subsegment (the AWS Lambda one is closed automatically)
34
- subsegment . close ( ) ;
35
- // Set the facade segment as active again
36
- tracer . setSegment ( segment ) ;
37
-
38
38
return res ;
39
39
} ;
0 commit comments