@middy/cloudwatch-metrics 5.1.0 → 5.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +37 -34
  2. package/package.json +3 -3
package/index.js CHANGED
@@ -1,35 +1,38 @@
1
- import awsEmbeddedMetrics from 'aws-embedded-metrics';
2
- const defaults = {};
3
- const cloudwatchMetricsMiddleware = (opts = {})=>{
4
- const { namespace, dimensions } = {
5
- ...defaults,
6
- ...opts
7
- };
8
- const cloudwatchMetricsBefore = (request)=>{
9
- const metrics = awsEmbeddedMetrics.createMetricsLogger();
10
- if (namespace) {
11
- metrics.setNamespace(namespace);
12
- }
13
- if (dimensions) {
14
- metrics.setDimensions(dimensions);
15
- }
16
- Object.assign(request.context, {
17
- metrics
18
- });
19
- };
20
- const cloudwatchMetricsAfter = async (request)=>{
21
- await request.context.metrics.flush();
22
- };
23
- const cloudwatchMetricsOnError = async (request)=>{
24
- try {
25
- await cloudwatchMetricsAfter(request);
26
- } catch (e) {}
27
- };
28
- return {
29
- before: cloudwatchMetricsBefore,
30
- after: cloudwatchMetricsAfter,
31
- onError: cloudwatchMetricsOnError
32
- };
33
- };
34
- export default cloudwatchMetricsMiddleware;
1
+ import awsEmbeddedMetrics from 'aws-embedded-metrics'
35
2
 
3
+ const defaults = {}
4
+
5
+ const cloudwatchMetricsMiddleware = (opts = {}) => {
6
+ const { namespace, dimensions } = { ...defaults, ...opts }
7
+ const cloudwatchMetricsBefore = (request) => {
8
+ const metrics = awsEmbeddedMetrics.createMetricsLogger()
9
+
10
+ // If not set, defaults to aws-embedded-metrics
11
+ if (namespace) {
12
+ metrics.setNamespace(namespace)
13
+ }
14
+
15
+ // If not set, keeps defaults as defined here https://github.com/awslabs/aws-embedded-metrics-node/#configuration
16
+ if (dimensions) {
17
+ metrics.setDimensions(dimensions)
18
+ }
19
+ Object.assign(request.context, { metrics })
20
+ }
21
+
22
+ const cloudwatchMetricsAfter = async (request) => {
23
+ await request.context.metrics.flush()
24
+ }
25
+ const cloudwatchMetricsOnError = async (request) => {
26
+ try {
27
+ await cloudwatchMetricsAfter(request)
28
+ } catch (e) {}
29
+ }
30
+
31
+ return {
32
+ before: cloudwatchMetricsBefore,
33
+ after: cloudwatchMetricsAfter,
34
+ onError: cloudwatchMetricsOnError
35
+ }
36
+ }
37
+
38
+ export default cloudwatchMetricsMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/cloudwatch-metrics",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "Embedded CloudWatch metrics middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -66,8 +66,8 @@
66
66
  "aws-embedded-metrics": "4.1.1"
67
67
  },
68
68
  "devDependencies": {
69
- "@middy/core": "5.1.0",
69
+ "@middy/core": "5.2.0",
70
70
  "@types/aws-lambda": "^8.10.101"
71
71
  },
72
- "gitHead": "bbdaf5843914921804ba085dd58117273febe6b5"
72
+ "gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
73
73
  }