Skip to content

Commit 6addea6

Browse files
committed
Documentation
1 parent ccba5ef commit 6addea6

File tree

4 files changed

+83
-30
lines changed

4 files changed

+83
-30
lines changed

docs/core/tracer.md

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,16 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
4242

4343
=== "Middleware"
4444

45-
```typescript hl_lines="1 3 6"
45+
```typescript hl_lines="1-2 4 7 9"
4646
import { Tracer } from '@aws-lambda-powertools/tracer';
47+
import middy from '@middy/core';
4748

4849
const tracer = Tracer(); // Sets service via env var
4950
// OR tracer = Tracer({ service: 'example' });
5051

51-
// TODO: update example once middleware has been implemented.
52-
53-
export const handler = async (_event: any, _context: any) => {
52+
export const handler = middy(async (_event: any, _context: any) => {
5453
...
55-
}
54+
}).use(captureLambdaHandler(tracer));
5655
```
5756

5857
=== "Decorator"
@@ -76,7 +75,7 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
7675

7776
=== "Manual"
7877

79-
```typescript hl_lines="1-2 4 8-9 11 17 20 24"
78+
```typescript hl_lines="1-2 4 9-10 12 18 21 25"
8079
import { Tracer } from '@aws-lambda-powertools/tracer';
8180
import { Segment } from 'aws-xray-sdk-core';
8281

@@ -107,8 +106,7 @@ You can quickly start by importing the `Tracer` class, initialize it outside the
107106
}
108107
```
109108

110-
<!-- TODO: Replace name of middleware once implemented -->
111-
When using thes `captureLambdaHanlder` decorator or the `TBD` middleware, Tracer performs these additional tasks to ease operations:
109+
When using the `captureLambdaHanlder` decorator or middleware, Tracer performs these additional tasks to ease operations:
112110

113111
* Handles the lifecycle of the subsegment
114112
* Creates a `ColdStart` annotation to easily filter traces that have had an initialization overhead
@@ -148,23 +146,10 @@ When using thes `captureLambdaHanlder` decorator or the `TBD` middleware, Tracer
148146

149147
### Methods
150148

