Skip to content

Commit 0e5422a

Browse files
authored
docs(metrics): review API docs & README (#2983)
1 parent 804d682 commit 0e5422a

File tree

6 files changed

+224
-72
lines changed

6 files changed

+224
-72
lines changed

packages/idempotency/README.md

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1-
# Powertools for AWS Lambda (TypeScript) - Idempotency Utility
1+
# Powertools for AWS Lambda (TypeScript) - Idempotency Utility <!-- omit in toc -->
22

33
Powertools for AWS Lambda (TypeScript) is a developer toolkit to implement
44
Serverless [best practices and increase developer velocity](https://docs.powertools.aws.dev/lambda/typescript/latest/#features).
55

66
You can use the package in both TypeScript and JavaScript code bases.
77

8+
- [Intro](#intro)
9+
- [Usage](#usage)
10+
- [Function wrapper](#function-wrapper)
11+
- [Decorator](#decorator)
12+
- [Middy middleware](#middy-middleware)
13+
- [DynamoDB persistence layer](#dynamodb-persistence-layer)
14+
- [Contribute](#contribute)
15+
- [Roadmap](#roadmap)
16+
- [Connect](#connect)
17+
- [How to support Powertools for AWS Lambda (TypeScript)?](#how-to-support-powertools-for-aws-lambda-typescript)
18+
- [Becoming a reference customer](#becoming-a-reference-customer)
19+
- [Sharing your work](#sharing-your-work)
20+
- [Using Lambda Layer](#using-lambda-layer)
21+
- [License](#license)
22+
823
## Intro
924

1025
This package provides a utility to implement idempotency in your Lambda functions.

packages/metrics/README.md

+139-42
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,167 @@ 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+
- [Flushing metrics](#flushing-metrics)
11+
- [Capturing cold start as a metric](#capturing-cold-start-as-a-metric)
12+
- [Adding metadata](#adding-metadata)
1813
- [Contribute](#contribute)
1914
- [Roadmap](#roadmap)
2015
- [Connect](#connect)
2116
- [How to support Powertools for AWS Lambda (TypeScript)?](#how-to-support-powertools-for-aws-lambda-typescript)
2217
- [Becoming a reference customer](#becoming-a-reference-customer)
2318
- [Sharing your work](#sharing-your-work)
2419
- [Using Lambda Layer](#using-lambda-layer)
25-
- [Credits](#credits)
2620
- [License](#license)
2721

28-
## Features
22+
## Intro
23+
24+
## Usage
25+
26+
To get started, install the library by running:
27+
28+
```sh
29+
npm i @aws-lambda-powertools/metrics
30+
```
31+
32+
### Basic usage
2933

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
34+
The library provides a utility function to emit metrics to CloudWatch using [Embedded Metric Format (EMF)](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format.html).
3435

35-
## Getting started
36+
```ts
37+
import { MetricUnit, Metrics } from '@aws-lambda-powertools/metrics';
3638

37-
Find the complete project's [documentation here](https://docs.powertools.aws.dev/lambda/typescript).
39+
const metrics = new Metrics({
40+
namespace: 'serverlessAirline',
41+
serviceName: 'orders',
42+
});
3843

39-
### Installation
44+
export const handler = async (
45+
_event: unknown,
46+
_context: unknown
47+
): Promise<void> => {
48+
metrics.addMetric('successfulBooking', MetricUnit.Count, 1);
49+
};
50+
```
51+
52+
### Flushing metrics
53+
54+
As you finish adding all your metrics, you need to serialize and "flush them" by calling publishStoredMetrics(). This will print the metrics to standard output.
55+
56+
You can flush metrics automatically using one of the following methods:
4057

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.
58+
- manually by calling `publishStoredMetrics()` at the end of your Lambda function
4359

44-
Install all three core utilities at once with this single command:
60+
```ts
61+
import { MetricUnit, Metrics } from '@aws-lambda-powertools/metrics';
4562

46-
```shell
47-
npm install @aws-lambda-powertools/logger @aws-lambda-powertools/tracer @aws-lambda-powertools/metrics
63+
const metrics = new Metrics({
64+
namespace: 'serverlessAirline',
65+
serviceName: 'orders',
66+
});
67+
68+
export const handler = async (
69+
_event: unknown,
70+
_context: unknown
71+
): Promise<void> => {
72+
metrics.addMetric('successfulBooking', MetricUnit.Count, 1);
73+
metrics.publishStoredMetrics();
74+
};
4875
```
4976

50-
Or refer to the installation guide of each utility:
77+
- middy compatible middleware `logMetrics()`
78+
79+
```ts
80+
import { MetricUnit, Metrics } from '@aws-lambda-powertools/metrics';
81+
import { logMetrics } from '@aws-lambda-powertools/metrics/middleware';
82+
import middy from '@middy/core';
5183

52-
👉 [Installation guide for the **Tracer** utility](https://docs.powertools.aws.dev/lambda/typescript/latest/core/tracer#getting-started)
84+
const metrics = new Metrics({
85+
namespace: 'serverlessAirline',
86+
serviceName: 'orders',
87+
});
5388

54-
👉 [Installation guide for the **Logger** utility](https://docs.powertools.aws.dev/lambda/typescript/latest/core/logger#getting-started)
89+
const lambdaHandler = async (
90+
_event: unknown,
91+
_context: unknown
92+
): Promise<void> => {
93+
metrics.addMetric('successfulBooking', MetricUnit.Count, 1);
94+
};
5595

56-
👉 [Installation guide for the **Metrics** utility](https://docs.powertools.aws.dev/lambda/typescript/latest/core/metrics#getting-started)
96+
export const handler = middy(lambdaHandler).use(logMetrics(metrics));
97+
```
5798

58-
👉 [Installation guide for the **Parameters** utility](https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/parameters/#getting-started)
99+
- using decorator `@logMetrics()`
59100

60-
### Examples
101+
```ts
102+
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
103+
import { MetricUnit, Metrics } from '@aws-lambda-powertools/metrics';
61104

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.
105+
const metrics = new Metrics({
106+
namespace: 'serverlessAirline',
107+
serviceName: 'orders',
108+
});
63109

64-
### Demo applications
110+
class Lambda implements LambdaInterface {
111+
@metrics.logMetrics()
112+
public async handler(_event: unknown, _context: unknown): Promise<void> {
113+
metrics.addMetric('successfulBooking', MetricUnit.Count, 1);
114+
}
115+
}
65116

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).
117+
const handlerClass = new Lambda();
118+
export const handler = handlerClass.handler.bind(handlerClass);
119+
```
68120

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.
121+
Using the Middy middleware or decorator will automatically validate, serialize, and flush all your metrics.
122+
123+
### Capturing cold start as a metric
124+
125+
You can optionally capture cold start metrics with the logMetrics middleware or decorator via the captureColdStartMetric param.
126+
127+
```ts
128+
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
129+
import { MetricUnit, Metrics } from '@aws-lambda-powertools/metrics';
130+
131+
const metrics = new Metrics({
132+
namespace: 'serverlessAirline',
133+
serviceName: 'orders',
134+
});
135+
136+
export class MyFunction implements LambdaInterface {
137+
@metrics.logMetrics({ captureColdStartMetric: true })
138+
public async handler(_event: unknown, _context: unknown): Promise<void> {
139+
metrics.addMetric('successfulBooking', MetricUnit.Count, 1);
140+
}
141+
}
142+
```
143+
144+
### Adding metadata
145+
146+
You can add high-cardinality data as part of your Metrics log with the `addMetadata` method. This is useful when you want to search highly contextual information along with your metrics in your logs.
147+
148+
```ts
149+
import { MetricUnit, Metrics } from '@aws-lambda-powertools/metrics';
150+
import { logMetrics } from '@aws-lambda-powertools/metrics/middleware';
151+
import middy from '@middy/core';
152+
153+
const metrics = new Metrics({
154+
namespace: 'serverlessAirline',
155+
serviceName: 'orders',
156+
});
157+
158+
const lambdaHandler = async (
159+
_event: unknown,
160+
_context: unknown
161+
): Promise<void> => {
162+
metrics.addMetric('successfulBooking', MetricUnit.Count, 1);
163+
metrics.addMetadata('bookingId', '7051cd10-6283-11ec-90d6-0242ac120003');
164+
};
165+
166+
export const handler = middy(lambdaHandler).use(logMetrics(metrics));
167+
```
70168

71169
## Contribute
72170

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

87185
### Becoming a reference customer
88186

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.
187+
Knowing which companies are using this library is important to help prioritize the project internally. If your company
188+
is using Powertools for AWS Lambda (TypeScript), you can request to have your name and logo added to the README file by
189+
raising a [Support Powertools for AWS Lambda (TypeScript) (become a reference)](https://s12d.com/become-reference-pt-ts)
190+
issue.
90191

91192
The following companies, among others, use Powertools:
92193

@@ -112,11 +213,7 @@ Share what you did with Powertools for AWS Lambda (TypeScript) 💞💞. Blog po
112213

113214
### Using Lambda Layer
114215

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/).
216+
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](https://docs.powertools.aws.dev/lambda/typescript/latest/#lambda-layer), you can add Powertools as a dev dependency to not impact the development process.
120217

121218
## License
122219

0 commit comments

Comments
 (0)