https://github.com/shutterstock/aws-embedded-metrics-flatten
Helper for aws-embedded-metrics npm module - Optionally disable emitting metrics and consolidate / flatten thousands of increments on Count metrics into a single line per run.
https://github.com/shutterstock/aws-embedded-metrics-flatten
aws-cloudwatch-metrics embedded-metrics flatten optional
Last synced: about 1 year ago
JSON representation
Helper for aws-embedded-metrics npm module - Optionally disable emitting metrics and consolidate / flatten thousands of increments on Count metrics into a single line per run.
- Host: GitHub
- URL: https://github.com/shutterstock/aws-embedded-metrics-flatten
- Owner: shutterstock
- License: mit
- Created: 2023-06-29T19:11:48.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-06-11T20:15:39.000Z (almost 2 years ago)
- Last Synced: 2025-02-21T11:16:29.024Z (over 1 year ago)
- Topics: aws-cloudwatch-metrics, embedded-metrics, flatten, optional
- Language: JavaScript
- Homepage: https://tech.shutterstock.com/aws-embedded-metrics-flatten/
- Size: 138 KB
- Stars: 1
- Watchers: 5
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://www.npmjs.com/package/@shutterstock/aws-embedded-metrics-flatten) [](https://opensource.org/licenses/MIT) [](https://tech.shutterstock.com/aws-embedded-metrics-flatten/) [](https://github.com/shutterstock/aws-embedded-metrics-flatten/actions/workflows/ci.yml) [](https://github.com/shutterstock/aws-embedded-metrics-flatten/actions/workflows/publish.yml) [](https://github.com/shutterstock/aws-embedded-metrics-flatten/actions/workflows/docs.yml)
# Overview
Extensions for the [aws-embedded-metrics](https://www.npmjs.com/package/aws-embedded-metrics) npm module, adding flatcount metrics that emit a total count instead of an array of each individual count, and the ability to disable metrics without removing metrics method calls.
## Installation
The package is available on npm as [@shutterstock/aws-embedded-metrics-flatten](https://www.npmjs.com/package/@shutterstock/aws-embedded-metrics-flatten)
`npm i @shutterstock/aws-embedded-metrics-flatten`
## Importing
```typescript
import { FlatCountMetrics, metricScopeDummy } from '@shutterstock/aws-embedded-metrics-flatten';
```
# Features
- Flattens count metrics into a single emitted line
- This works around the max length limitation of CloudWatch Embedded Metrics log messages
- Rather than emitting an array, such as `[1, 1, 1, 1, 1]`, it emits a single count of `[5]`, dramatically reducing the size of the log message when counting thousands of iterations in a Lambda function
- Gives the ability to disable the metrics without having to protect each metric emit with a conditional
# Flattening Count Metrics
CloudWatch embedded metrics (parsed from CloudWatch logs) have a limit of something like 1,000 items in a metrics log statement. Metrics beyond that point will be ignored.
`aws-embedded-metrics` will emit metrics with `10,000` calls to `.putMetric('MyMetric', 1, metricUnit.Count);` as an array with 10,000 elements all with the value of `1`... and about `9,000` of those will get ignored as a result.
# Optional Metrics
`metricScopeDummy` is a replacement for `metricScope` that will pass an `MetricsLoggerDummy` instance instead of a `MetricsLogger` instance to the callback function. `MetricsLoggerDummy` implements the same interface as `MetricsLogger` but it does not accumulate data and does not emit any logs.
Coupled with a configuration setting / env var it is possible to emit metrics or not emit metrics by changing which function is used to create the `metrics` object.
# Contributing
## Setting up Build Environment
- `nvm use`
- `npm i`
- `npm run build`
- `npm run lint`
- `npm run test`
## Running Examples
### metrics-flatten
1. `npm run example:metrics-flatten`
1. Will print 2 lines of metrics
2. `MyFlatMetric` will have a single value of `10000`
2. `FLATTEN_METRICS=false npm run example:metrics-flatten`
1. Will print many lines of metrics
2. `MyFlatMetric` will have 10,000 values of `1` across many different lines
### metrics-optional
1. `npm run example:metrics-optional`
1. Metrics will print
2. `EMIT_METRICS=false npm run example:metrics-optional`
1. Metrics will not print