Skip to content

Commit ad4b941

Browse files
chore(tracer): quality of life improvements (#337)
* Moved to common packages * Updated docs * Update main docs * Exposed addResponseAsMetadata * Exposed addErrorAsMetadata * Exposed annotateColdStarted * Feature parity w/ Python #851 * Feature parity w/ Python #861 * Revisited middleware implementation * Fixed typo in jsdocs * Reverted to local context/event usage in tests * Update packages/tracing/src/Tracer.ts Co-authored-by: Sara Gerion <[email protected]> * Update packages/tracing/README.md Co-authored-by: Sara Gerion <[email protected]> * Update packages/tracing/README.md Co-authored-by: Sara Gerion <[email protected]> * Update packages/tracing/README.md Co-authored-by: Sara Gerion <[email protected]> Co-authored-by: Sara Gerion <[email protected]>
1 parent 876687b commit ad4b941

File tree

9 files changed

+601
-1163
lines changed

9 files changed

+601
-1163
lines changed

Diff for: docs/core/tracer.md

+14-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Tracer is an opinionated thin wrapper for [AWS X-Ray SDK for Node.js](https://gi
99

1010
## Key features
1111

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
1313
* Auto-disable when not running in AWS Lambda environment
1414
* Support tracing functions via decorators, middleware, and manual instrumentation
1515
* 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
7575

7676
=== "Manual"
7777

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"
7979
import { Tracer } from '@aws-lambda-powertools/tracer';
80-
import { Segment } from 'aws-xray-sdk-core';
8180

8281
const tracer = Tracer(); // Sets service via env var
8382
// OR tracer = Tracer({ service: 'example' });
8483

8584
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();
9190

9291
let res;
9392
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);
9796
} catch (err) {
9897
// Add the error as metadata
99-
subsegment.addError(err, false);
98+
tracer.addErrorAsMetadata(err as Error);
10099
}
101-
102-
// Close subsegment
103-
subsegment.close();
100+
101+
// Close subsegment (the AWS Lambda one is closed automatically)
102+
handlerSegment.close();
104103

105104
return res;
106105
}

0 commit comments

Comments
 (0)