151-
You can trace other methods using the `captureMethod` decorator.
152-
153-
=== "Middleware"
154-
155-
```typescript hl_lines="1 3 6"
156-
import { Tracer } from '@aws-lambda-powertools/tracer';
157-
158-
const tracer = Tracer();
149+
You can trace other methods using the `captureMethod` decorator or manual instrumentation.
159150

160-
// TODO: update example once middleware has been implemented.
161-
162-
163-
164-
export const handler = async (_event: any, _context: any) => {
165-
...
166-
}
167-
```
151+
!!! info
152+
We currently support a middleware for tracing methods, [let us know](https://github.com/awslabs/aws-lambda-powertools-typecsript/issues/new?assignees=&labels=feature-request%2C+triage&template=feature_request.md&title=) if you'd like to see one!
168153

169154
=== "Decorator"
170155

packages/tracing/src/Tracer.ts

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,28 @@ import { Segment, Subsegment } from 'aws-xray-sdk-core';
2020
* ## Usage
2121
*
2222
* ### Functions usage with middlewares
23-
* TBD
23+
*
24+
* If you use function-based Lambda handlers you can use the [captureLambdaHanlder()](./_aws_lambda_powertools_tracer.Tracer.html) middy middleware to automatically:
25+
* * handle the subsegment lifecycle
26+
* * add the `ColdStart` annotation
27+
* * add the function response as metadata
28+
* * add the function error as metadata (if any)
29+
*
30+
* @example
31+
* ```typescript
32+
* import { Tracer, captureLambdaHandler } from '@aws-lambda-powertools/tracer';
33+
* import middy from '@middy/core';
34+
*
35+
* const tracer = new Tracer({ serviceName: 'my-service' });
36+
*
37+
* export const handler = middy(async (_event: any, _context: any) => {
38+
* ...
39+
* }).use(captureLambdaHandler(tracer));
40+
* ```
2441
*
2542
* ### Object oriented usage with decorators
2643
*
27-
* If you use TypeScript Classes to wrap your Lambda handler you can use the [@tracer.captureLambdaHanlder()](./_aws_lambda_powertools_tracer.Tracer.html#captureLambdaHanlder) decorator to automatically:
44+
* If instead you use TypeScript Classes to wrap your Lambda handler you can use the [@tracer.captureLambdaHanlder()](./_aws_lambda_powertools_tracer.Tracer.html#captureLambdaHanlder) decorator to automatically:
2845
* * handle the subsegment lifecycle
2946
* * add the `ColdStart` annotation
3047
* * add the function response as metadata
@@ -65,7 +82,7 @@ import { Segment, Subsegment } from 'aws-xray-sdk-core';
6582
* const subsegment = new Subsegment(`## ${context.functionName}`);
6683
* tracer.setSegment(subsegment);
6784
* // Add the ColdStart annotation
68-
* this.putAnnotation('ColdStart', tracer.coldStart);
85+
* this.putAnnotation('ColdStart', tracer.isColdStart());
6986
*
7087
* let res;
7188
* try {
@@ -351,10 +368,26 @@ class Tracer implements TracerInterface {
351368
return segment;
352369
}
353370

371+
/**
372+
* Get the current value of the `captureError` property.
373+
*
374+
* You can use this method during manual instrumentation to determine
375+
* if tracer should be capturing errors.
376+
*
377+
* @returns captureError - `true` if errors should be captured, `false` otherwise.
378+
*/
354379
public isCaptureErrorEnabled(): boolean {
355380
return this.captureError;
356381
}
357382

383+
/**
384+
* Get the current value of the `captureResponse` property.
385+
*
386+
* You can use this method during manual instrumentation to determine
387+
* if tracer should be capturing function responses.
388+
*
389+
* @returns captureResponse - `true` if responses should be captured, `false` otherwise.
390+
*/
358391
public isCaptureResponseEnabled(): boolean {
359392
return this.captureResponse;
360393
}
@@ -368,7 +401,7 @@ class Tracer implements TracerInterface {
368401
*
369402
* @see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
370403
*
371-
* @returns boolean - true if is cold start otherwise false
404+
* @returns boolean - `true` if is cold start, otherwise `false`
372405
*/
373406
public static isColdStart(): boolean {
374407
if (Tracer.coldStart === true) {
@@ -380,6 +413,14 @@ class Tracer implements TracerInterface {
380413
return false;
381414
}
382415

416+
/**
417+
* Get the current value of the `tracingEnabled` property.
418+
*
419+
* You can use this method during manual instrumentation to determine
420+
* if tracer is currently enabled.
421+
*
422+
* @returns tracingEnabled - `true` if tracing is enabled, `false` otherwise.
423+
*/
383424
public isTracingEnabled(): boolean {
384425
return this.tracingEnabled;
385426
}

packages/tracing/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './helpers';
22
export * from './Tracer';
3-
export * from './TracerInterface';
3+
export * from './TracerInterface';
4+
export * from './middleware/middy';

packages/tracing/src/middleware/middy.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,30 @@ import middy from '@middy/core';
22
import { Subsegment } from 'aws-xray-sdk-core';
33
import { Tracer } from '../Tracer';
44

5+
/**
6+
* A middy middleware automating capture of metadata and annotations on segments or subsegments ofr a Lambda Handler.
7+
*
8+
* Using this middleware on your handler function will automatically:
9+
* * handle the subsegment lifecycle
10+
* * add the `ColdStart` annotation
11+
* * add the function response as metadata
12+
* * add the function error as metadata (if any)
13+
*
14+
* @example
15+
* ```typescript
16+
* import { Tracer, captureLambdaHandler } from '@aws-lambda-powertools/tracer';
17+
* import middy from '@middy/core';
18+
*
19+
* const tracer = new Tracer({ serviceName: 'my-service' });
20+
*
21+
* export const handler = middy(async (_event: any, _context: any) => {
22+
* ...
23+
* }).use(captureLambdaHandler(tracer));
24+
* ```
25+
*
26+
* @param tracer - The Tracer instance to use for tracing
27+
* @returns middleware object - The middy middleware object
28+
*/
529
const captureLambdaHandler = (target: Tracer): middy.MiddlewareObj => {
630
const captureLambdaHandlerBefore = async (request: middy.Request): Promise<void> => {
731
if (target.isTracingEnabled()) {
@@ -34,7 +58,9 @@ const captureLambdaHandler = (target: Tracer): middy.MiddlewareObj => {
3458
subsegment?.addError(request.error as Error, false);
3559
}
3660
// TODO: should this error be thrown?? I.e. should we stop the event flow & return?
37-
// throw error;
61+
// throw request.error;
62+
63+
subsegment?.close();
3864
}
3965
};
4066

0 commit comments

Comments
 (0)