Skip to content

Commit f8cf705

Browse files
sarageriondreamorosiijemmyflochaz
authored
feat(metrics): logMetrics middleware (#338)
* feat(metric): add logic, unit tests for the middy middleware * chore(metrics): lint changes * docs(metrics): logMetric middleware and examples * test(metrics): add array input for middleware * Update packages/metrics/package.json Co-authored-by: Andrea Amorosi <[email protected]> * Update docs/core/metrics.md Co-authored-by: ijemmy <[email protected]> * Update docs/core/metrics.md Co-authored-by: ijemmy <[email protected]> * Update docs/core/metrics.md Co-authored-by: ijemmy <[email protected]> * Update docs/core/metrics.md Co-authored-by: ijemmy <[email protected]> * improv(metrics): improve docs, examples, naming * Update packages/metrics/examples/decorator/default-dimensions.ts Co-authored-by: ijemmy <[email protected]> * Update packages/metrics/examples/decorator/empty-metrics.ts Co-authored-by: ijemmy <[email protected]> * Update docs/core/metrics.md Co-authored-by: Florian Chazal <[email protected]> * Update docs/core/metrics.md Co-authored-by: Florian Chazal <[email protected]> * Update docs/core/metrics.md Co-authored-by: Florian Chazal <[email protected]> * Update docs/core/metrics.md Co-authored-by: Florian Chazal <[email protected]> * Update docs/core/metrics.md Co-authored-by: Florian Chazal <[email protected]> * Update packages/metrics/examples/decorator/manual-flushing.ts Co-authored-by: ijemmy <[email protected]> * improv(metrics): logMetricsAfterOrError middleware, examples update * improv(metrics): logMetricsAfterOrError middleware, examples update * improv(metrics): logMetricsAfterOrError middleware, examples update * tests(metrics): fix handlers type * fix(metrics): examples file path * revert(metrics): jest groups * chore(metrics): rename examples script, prepending 'decorator' * fix(metrics): removed empty lint file * ci(all): exclude examples CDK for linting * chore(all): lint fix for CDK examples * test(metrics): added metrics middleware jest group Co-authored-by: Andrea Amorosi <[email protected]> Co-authored-by: ijemmy <[email protected]> Co-authored-by: Florian Chazal <[email protected]>
1 parent 1486333 commit f8cf705

38 files changed

+1028
-585
lines changed

Diff for: docs/core/metrics.md

+252-97
Large diffs are not rendered by default.

Diff for: packages/metrics/.eslintrc.json

-231
This file was deleted.

Diff for: packages/metrics/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Metrics has two global settings that will be used across all metrics emitted:
2424

2525
|Setting|Description|Environment Variable|Constructor Parameter|
2626
|---|---|---|---|
27-
|Metric namespace|Logical container where all metrics will be placed e.g. ServerlessAirline|POWERTOOLS_METRICS_NAMESPACE|namespace|
27+
|Metric namespace|Logical container where all metrics will be placed e.g. serverlessAirline|POWERTOOLS_METRICS_NAMESPACE|namespace|
2828
|Service|Optionally, sets service metric dimension across all metrics e.g. payment|POWERTOOLS_SERVICE_NAME|service|
2929

3030
```typescript

Diff for: packages/metrics/examples/cold-start.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
22
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
3-
import { LambdaInterface } from './utils/lambda/LambdaInterface';
43
import { populateEnvironmentVariables } from '../tests/helpers';
5-
import { Callback, Context } from 'aws-lambda/handler';
64
import { Metrics, MetricUnits } from '../src';
5+
import middy from '@middy/core';
6+
import { logMetrics } from '../src/middleware/middy';
77

88
// Populate runtime
99
populateEnvironmentVariables();
@@ -12,15 +12,15 @@ process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';
1212

1313
const metrics = new Metrics();
1414

15-
class Lambda implements LambdaInterface {
15+
const lambdaHandler = async (): Promise<void> => {
1616

17-
@metrics.logMetrics({ captureColdStartMetric: true })
18-
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
19-
metrics.addDimension('OuterDimension', 'true');
20-
metrics.addMetric('test-metric', MetricUnits.Count, 10);
21-
}
17+
metrics.addDimension('custom-dimension', 'true');
18+
metrics.addMetric('test-metric', MetricUnits.Count, 10);
2219

23-
}
20+
};
2421

25-
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
26-
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked again!'));
22+
const handlerWithMiddleware = middy(lambdaHandler)
23+
.use(logMetrics(metrics, { captureColdStartMetric: true }));
24+
25+
handlerWithMiddleware(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
26+
handlerWithMiddleware(dummyEvent, dummyContext, () => console.log('Lambda invoked again!'));

Diff for: packages/metrics/examples/constructor-options.ts

+14-10
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
22
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
3+
import { populateEnvironmentVariables } from '../tests/helpers';
34
import { Metrics, MetricUnits } from '../src';
4-
import { LambdaInterface } from "./utils/lambda";
5-
import { Callback, Context } from "aws-lambda/handler";
5+
import middy from '@middy/core';
6+
import { logMetrics } from '../src/middleware/middy';
7+
8+
// Populate runtime
9+
populateEnvironmentVariables();
10+
// Additional runtime variables
11+
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';
612

713
const metrics = new Metrics({
814
namespace: 'hello-world-constructor',
915
service: 'hello-world-service-constructor'
1016
});
1117

12-
class Lambda implements LambdaInterface {
13-
14-
@metrics.logMetrics()
15-
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
16-
metrics.addMetric('test-metric', MetricUnits.Count, 10);
18+
const lambdaHandler = async (): Promise<void> => {
19+
metrics.addMetric('test-metric', MetricUnits.Count, 10);
20+
};
1721

18-
}
22+
const handlerWithMiddleware = middy(lambdaHandler)
23+
.use(logMetrics(metrics));
1924

20-
}
21-
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
25+
handlerWithMiddleware(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));

Diff for: packages/metrics/examples/decorator/cold-start.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
2+
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
3+
import { LambdaInterface } from './../utils/lambda/LambdaInterface';
4+
import { populateEnvironmentVariables } from '../../tests/helpers';
5+
import { Callback, Context } from 'aws-lambda/handler';
6+
import { Metrics, MetricUnits } from '../../src';
7+
8+
// Populate runtime
9+
populateEnvironmentVariables();
10+
// Additional runtime variables
11+
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world';
12+
13+
const metrics = new Metrics();
14+
15+
class Lambda implements LambdaInterface {
16+
17+
@metrics.logMetrics({ captureColdStartMetric: true })
18+
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
19+
metrics.addDimension('OuterDimension', 'true');
20+
metrics.addMetric('test-metric', MetricUnits.Count, 10);
21+
}
22+
23+
}
24+
25+
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
26+
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked again!'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as dummyEvent from '../../../../tests/resources/events/custom/hello-world.json';
2+
import { context as dummyContext } from '../../../../tests/resources/contexts/hello-world';
3+
import { Metrics, MetricUnits } from '../../src';
4+
import { LambdaInterface } from './../utils/lambda';
5+
import { Callback, Context } from 'aws-lambda/handler';
6+
7+
const metrics = new Metrics({
8+
namespace: 'hello-world-constructor',
9+
service: 'hello-world-service-constructor'
10+
});
11+
12+
class Lambda implements LambdaInterface {
13+
14+
@metrics.logMetrics()
15+
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
16+
metrics.addMetric('test-metric', MetricUnits.Count, 10);
17+
18+
}
19+
20+
}
21+
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { populateEnvironmentVariables } from '../../tests/helpers';
2+
3+
// Populate runtime
4+
populateEnvironmentVariables();
5+
// Additional runtime variables
6+
process.env.POWERTOOLS_METRICS_NAMESPACE = 'hello-world-constructor';
7+
process.env.POWERTOOLS_SERVICE_NAME = 'hello-world-service-constructor';
8+
9+
import * as dummyEvent from '../../../tests/resources/events/custom/hello-world.json';
10+
import { context as dummyContext } from '../../../tests/resources/contexts/hello-world';
11+
import { LambdaInterface } from './utils/lambda/LambdaInterface';
12+
import { Callback, Context } from 'aws-lambda/handler';
13+
import { Metrics, MetricUnits } from '../src';
14+
15+
const metrics = new Metrics({ defaultDimensions:{ 'application': 'hello-world' } });
16+
17+
class Lambda implements LambdaInterface {
18+
19+
@metrics.logMetrics()
20+
public handler<TEvent, TResult>(_event: TEvent, _context: Context, _callback: Callback<TResult>): void | Promise<TResult> {
21+
metrics.addMetric('test-metric', MetricUnits.Count, 10);
22+
23+
}
24+
25+
}
26+
27+
new Lambda().handler(dummyEvent, dummyContext, () => console.log('Lambda invoked!'));

0 commit comments

Comments
 (0)