You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/metrics/README.md
+44-80
Original file line number
Diff line number
Diff line change
@@ -1,15 +1,14 @@
1
-
# Powertools for AWS Lambda (TypeScript)<!-- omit in toc -->
1
+
# Powertools for AWS Lambda (TypeScript)
2
2
3
3
Powertools for AWS Lambda (TypeScript) is a developer toolkit to implement Serverless [best practices and increase developer velocity](https://docs.powertools.aws.dev/lambda/typescript/latest/#features).
4
4
5
5
You can use the library in both TypeScript and JavaScript code bases.
6
6
7
-
-[Intro](#intro)
8
7
-[Usage](#usage)
9
-
-[Basic usage](#basic-usage)
10
8
-[Flushing metrics](#flushing-metrics)
11
9
-[Capturing cold start as a metric](#capturing-cold-start-as-a-metric)
@@ -19,134 +18,99 @@ You can use the library in both TypeScript and JavaScript code bases.
19
18
-[Using Lambda Layer](#using-lambda-layer)
20
19
-[License](#license)
21
20
22
-
## Intro
23
-
24
21
## Usage
25
22
23
+
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).
24
+
26
25
To get started, install the library by running:
27
26
28
27
```sh
29
28
npm i @aws-lambda-powertools/metrics
30
29
```
31
30
32
-
### Basic usage
31
+
After initializing the Metrics class, you can add metrics using the [https://docs.powertools.aws.dev/lambda/typescript/latest/core/metrics/#creating-metrics](`addMetric()`) method. The metrics are stored in a buffer and are flushed when calling [https://docs.powertools.aws.dev/lambda/typescript/latest/core/metrics/#flushing-metrics](`publishStoredMetrics()`).
33
32
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).
33
+
Each metric can also have dimensions and metadata added to it.
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:
53
+
As you finish adding all your metrics, you need to serialize and "flush them" by calling `publishStoredMetrics()`, which will emit the metrics to stdout in the Embedded Metric Format (EMF). The metrics are then picked up by the Lambda runtime and sent to CloudWatch.
57
54
58
-
- manually by calling `publishStoredMetrics()` at the end of your Lambda function
55
+
The `publishStoredMetrics()`method is synchronous and will block the event loop until the metrics are flushed. If you want Metrics to flush automatically at the end of your Lambda function, you can use the `@logMetrics()` decorator or the `logMetrics()` middleware.
With Metrics, you can capture cold start as a metric by calling the `captureColdStartMetric()` method. This method will add a metric with the name `ColdStart` and the value `1` to the metrics buffer.
76
60
77
-
- middy compatible middleware `logMetrics()`
61
+
This metric is flushed automatically as soon as the method is called, to ensure that the cold start is captured regardless of whether the metrics are flushed manually or automatically.
Note that we don't emit a `ColdStart` metric with value `0` when the function is warm, as this would result in a high volume of metrics being emitted to CloudWatch, so you'll need to rely on the absence of the `ColdStart` metric to determine if the function is warm.
77
+
78
+
### Class method decorator
79
+
80
+
If you are using TypeScript and are comfortable with writing classes, you can use the `@logMetrics()` decorator to automatically flush metrics at the end of your Lambda function as well as configure additional options such as throwing an error if no metrics are added, capturing cold start as a metric, and more.
Decorators are a Stage 3 proposal for JavaScript and are not yet part of the ECMAScript standard. The current implmementation in this library is based on the legacy TypeScript decorator syntax enabled by the [`experimentalDecorators` flag](https://www.typescriptlang.org/tsconfig/#experimentalDecorators) set to `true` in the `tsconfig.json` file.
If instead you are using [Middy.js](http://middy.js.org) and prefer to use middleware, you can use the `@logMetrics()` middleware to do the same as the class method decorator.
145
109
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.
110
+
The `@logMetrics()` middleware can be used with Middy.js to automatically flush metrics at the end of your Lambda function as well as configure additional options such as throwing an error if no metrics are added, capturing cold start as a metric, and set default dimensions.
The `logMetrics()` middleware is compatible with `@middy/[email protected]` and above.
132
+
169
133
## Contribute
170
134
171
135
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).
0 commit comments