@middy/cloudwatch-metrics 7.2.3 → 7.3.1

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 +6 -1
  2. package/index.js +34 -4
  3. package/package.json +2 -5
package/index.d.ts CHANGED
@@ -7,7 +7,8 @@ export { MetricsLogger } from "aws-embedded-metrics";
7
7
 
8
8
  export interface Options {
9
9
  namespace?: string;
10
- dimensions?: Array<Record<string, string>>;
10
+ dimensions?: Record<string, string> | 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,38 @@
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
+ // `dimensions` accepts either a single dimension set (object of
12
+ // string->string) or an array of dimension sets, matching the documented
13
+ // API and aws-embedded-metrics' `setDimensions(dimensionSets)` signature.
14
+ const dimensionSetSchema = {
15
+ type: "object",
16
+ additionalProperties: { type: "string", minLength: 1, maxLength: 1024 },
17
+ };
18
+
19
+ const optionSchema = {
20
+ type: "object",
21
+ properties: {
22
+ namespace: { type: "string", minLength: 1, maxLength: 256 },
23
+ dimensions: {
24
+ oneOf: [dimensionSetSchema, { type: "array", items: dimensionSetSchema }],
25
+ },
26
+ onFlushError: { instanceof: "Function" },
27
+ },
28
+ additionalProperties: false,
29
+ };
30
+
31
+ export const cloudwatchMetricsValidateOptions = (options) =>
32
+ validateOptions("@middy/cloudwatch-metrics", optionSchema, options);
6
33
 
7
34
  const cloudwatchMetricsMiddleware = (opts = {}) => {
8
- const { namespace, dimensions } = { ...defaults, ...opts };
35
+ const { namespace, dimensions, onFlushError } = { ...defaults, ...opts };
9
36
  const cloudwatchMetricsBefore = async (request) => {
10
37
  const metrics = awsEmbeddedMetrics.createMetricsLogger();
11
38
 
@@ -24,8 +51,11 @@ const cloudwatchMetricsMiddleware = (opts = {}) => {
24
51
  const flushMetrics = async (request) => {
25
52
  try {
26
53
  await request.context.metrics?.flush();
27
- } catch {
28
- // Swallow flush errors to prevent metrics from crashing the handler
54
+ } catch (err) {
55
+ // Flush errors are swallowed to prevent metrics from crashing the
56
+ // handler. Users who need visibility (e.g., to catch IAM or network
57
+ // misconfigurations) can pass `onFlushError` to observe them.
58
+ onFlushError?.(err);
29
59
  }
30
60
  };
31
61
  const cloudwatchMetricsAfter = flushMetrics;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/cloudwatch-metrics",
3
- "version": "7.2.3",
3
+ "version": "7.3.1",
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.3",
71
+ "@middy/core": "7.3.1",
75
72
  "@types/aws-lambda": "^8.0.0",
76
73
  "@types/node": "^22.0.0"
77
74
  }