@@ -9,7 +9,7 @@ Tracer is an opinionated thin wrapper for [AWS X-Ray SDK for Node.js](https://gi
9
9
10
10
## Key features
11
11
12
- * Auto capture cold start as annotation , and responses or full exceptions as metadata
12
+ * Auto capture cold start and service name as annotations , and responses or full exceptions as metadata
13
13
* Auto-disable when not running in AWS Lambda environment
14
14
* Support tracing functions via decorators, middleware, and manual instrumentation
15
15
* Support tracing AWS SDK v2 and v3 via AWS X-Ray SDK for Node.js
@@ -75,32 +75,31 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
75
75
76
76
=== "Manual"
77
77
78
- ```typescript hl_lines="1-2 4 9-10 12 18 21 25 "
78
+ ```typescript hl_lines="1 3 7 9 11 17 20 24 "
79
79
import { Tracer } from '@aws-lambda-powertools/tracer';
80
- import { Segment } from 'aws-xray-sdk-core';
81
80
82
81
const tracer = Tracer(); // Sets service via env var
83
82
// OR tracer = Tracer({ service: 'example' });
84
83
85
84
export const handler = async (_event: any, context: any) => {
86
- // Create subsegment & set it as active
87
- const subsegment = new Subsegment(`## ${context.functionName}`);
88
- tracer.setSegment(subsegment );
89
- // Add the ColdStart annotation
90
- this.putAnnotation('ColdStart', tracer.coldStart );
85
+ const segment = tracer.getSegment(); // This is the facade segment (the one that is created by AWS Lambda)
86
+ // Create subsegment for the function
87
+ const handlerSegment = segment.addNewSubsegment(`## ${context.functionName}` );
88
+ tracer.annotateColdStart();
89
+ tracer.addServiceNameAnnotation( );
91
90
92
91
let res;
93
92
try {
94
- res = await someLogic(); // Do something
95
- // Add the response as metadata
96
- tracer.putMetadata(`${ context.functionName} response`, data );
93
+ res = ...
94
+ // Add the response as metadata
95
+ tracer.addResponseAsMetadata(res, context.functionName);
97
96
} catch (err) {
98
97
// Add the error as metadata
99
- subsegment.addError (err, false );
98
+ tracer.addErrorAsMetadata (err as Error );
100
99
}
101
-
102
- // Close subsegment
103
- subsegment .close();
100
+
101
+ // Close subsegment (the AWS Lambda one is closed automatically)
102
+ handlerSegment .close();
104
103
105
104
return res;
106
105
}
0 commit comments