Skip to content

Commit 630b56d

Browse files
committed
Updates to examples & docs
1 parent 5e442b0 commit 630b56d

File tree

8 files changed

+103
-74
lines changed

8 files changed

+103
-74
lines changed

docs/core/tracer.md

+36-18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ Tracer is an opinionated thin wrapper for [AWS X-Ray SDK for Node.js](https://gi
1616

1717
## Getting started
1818

19+
Tracer has three global settings that can be used to configure its behavior:
20+
21+
Setting | Description | Environment variable | Constructor parameter
22+
------------------------------------------------- | ------------------------------------------------- | ------------------------------------------------- | -------------------------------------------------
23+
**Trace Enabled** | Explicitly enables/disables tracing | `POWERTOOLS_TRACE_ENABLED` | `enabled`
24+
**Capture Responses** | Captures Lambda or method return as metadata | `POWERTOOLS_TRACER_CAPTURE_RESPONSE` | N/A
25+
**Capture Errors** | Captures Lambda or method exception as metadata | `POWERTOOLS_TRACER_CAPTURE_ERROR` | N/A
26+
1927
### Permissions
2028

2129
Before your use this utility, your AWS Lambda function [must have permissions](https://docs.aws.amazon.com/lambda/latest/dg/services-xray.html#services-xray-permissions) to send traces to AWS X-Ray.
@@ -63,7 +71,7 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
6371
// OR tracer = Tracer({ service: 'example' });
6472

6573
class Lambda {
66-
@tracer.captureLambdaHanlder()
74+
@tracer.captureLambdaHandler()
6775
public handler(event: any, context: any) {
6876
...
6977
}
@@ -79,27 +87,33 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
7987
import { Tracer } from '@aws-lambda-powertools/tracer';
8088

8189
const tracer = Tracer(); // Sets service via env var
82-
// OR tracer = Tracer({ service: 'example' });
90+
// OR tracer = Tracer({ service: 'serverlessAirline' });
8391

8492
export const handler = async (_event: any, context: any) => {
8593
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}`);
94+
// Create subsegment for the function & set it as active
95+
const subsegment = segment.addNewSubsegment(`## ${process.env._HANDLER}`);
96+
tracer.setSegment(subsegment);
97+
98+
// Annotate the subsegment with the cold start & serviceName
8899
tracer.annotateColdStart();
89100
tracer.addServiceNameAnnotation();
90101

91102
let res;
92103
try {
93104
res = ...
94105
// Add the response as metadata
95-
tracer.addResponseAsMetadata(res, context.functionName);
106+
tracer.addResponseAsMetadata(res, process.env._HANDLER);
96107
} catch (err) {
97108
// Add the error as metadata
98109
tracer.addErrorAsMetadata(err as Error);
110+
throw err;
99111
}
100112

101113
// Close subsegment (the AWS Lambda one is closed automatically)
102-
handlerSegment.close();
114+
subsegment.close();
115+
// Set the facade segment as active again
116+
tracer.setSegment(segment);
103117

104118
return res;
105119
}
@@ -109,6 +123,7 @@ When using the `captureLambdaHandler` decorator or middleware, Tracer performs t
109123

110124
* Handles the lifecycle of the subsegment
111125
* Creates a `ColdStart` annotation to easily filter traces that have had an initialization overhead
126+
* Creates a `ServiceName` annotation to easily filter traces that have a specific service name
112127
* Captures any response, or full exceptions generated by the handler, and include as tracing metadata
113128

114129
### Annotations & Metadata
@@ -123,10 +138,10 @@ When using the `captureLambdaHandler` decorator or middleware, Tracer performs t
123138
```typescript hl_lines="6"
124139
import { Tracer } from '@aws-lambda-powertools/tracer';
125140

126-
const tracer = new Tracer({ serviceName: 'my-service' });
141+
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
127142

128143
export const handler = async (_event: any, _context: any) => {
129-
tracer.putAnnotation('PaymentStatus', "SUCCESS");
144+
tracer.putAnnotation('successfulBooking', true);
130145
}
131146
```
132147
=== "Metadata"
@@ -135,11 +150,11 @@ When using the `captureLambdaHandler` decorator or middleware, Tracer performs t
135150
```typescript hl_lines="7"
136151
import { Tracer } from '@aws-lambda-powertools/tracer';
137152

138-
const tracer = new Tracer({ serviceName: 'my-service' });
153+
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
139154

140155
export const handler = async (_event: any, _context: any) => {
141156
const res = someLogic();
142-
tracer.putMetadata('PaymentResponse', res);
157+
tracer.putMetadata('paymentResponse', res);
143158
}
144159
```
145160

@@ -178,29 +193,32 @@ You can trace other methods using the `captureMethod` decorator or manual instru
178193

179194
=== "Manual"
180195

181-
```typescript hl_lines="2 8-9 15 18 22"
196+
```typescript hl_lines="6 8-9 15 23 25"
182197
import { Tracer } from '@aws-lambda-powertools/tracer';
183-
import { Segment } from 'aws-xray-sdk-core';
184198

185-
const tracer = new Tracer({ serviceName: 'my-service' });
199+
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
186200

187201
const chargeId = async () => {
188-
// Create subsegment & set it as active
189-
const subsegment = new Subsegment(`### chargeId`);
202+
const parentSubsegment = tracer.getSegment(); // This is the subsegment currently active
203+
// Create subsegment for the function & set it as active
204+
const subsegment = parentSubsegment.addNewSubsegment(`### chargeId`);
190205
tracer.setSegment(subsegment);
191206

192207
let res;
193208
try {
194209
res = await someLogic(); // Do something
195210
// Add the response as metadata
196-
tracer.putMetadata(`chargeId response`, data);
211+
tracer.addResponseAsMetadata(res, 'chargeId');
197212
} catch (err) {
198213
// Add the error as metadata
199-
subsegment.addError(err, false);
214+
tracer.addErrorAsMetadata(err as Error);
215+
throw err;
200216
}
201217

202-
// Close subsegment
218+
// Close subsegment (the AWS Lambda one is closed automatically)
203219
subsegment.close();
220+
// Set the facade segment as active again
221+
tracer.setSegment(parentSubsegment);
204222

205223
return res;
206224
}

docs/index.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ This project separates core utilities that will be available in other runtimes v
4040
| ------------------------------------------------- | --------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- | ------------------------------------------------- |
4141
| **POWERTOOLS_SERVICE_NAME** | Sets service name used for tracing namespace, metrics dimension and structured logging | All | `"service_undefined"` |
4242
| **POWERTOOLS_METRICS_NAMESPACE** | Sets namespace used for metrics | [Metrics](./core/metrics) | `None` |
43-
| **POWERTOOLS_TRACE_DISABLED** | Explicitly disables tracing | [Tracing](./core/tracer) | `false` |
44-
| **POWERTOOLS_TRACER_CAPTURE_RESPONSE** | Captures Lambda or method return as metadata. | [Tracing](./core/tracer) | `true` |
45-
| **POWERTOOLS_TRACER_CAPTURE_ERROR** | Captures Lambda or method exception as metadata. | [Tracing](./core/tracer) | `true` |
43+
| **POWERTOOLS_TRACE_ENABLED** | Explicitly enables/disables tracing | [Tracing](./core/tracer) | `true` |
44+
| **POWERTOOLS_TRACER_CAPTURE_RESPONSE** | Captures Lambda or method return as metadata | [Tracing](./core/tracer) | `true` |
45+
| **POWERTOOLS_TRACER_CAPTURE_ERROR** | Captures Lambda or method exception as metadata | [Tracing](./core/tracer) | `true` |
4646
| **POWERTOOLS_LOGGER_LOG_EVENT** | Logs incoming event | [Logging](./core/logger) | `false` |
4747
| **POWERTOOLS_LOGGER_SAMPLE_RATE** | Debug log sampling | [Logging](./core/logger) | `0` |
4848
| **POWERTOOLS_LOG_DEDUPLICATION_DISABLED** | Disables log deduplication filter protection to use Pytest Live Log feature | [Logging](./core/logger) | `false` |

packages/tracing/README.md

+13-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ If you use function-based Lambda handlers you can use the [captureLambdaHandler(
2626
import { Tracer, captureLambdaHandler } from '@aws-lambda-powertools/tracer';
2727
import middy from '@middy/core';
2828

29-
const tracer = new Tracer({ serviceName: 'my-service' });
29+
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
3030

3131
export const handler = middy(async (_event: any, _context: any) => {
3232
...
@@ -44,7 +44,7 @@ If instead you use TypeScript Classes to wrap your Lambda handler you can use th
4444
```typescript
4545
import { Tracer } from '@aws-lambda-powertools/tracer';
4646

47-
const tracer = new Tracer({ serviceName: 'my-service' });
47+
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
4848

4949
class Lambda {
5050
@tracer.captureLambdaHandler()
@@ -64,27 +64,33 @@ If you prefer to manually instrument your Lambda handler you can use the methods
6464
```typescript
6565
import { Tracer } from '@aws-lambda-powertools/tracer';
6666

67-
const tracer = new Tracer({ serviceName: 'my-service' });
67+
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
6868

6969
export const handler = async (_event: any, context: any) => {
7070
const segment = tracer.getSegment(); // This is the facade segment (the one that is created by AWS Lambda)
71-
// Create subsegment for the function
72-
const handlerSegment = segment.addNewSubsegment(`## ${context.functionName}`);
71+
// Create subsegment for the function & set it as active
72+
const subsegment = segment.addNewSubsegment(`## ${process.env._HANDLER}`);
73+
tracer.setSegment(subsegment);
74+
75+
// Annotate the subsegment with the cold start & serviceName
7376
tracer.annotateColdStart();
7477
tracer.addServiceNameAnnotation();
7578

7679
let res;
7780
try {
7881
res = ...
7982
// Add the response as metadata
80-
tracer.addResponseAsMetadata(res, context.functionName);
83+
tracer.addResponseAsMetadata(res, process.env._HANDLER);
8184
} catch (err) {
8285
// Add the error as metadata
8386
tracer.addErrorAsMetadata(err as Error);
87+
throw err;
8488
}
8589

8690
// Close subsegment (the AWS Lambda one is closed automatically)
87-
handlerSegment.close();
91+
subsegment.close();
92+
// Set the facade segment as active again
93+
tracer.setSegment(segment);
8894

8995
return res;
9096
}

0 commit comments

Comments
 (0)