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
+36-91Lines changed: 36 additions & 91 deletions
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,80 @@ 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
33
-
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).
31
+
After initializing the Metrics class, you can add metrics using the [Metrics.addMetric](`addMetric()`) method. The metrics are stored in a buffer and are flushed when calling [Metrics.publishStoredMetrics](`publishStoredMetrics()`). Each metric can 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:
57
-
58
-
- manually by calling `publishStoredMetrics()` at the end of your Lambda function
As you finish adding all your metrics, you need to serialize and "flush them" by calling [publishStoredMetrics()](`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.
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
90
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.
91
+
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.
113
+
169
114
## Contribute
170
115
171
116
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