Skip to content

Commit 1bab6ea

Browse files
authored
docs(tracer): review API docs & README for Tracer (aws-powertools#2992)
1 parent e1e7f04 commit 1bab6ea

File tree

3 files changed

+149
-78
lines changed

3 files changed

+149
-78
lines changed

packages/tracer/README.md

Lines changed: 101 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,130 @@ Powertools for AWS Lambda (TypeScript) is a developer toolkit to implement Serve
44

55
You can use the library in both TypeScript and JavaScript code bases.
66

7-
> Also available in [Python](https://github.com/aws-powertools/powertools-lambda-python), [Java](https://github.com/aws-powertools/powertools-lambda-java), and [.NET](https://github.com/aws-powertools/powertools-lambda-dotnet).
8-
9-
**[Documentation](https://docs.powertools.aws.dev/lambda/typescript/)** | **[npm](https://www.npmjs.com/org/aws-lambda-powertools)** | **[Roadmap](https://docs.powertools.aws.dev/lambda/typescript/latest/roadmap)** | **[Examples](https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/examples)** | **[Serverless TypeScript Demo](https://github.com/aws-samples/serverless-typescript-demo)**
10-
11-
## Table of contents <!-- omit in toc -->
12-
13-
- [Features](#features)
14-
- [Getting started](#getting-started)
15-
- [Installation](#installation)
16-
- [Examples](#examples)
17-
- [Demo applications](#demo-applications)
7+
- [Intro](#intro)
8+
- [Usage](#usage)
9+
- [Basic usage](#basic-usage)
10+
- [Capture AWS SDK clients](#capture-aws-sdk-clients)
11+
- [Add metadata and annotations](#add-metadata-and-annotations)
1812
- [Contribute](#contribute)
1913
- [Roadmap](#roadmap)
2014
- [Connect](#connect)
2115
- [How to support Powertools for AWS Lambda (TypeScript)?](#how-to-support-powertools-for-aws-lambda-typescript)
2216
- [Becoming a reference customer](#becoming-a-reference-customer)
2317
- [Sharing your work](#sharing-your-work)
2418
- [Using Lambda Layer](#using-lambda-layer)
25-
- [Credits](#credits)
2619
- [License](#license)
2720

28-
## Features
21+
## Intro
22+
23+
The Tracer utility is an opinionated thin wrapper for [AWS X-Ray SDK for Node.js](https://github.com/aws/aws-xray-sdk-node), to automatically capture cold starts, trace HTTP(S) clients including `fetch` and generate segments and add metadata or annotations to traces.
24+
25+
## Usage
26+
27+
To get started, install the library by running:
28+
29+
```sh
30+
npm i @aws-lambda-powertools/tracer
31+
```
32+
33+
### Basic usage
34+
35+
Add `Tracer` to your Lambda handler as decorator:
2936

30-
- **[Tracer](https://docs.powertools.aws.dev/lambda/typescript/latest/core/tracer/)** - Utilities to trace Lambda function handlers, and both synchronous and asynchronous functions
31-
- **[Logger](https://docs.powertools.aws.dev/lambda/typescript/latest/core/logger/)** - Structured logging made easier, and a middleware to enrich log items with key details of the Lambda context
32-
- **[Metrics](https://docs.powertools.aws.dev/lambda/typescript/latest/core/metrics/)** - Custom Metrics created asynchronously via CloudWatch Embedded Metric Format (EMF)
33-
- **[Parameters](https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/parameters/)** - High-level functions to retrieve one or more parameters from AWS SSM, Secrets Manager, AppConfig, and DynamoDB
37+
```ts
38+
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
39+
import { Tracer } from '@aws-lambda-powertools/tracer';
3440

35-
## Getting started
41+
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
3642

37-
Find the complete project's [documentation here](https://docs.powertools.aws.dev/lambda/typescript).
43+
class Lambda implements LambdaInterface {
44+
// Decorate your handler class method
45+
@tracer.captureLambdaHandler()
46+
public async handler(_event: unknown, _context: unknown): Promise<void> {
47+
tracer.getSegment();
48+
}
49+
}
3850

39-
### Installation
51+
const handlerClass = new Lambda();
52+
export const handler = handlerClass.handler.bind(handlerClass);
53+
```
54+
55+
or using middy:
56+
57+
```ts
58+
import { Tracer } from '@aws-lambda-powertools/tracer';
59+
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer/middleware';
60+
import middy from '@middy/core';
4061

41-
The Powertools for AWS Lambda (TypeScript) utilities follow a modular approach, similar to the official [AWS SDK v3 for JavaScript](https://github.com/aws/aws-sdk-js-v3).
42-
Each TypeScript utility is installed as standalone NPM package.
62+
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
4363

44-
Install all three core utilities at once with this single command:
64+
const lambdaHandler = async (
65+
_event: unknown,
66+
_context: unknown
67+
): Promise<void> => {
68+
tracer.putAnnotation('successfulBooking', true);
69+
};
4570

46-
```shell
47-
npm install @aws-lambda-powertools/logger @aws-lambda-powertools/tracer @aws-lambda-powertools/metrics
71+
// Wrap the handler with middy
72+
export const handler = middy(lambdaHandler)
73+
// Use the middleware by passing the Tracer instance as a parameter
74+
.use(captureLambdaHandler(tracer));
4875
```
4976

50-
Or refer to the installation guide of each utility:
77+
### Capture AWS SDK clients
78+
79+
To capture AWS SDK clients, you can use the `captureAWSv3Client` method:
80+
81+
```ts
82+
import { Tracer } from '@aws-lambda-powertools/tracer';
83+
import { SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
5184

52-
👉 [Installation guide for the **Tracer** utility](https://docs.powertools.aws.dev/lambda/typescript/latest/core/tracer#getting-started)
85+
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
86+
// Instrument the AWS SDK client
87+
const client = tracer.captureAWSv3Client(new SecretsManagerClient({}));
5388

54-
👉 [Installation guide for the **Logger** utility](https://docs.powertools.aws.dev/lambda/typescript/latest/core/logger#getting-started)
89+
export default client;
90+
```
91+
92+
### Add metadata and annotations
5593

56-
👉 [Installation guide for the **Metrics** utility](https://docs.powertools.aws.dev/lambda/typescript/latest/core/metrics#getting-started)
94+
You can add metadata and annotations to trace:
5795

58-
👉 [Installation guide for the **Parameters** utility](https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/parameters/#getting-started)
96+
```ts
5997

60-
### Examples
98+
import { Tracer } from '@aws-lambda-powertools/tracer';
6199

62-
You can find examples of how to use Powertools for AWS Lambda (TypeScript) in the [examples](https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/examples/app) directory. The application is a simple REST API that can be deployed via either AWS CDK or AWS SAM.
100+
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
63101

64-
### Demo applications
102+
export const handler = async (
103+
_event: unknown,
104+
_context: unknown
105+
): Promise<void> => {
106+
const handlerSegment = tracer.getSegment()?.addNewSubsegment('### handler');
107+
handlerSegment && tracer.setSegment(handlerSegment);
65108

66-
The [Serverless TypeScript Demo](https://github.com/aws-samples/serverless-typescript-demo) shows how to use Powertools for AWS Lambda (TypeScript).
67-
You can find instructions on how to deploy and load test this application in the [repository](https://github.com/aws-samples/serverless-typescript-demo).
109+
tracer.putMetadata('paymentResponse', {
110+
foo: 'bar',
111+
});
112+
tracer.putAnnotation('successfulBooking', true);
68113

69-
The [AWS Lambda performance tuning](https://github.com/aws-samples/optimizations-for-lambda-functions) repository also uses Powertools for AWS Lambda (TypeScript) as well as demonstrating other performance tuning techniques for Lambda functions written in TypeScript.
114+
handlerSegment?.close();
115+
handlerSegment && tracer.setSegment(handlerSegment?.parent);
116+
};
117+
```
70118

71119
## Contribute
72120

73-
If you are interested in contributing to this project, please refer to our [Contributing Guidelines](https://github.com/aws-powertools/powertools-lambda-typescript/blob/main/CONTRIBUTING.md).
121+
If you are interested in contributing to this project, please refer to
122+
our [Contributing Guidelines](https://github.com/aws-powertools/powertools-lambda-typescript/blob/main/CONTRIBUTING.md).
74123

75124
## Roadmap
76125

77126
The roadmap of Powertools for AWS Lambda (TypeScript) is driven by customers’ demand.
78-
Help us prioritize upcoming functionalities or utilities by [upvoting existing RFCs and feature requests](https://github.com/aws-powertools/powertools-lambda-typescript/issues), or [creating new ones](https://github.com/aws-powertools/powertools-lambda-typescript/issues/new/choose), in this GitHub repository.
127+
Help us prioritize upcoming functionalities or utilities
128+
by [upvoting existing RFCs and feature requests](https://github.com/aws-powertools/powertools-lambda-typescript/issues),
129+
or [creating new ones](https://github.com/aws-powertools/powertools-lambda-typescript/issues/new/choose), in this GitHub
130+
repository.
79131

80132
## Connect
81133

@@ -86,7 +138,10 @@ Help us prioritize upcoming functionalities or utilities by [upvoting existing R
86138

87139
### Becoming a reference customer
88140

89-
Knowing which companies are using this library is important to help prioritize the project internally. If your company is using Powertools for AWS Lambda (TypeScript), you can request to have your name and logo added to the README file by raising a [Support Powertools for AWS Lambda (TypeScript) (become a reference)](https://github.com/aws-powertools/powertools-lambda-typescript/issues/new?assignees=&labels=customer-reference&template=support_powertools.yml&title=%5BSupport+Lambda+Powertools%5D%3A+%3Cyour+organization+name%3E) issue.
141+
Knowing which companies are using this library is important to help prioritize the project internally. If your company
142+
is using Powertools for AWS Lambda (TypeScript), you can request to have your name and logo added to the README file by
143+
raising a [Support Powertools for AWS Lambda (TypeScript) (become a reference)](https://s12d.com/become-reference-pt-ts)
144+
issue.
90145

91146
The following companies, among others, use Powertools:
92147

@@ -108,15 +163,16 @@ The following companies, among others, use Powertools:
108163

109164
### Sharing your work
110165

111-
Share what you did with Powertools for AWS Lambda (TypeScript) 💞💞. Blog post, workshops, presentation, sample apps and others. Check out what the community has already shared about Powertools for AWS Lambda (TypeScript) [here](https://docs.powertools.aws.dev/lambda/typescript/latest/we_made_this).
166+
Share what you did with Powertools for AWS Lambda (TypeScript) 💞💞. Blog post, workshops, presentation, sample apps and
167+
others. Check out what the community has already shared about Powertools for AWS Lambda (
168+
TypeScript) [here](https://docs.powertools.aws.dev/lambda/typescript/latest/we_made_this).
112169

113170
### Using Lambda Layer
114171

115-
This helps us understand who uses Powertools for AWS Lambda (TypeScript) in a non-intrusive way, and helps us gain future investments for other Powertools for AWS Lambda languages. When [using Layers](#lambda-layers), you can add Powertools for AWS Lambda (TypeScript) as a dev dependency (or as part of your virtual env) to not impact the development process.
116-
117-
## Credits
118-
119-
Credits for the Powertools for AWS Lambda (TypeScript) idea go to [DAZN](https://github.com/getndazn) and their [DAZN Lambda Powertools](https://github.com/getndazn/dazn-lambda-powertools/).
172+
This helps us understand who uses Powertools for AWS Lambda (TypeScript) in a non-intrusive way, and helps us gain
173+
future investments for other Powertools for AWS Lambda languages.
174+
When [using Layers](https://docs.powertools.aws.dev/lambda/typescript/latest/#lambda-layer), you can add Powertools as a
175+
dev dependency to not impact the development process.
120176

121177
## License
122178

0 commit comments

Comments
 (0)