@middy/cloudwatch-metrics 7.2.2 → 7.3.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 (3) hide show
  1. package/index.d.ts +5 -0
  2. package/index.js +26 -7
  3. package/package.json +2 -5
package/index.d.ts CHANGED
@@ -8,6 +8,7 @@ export { MetricsLogger } from "aws-embedded-metrics";
8
8
  export interface Options {
9
9
  namespace?: string;
10
10
  dimensions?: Array<Record<string, string>>;
11
+ onFlushError?: (error: Error) => void;
11
12
  }
12
13
 
13
14
  export type Context = LambdaContext & {
@@ -18,4 +19,8 @@ declare function cloudwatchMetrics(
18
19
  options?: Options,
19
20
  ): middy.MiddlewareObj<unknown, unknown, Error>;
20
21
 
22
+ export declare function cloudwatchMetricsValidateOptions(
23
+ options?: Record<string, unknown>,
24
+ ): void;
25
+
21
26
  export default cloudwatchMetrics;
package/index.js CHANGED
@@ -1,11 +1,24 @@
1
1
  // Copyright 2017 - 2026 will Farrell, Luciano Mammino, and Middy contributors.
2
2
  // SPDX-License-Identifier: MIT
3
+
4
+ import { validateOptions } from "@middy/util";
3
5
  import awsEmbeddedMetrics from "aws-embedded-metrics";
4
6
 
5
- const defaults = {};
7
+ const defaults = {
8
+ onFlushError: undefined,
9
+ };
10
+
11
+ const optionSchema = {
12
+ namespace: "string?",
13
+ dimensions: "array?",
14
+ onFlushError: "function?",
15
+ };
16
+
17
+ export const cloudwatchMetricsValidateOptions = (options) =>
18
+ validateOptions("@middy/cloudwatch-metrics", optionSchema, options);
6
19
 
7
20
  const cloudwatchMetricsMiddleware = (opts = {}) => {
8
- const { namespace, dimensions } = { ...defaults, ...opts };
21
+ const { namespace, dimensions, onFlushError } = { ...defaults, ...opts };
9
22
  const cloudwatchMetricsBefore = async (request) => {
10
23
  const metrics = awsEmbeddedMetrics.createMetricsLogger();
11
24
 
@@ -21,12 +34,18 @@ const cloudwatchMetricsMiddleware = (opts = {}) => {
21
34
  Object.assign(request.context, { metrics });
22
35
  };
23
36
 
24
- const cloudwatchMetricsAfter = async (request) => {
25
- await request.context.metrics?.flush();
26
- };
27
- const cloudwatchMetricsOnError = async (request) => {
28
- await request.context.metrics?.flush();
37
+ const flushMetrics = async (request) => {
38
+ try {
39
+ await request.context.metrics?.flush();
40
+ } catch (err) {
41
+ // Flush errors are swallowed to prevent metrics from crashing the
42
+ // handler. Users who need visibility (e.g., to catch IAM or network
43
+ // misconfigurations) can pass `onFlushError` to observe them.
44
+ onFlushError?.(err);
45
+ }
29
46
  };
47
+ const cloudwatchMetricsAfter = flushMetrics;
48
+ const cloudwatchMetricsOnError = flushMetrics;
30
49
 
31
50
  return {
32
51
  before: cloudwatchMetricsBefore,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/cloudwatch-metrics",
3
- "version": "7.2.2",
3
+ "version": "7.3.0",
4
4
  "description": "Embedded CloudWatch metrics middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -17,9 +17,6 @@
17
17
  "import": {
18
18
  "types": "./index.d.ts",
19
19
  "default": "./index.js"
20
- },
21
- "require": {
22
- "default": "./index.js"
23
20
  }
24
21
  }
25
22
  },
@@ -71,7 +68,7 @@
71
68
  "aws-embedded-metrics": "4.2.1"
72
69
  },
73
70
  "devDependencies": {
74
- "@middy/core": "7.2.2",
71
+ "@middy/core": "7.3.0",
75
72
  "@types/aws-lambda": "^8.0.0",
76
73
  "@types/node": "^22.0.0"
77
74
  }