Skip to content

fix(metrics): Support multiple addMetric() call with the same metric name #390

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

Merged
merged 6 commits into from
Jan 4, 2022

Conversation

ijemmy
Copy link
Contributor

@ijemmy ijemmy commented Jan 3, 2022

Description of your changes

Currently, adding metrics with the same name will override the existing value. For example,

metrics.addMetric('successfulBooking', MetricUnits.Count, 2);
metrics.addMetric('successfulBooking', MetricUnits.Count, 1);

Will emit only one EMF metric with value "1" (the 2nd call override the first one).

This is not the correct behavior. This PR implements the change to emit two values in array like this:

    {
        "performedActionA": [ // Use array instead of number when there're multiple values
            2,
            1
        ],
        "_aws": {
            "Timestamp": 1592234975665,
            "CloudWatchMetrics": [
                {
                "Namespace": "serverlessAirline",
                //...
                "Metrics": [
                    {
                    "Name": "performedActionA",
                    "Unit": "Count"
                    }
                ]
                }
            ]
        },
        //...
    }

How to verify this change

Run test for metrics package. I have added test cases in Metrics.test.ts and refactor the test for middy to reflect this change

Related issues, RFCs

#384

PR status

Is this ready for review?: YES
Is it a breaking change?: NO

Checklist

  • My changes meet the tenets criteria
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in areas that should be flagged with a TODO, or hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • The code coverage hasn't decreased
  • I have added tests that prove my change is effective and works
  • New and existing unit tests pass locally and in Github Actions
  • Any dependent changes have been merged and published in downstream module
  • The PR title follows the conventional commit semantics

Breaking change checklist

  • I have documented the migration process
  • I have added, implemented necessary warnings (if it can live side by side)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@@ -10,7 +10,7 @@
"commit": "commit",
"test": "jest --group=unit --detectOpenHandles --coverage --verbose",
"test:e2e": "jest --group=e2e",
"watch": "jest --watch",
"watch": "jest --group=unit --watch ",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not practical to run e2e test in the watch mode so I filtered this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point !

Comment on lines +316 to +330
"Timestamp": 1592234975665,
"CloudWatchMetrics": [
{
"Name": "bookingConfirmation",
"Unit": "Count"
"Namespace": "exampleApplication",
"Dimensions": [
[
"service"
]
],
"Metrics": [
{
"Name": "bookingConfirmation",
"Unit": "Count"
}
]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just shifting indentation. The current version misses one more indentation under "_aws"

@ijemmy ijemmy requested review from flochaz and saragerion January 3, 2022 19:00
@ijemmy ijemmy marked this pull request as ready for review January 3, 2022 19:03
@ijemmy
Copy link
Contributor Author

ijemmy commented Jan 3, 2022

@heitorlessa Ping, in case you want to port this to other languages.

@dreamorosi dreamorosi added this to the beta-release milestone Jan 3, 2022
@dreamorosi dreamorosi added bug Something isn't working metrics This item relates to the Metrics Utility labels Jan 3, 2022
Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion to improve error thrown and docs section name

flochaz
flochaz previously approved these changes Jan 3, 2022
@@ -10,7 +10,7 @@
"commit": "commit",
"test": "jest --group=unit --detectOpenHandles --coverage --verbose",
"test:e2e": "jest --group=e2e",
"watch": "jest --watch",
"watch": "jest --group=unit --watch ",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point !

saragerion
saragerion previously approved these changes Jan 4, 2022
Copy link
Contributor

@saragerion saragerion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic!

Copy link
Contributor

@saragerion saragerion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 🚀🚀🚀

@ijemmy ijemmy merged commit 91a2bba into main Jan 4, 2022
@ijemmy ijemmy deleted the fix/metrics/fixMultiAdd branch January 4, 2022 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working metrics This item relates to the Metrics Utility
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Multiple addMetrics() calls should not override each other but push more value into the array in EMF
5 participants