-
Notifications
You must be signed in to change notification settings - Fork 156
feat: add metrics #102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add metrics #102
Changes from 22 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
da493db
Start of metrics:
f98a135
Start of metrics:
21e8986
Initial metrics class
fbd7b94
Adding package-lock
d1cd247
addin first version of metric output
3ac09d7
Basic test
a471489
Fixing erroneous auto linter changeS
ae5b71f
Adding custom dimensions
43bbba9
Typo fix
13918b6
Typo fix
c6e267c
Typo fix
7253017
Adding metadata methods
ea8f92f
Start of documentation
c1dd278
Start of tests
559d448
Merge branch 'main' into feature/metrics
dabd95c
Adding first draft of testS
b8507ee
Merge branch 'main' into feature/metrics
fd0246a
Fixing typo in readme
f3295b3
Early PR notes, typos and better test structure
79126f0
Adding bulk dimension add
578960d
Exposing purge stored metrics function
dd2b56d
Removing unused variable
1b853d1
Adding some more info in the readme
984da53
Typo
4d3f0cd
Fixing readme and moving to RangeError for certain conditions
968826f
Updating tests to reflect new error wording
fdf84ac
Moving stored metrics type definition to types file
664d7a7
Setting default namespace if one isn't passed rather than throwing an…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
# `Metrics` | ||
|
||
|
||
## Usage | ||
|
||
```bash | ||
npm run test | ||
|
||
npm run example:hello-world | ||
npm run example:manual-flushing | ||
npm run example:dimensions | ||
npm run example:default-dimensions | ||
npm run example:empty-metrics | ||
npm run example:single-metric | ||
npm run example:cold-start | ||
``` | ||
|
||
### Getting started | ||
|
||
Metrics has two global settings that will be used across all metrics emitted: | ||
|
||
|Setting|Description|Environment Variable|Constructor Parameter| | ||
|---|---|---|---| | ||
|Metric namespace|Logical container where all metrics will be placed e.g. ServerlessAirline|POWERTOOLS_METRICS_NAMESPACE|namespace| | ||
|Service|Optionally, sets service metric dimension across all metrics e.g. payment|POWERTOOLS_SERVICE_NAME|service| | ||
|
||
```typescript | ||
// Import the library | ||
import { Metrics, MetricUnits } from '../src'; | ||
// When going public, it will be something like: import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics'; | ||
|
||
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world'; | ||
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world-service'; | ||
alan-churley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Instantiate the Logger with default configuration | ||
const metrics = new Metrics(); | ||
|
||
// Add an example Metric | ||
metrics.addMetric('test-metric', MetricUnits.Count, 10); | ||
|
||
//Print resulting data | ||
const metricsObject = metrics.serializeMetrics(); | ||
metrics.clearMetrics(); | ||
console.log(JSON.stringify(metricsObject)); | ||
|
||
``` | ||
|
||
<details> | ||
<summary>Click to expand and see the logs outputs</summary> | ||
|
||
```bash | ||
|
||
{ | ||
"_aws":{ | ||
"Timestamp":1625587915573, | ||
"CloudWatchMetrics": | ||
[ | ||
{ | ||
"Namespace":"hello-world", | ||
"Dimensions":[["service"]], | ||
"Metrics": | ||
[ | ||
{ | ||
"Name":"test-metric", | ||
"Unit":"Count" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"service":"hello-world-service", | ||
"test-metric":10 | ||
} | ||
|
||
``` | ||
</details> | ||
|
||
With decorators: | ||
|
||
```typescript | ||
|
||
import { Metrics, MetricUnits } from '../src'; | ||
|
||
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world'; | ||
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world-service'; | ||
|
||
// Instantiate the Logger with default configuration | ||
const metrics = new Metrics(); | ||
|
||
class Lambda implements LambdaInterface { | ||
|
||
@metrics.logMetrics() | ||
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> { | ||
metrics.addDimension('OuterDimension', 'true'); | ||
metrics.addMetric('test-metric', MetricUnits.Count, 10); | ||
} | ||
|
||
} | ||
|
||
``` | ||
|
||
### Capturing cold start | ||
|
||
By default the cold start metric is not captured, it can however be enabled using a parameter passed to the logMetrics decorator | ||
|
||
|
||
|
||
```typescript | ||
|
||
import { Metrics, MetricUnits } from '../src'; | ||
|
||
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world'; | ||
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world-service'; | ||
|
||
// Instantiate the Logger with default configuration | ||
const metrics = new Metrics(); | ||
|
||
class Lambda implements LambdaInterface { | ||
|
||
@metrics.logMetrics({ captureColdStartMetric: true })) | ||
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> { | ||
metrics.addDimension('OuterDimension', 'true'); | ||
metrics.addMetric('test-metric', MetricUnits.Count, 10); | ||
} | ||
|
||
} | ||
|
||
``` | ||
> Please note, we do not emit a 0 value for the ColdStart metric, for cost reasons | ||
<details> | ||
<summary>Click to expand and see the logs outputs</summary> | ||
|
||
```bash | ||
|
||
{ | ||
"_aws":{ | ||
"Timestamp":1625587915572, | ||
"CloudWatchMetrics": | ||
[ | ||
{ | ||
"Namespace":"hello-world", | ||
"Dimensions":[[ | ||
"service","function_name" | ||
]], | ||
"Metrics": | ||
[ | ||
{ | ||
"Name":"ColdStart", | ||
"Unit":"Count" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"service":"hello-world-service", | ||
"function_name":"foo-bar-function", | ||
"ColdStart":1 | ||
} | ||
{ | ||
"_aws":{ | ||
"Timestamp":1625587915573, | ||
"CloudWatchMetrics": | ||
[ | ||
{ | ||
"Namespace":"hello-world", | ||
"Dimensions":[["service"]], | ||
"Metrics": | ||
[ | ||
{ | ||
"Name":"test-metric", | ||
"Unit":"Count" | ||
} | ||
] | ||
} | ||
] | ||
}, | ||
"service":"hello-world-service", | ||
"test-metric":10 | ||
} | ||
|
||
``` | ||
</details> | ||
If it's a cold start invocation, this feature will: | ||
|
||
- Create a separate EMF blob solely containing a metric named ColdStart | ||
- Add function_name and service dimensions | ||
|
||
This has the advantage of keeping cold start metric separate from your application metrics, where you might have unrelated dimensions. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json'; | ||
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world'; | ||
import { LambdaInterface } from './utils/lambda/LambdaInterface'; | ||
import { populateEnvironmentVariables } from '../tests/helpers'; | ||
import { Callback, Context } from 'aws-lambda/handler'; | ||
import { Metrics, MetricUnits } from '../src'; | ||
|
||
// Populate runtime | ||
populateEnvironmentVariables(); | ||
// Additional runtime variables | ||
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world'; | ||
|
||
const metrics = new Metrics(); | ||
|
||
class Lambda implements LambdaInterface { | ||
|
||
@metrics.logMetrics({ captureColdStartMetric: true }) | ||
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> { | ||
metrics.addDimension('OuterDimension', 'true'); | ||
metrics.addMetric('test-metric', MetricUnits.Count, 10); | ||
} | ||
|
||
} | ||
|
||
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!')); | ||
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked again!')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { populateEnvironmentVariables } from '../tests/helpers'; | ||
|
||
// Populate runtime | ||
populateEnvironmentVariables(); | ||
// Additional runtime variables | ||
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world'; | ||
|
||
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json'; | ||
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world'; | ||
import { LambdaInterface } from './utils/lambda/LambdaInterface'; | ||
import { Callback, Context } from 'aws-lambda/handler'; | ||
import { Metrics, MetricUnits } from '../src'; | ||
|
||
const metrics = new Metrics(); | ||
alan-churley marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
class Lambda implements LambdaInterface { | ||
|
||
@metrics.logMetrics({ defaultDimensions:{ 'application': 'hello-world' } }) | ||
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> { | ||
|
||
metrics.addDimension('environment', 'dev'); | ||
metrics.addDimension('application', 'hello-world-dev'); | ||
saragerion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
metrics.addMetric('test-metric', MetricUnits.Count, 10); | ||
const metricsObject = metrics.serializeMetrics(); | ||
metrics.clearMetrics(); | ||
metrics.clearDimensions(); | ||
metrics.addMetric('new-test-metric', MetricUnits.Count, 5); | ||
console.log(JSON.stringify(metricsObject)); | ||
} | ||
|
||
} | ||
|
||
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { populateEnvironmentVariables } from '../tests/helpers'; | ||
|
||
// Populate runtime | ||
populateEnvironmentVariables(); | ||
// Additional runtime variables | ||
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world'; | ||
|
||
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json'; | ||
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world'; | ||
import { LambdaInterface } from './utils/lambda/LambdaInterface'; | ||
import { Callback, Context } from 'aws-lambda/handler'; | ||
import { Metrics, MetricUnits } from '../src'; | ||
|
||
const metrics = new Metrics(); | ||
|
||
class Lambda implements LambdaInterface { | ||
|
||
@metrics.logMetrics() | ||
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> { | ||
|
||
metrics.addDimension('environment', 'dev'); | ||
metrics.addMetric('test-metric', MetricUnits.Count, 10); | ||
|
||
} | ||
|
||
} | ||
|
||
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { populateEnvironmentVariables } from '../tests/helpers'; | ||
|
||
// Populate runtime | ||
populateEnvironmentVariables(); | ||
// Additional runtime variables | ||
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world'; | ||
|
||
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json'; | ||
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world'; | ||
import { LambdaInterface } from './utils/lambda/LambdaInterface'; | ||
import { Callback, Context } from 'aws-lambda/handler'; | ||
import { Metrics } from '../src'; | ||
|
||
const metrics = new Metrics(); | ||
|
||
class Lambda implements LambdaInterface { | ||
|
||
@metrics.logMetrics({ raiseOnEmptyMetrics: true }) | ||
saragerion marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> { | ||
|
||
} | ||
|
||
} | ||
|
||
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!')); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.