Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: PutMetricData Timestamp should be a Date #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

irae
Copy link

@irae irae commented Sep 8, 2023

πŸ“š 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.

AWS SDK is throwing when using cloudwatch-metrics withTimestamp.

Timestamp should be of Date type per MetricDatum spec.
Source:

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-cloudwatch/Interface/PutMetricDataCommandInput/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant