cloudwatch-metrics icon indicating copy to clipboard operation
cloudwatch-metrics copied to clipboard

fix: PutMetricData Timestamp should be a Date

Open irae opened this issue 1 year ago • 0 comments

📚 Context/Description Behind The Change

In version 1.3.3, cloudwatch-metrics is throwing when using the option {withTimestamp: true}.

The AWS SDK specifies that Timestamp should be of Date type per MetricDatum spec. In version [email protected] is sending a string instead.

Sources:

https://github.com/aws/aws-sdk-js-v3/blob/main/clients/client-cloudwatch/src/models/models_0.ts#L4491-L4496

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-cloudwatch/Interface/PutMetricDataCommandInput/

Error when using [email protected].

TypeError: input.Timestamp.toISOString is not a function
    at se_MetricDatum (/Users/irae/code/consider/web-fe/node_modules/@aws-sdk/client-cloudwatch/dist-cjs/protocols/Aws_query.js:2768:48)
    at se_MetricData (/Users/irae/code/consider/web-fe/node_modules/@aws-sdk/client-cloudwatch/dist-cjs/protocols/Aws_query.js:2700:31)
    at se_PutMetricDataInput (/Users/irae/code/consider/web-fe/node_modules/@aws-sdk/client-cloudwatch/dist-cjs/protocols/Aws_query.js:3248:31)
    at se_PutMetricDataCommand (/Users/irae/code/consider/web-fe/node_modules/@aws-sdk/client-cloudwatch/dist-cjs/protocols/Aws_query.js:355:12)
    at serialize (/Users/irae/code/consider/web-fe/node_modules/@aws-sdk/client-cloudwatch/dist-cjs/commands/PutMetricDataCommand.js:40:56)
    at /Users/irae/code/consider/web-fe/node_modules/@smithy/middleware-serde/dist-cjs/serializerMiddleware.js:12:27
    at /Users/irae/code/consider/web-fe/node_modules/@smithy/middleware-endpoint/dist-cjs/endpointMiddleware.js:20:16
    at async /Users/irae/code/consider/web-fe/node_modules/@aws-sdk/middleware-logger/dist-cjs/loggerMiddleware.js:7:26

To reproduce error and test the fix:

const cloudwatchMetrics = require('cloudwatch-metrics');
const {Metric} = cloudwatchMetrics;

cloudwatchMetrics.initialize({
    region: 'us-west-2',
});

const apiMetrics = new Metric(
    'EmailMetrics',
    'Count',
    [
        {
            Name: 'environment',
            Value: 'dev-irae',
        },
    ],
    {
        enabled: true,
        sendCallback: (error, data) => {
            error && console.error(error);
            data !== undefined && console.log(data);
        },
        sendInterval: 100,
        summaryInterval: 200,
        withTimestamp: true,
    }
);

apiMetrics.put(12, 'lastDay', 'Count', [
    {
        Name: 'EmailType',
        Value: 'listlog-added',
    },
]);

apiMetrics.shutdown();

🚨 Potential Risks & What To Monitor After Deployment

None

🧑‍🔬 How Has This Been Tested?

  • Updating to [email protected] in my product codebase causes it to throw. The proposed change, when linked or patched in my node_modules, fixes the issue.
  • Example code provided breaks on 1.3.3 and works with proposed changes

🚚 Release Plan

Bump version to 1.3.4 and publish npm package.

irae avatar Sep 08 '23 13:09 irae