Skip to content

Multiple values for a single metric not working #165

Closed

Description

@Dunedan

I'm trying to log multiple values for a single metric using powertool's Metrics class.
However when doing so I only get the last added value flushed.

Here is a minimal example which triggers the problem:

#!/usr/bin/env python3

import json

from aws_lambda_powertools import Metrics
from aws_lambda_powertools.metrics import MetricUnit

metrics = Metrics(namespace="Test", service="Test")

@metrics.log_metrics
def handler(event, context):
    metrics.add_metric(name="TestMetric", unit=MetricUnit.Count, value=1)
    metrics.add_metric(name="TestMetric", unit=MetricUnit.Count, value=5)

handler(None, None)

The code above produces the following output (formatted for better readability):

{
    "_aws": {
        "Timestamp": 1600250139457,
        "CloudWatchMetrics": [
            {
                "Namespace": "Test",
                "Dimensions": [
                    [
                        "service"
                    ]
                ],
                "Metrics": [
                    {
                        "Name": "TestMetric",
                        "Unit": "Count"
                    }
                ]
            }
        ]
    },
    "service": "Test",
    "TestMetric": 5.0
}

What I'd expect instead would be the following:

{
    "_aws": {
        "Timestamp": 1600250139457,
        "CloudWatchMetrics": [
            {
                "Namespace": "Test",
                "Dimensions": [
                    [
                        "service"
                    ]
                ],
                "Metrics": [
                    {
                        "Name": "TestMetric",
                        "Unit": "Count"
                    }
                ]
            }
        ]
    },
    "service": "Test",
    "TestMetric": [1.0, 5.0]
}

I'm not sure if this is a bug or if I'm missing something from the documentation, but to me it currently looks that logging multiple values for a single metric isn't possible without flushing manually between adding them.

Activity

  1. added
    bug
    triage
    on Sep 16, 2020
  2. heitorlessa commented on Sep 16, 2020

    @heitorlessa
    Contributor

    hey @Dunedan - This is not a bug but a known behaviour of Embedded Metric Format (EMF) feature we use for creating metrics async. It only supports one value per unique key name - If you add multiple values it'll override with the last one.

    For that and other purposes, we created the with single_metric context manager so it'll create one metric and flush it automatically.

    I thought of having another API for that would keep multiple values for the same metric, and then flush them at the end as a workaround to EMF specification - But not entirely sure on the value and UX yet. If you think this is a good idea, please feel free to write up a RFC to discuss more about it :)

  3. self-assigned this
    on Sep 16, 2020
  4. added
    feature-request
    and removed
    bug
    triage
    on Sep 16, 2020
  5. Dunedan commented on Sep 16, 2020

    @Dunedan
    ContributorAuthor

    Are you sure that this is a limitation of EMF?

    The EMF specification suggests otherwise:

    Metric targets MUST be either a numeric value or an array of numeric values.

    Also aws-embedded-metrics-python implements it as I would expect it and I believe such records are properly handled by CloudWatch Metrics.

    #!/usr/bin/env python3
    
    import os
    
    os.environ["AWS_LAMBDA_FUNCTION_NAME"] = "dummy"
    
    from aws_embedded_metrics import metric_scope
    
    @metric_scope
    def handler(metrics):
        metrics.put_dimensions({"Foo": "Bar"})
        metrics.put_metric("TestMetric", 1, "Count")
        metrics.put_metric("TestMetric", 1, "Count")
    
    handler()
    {
        "LogGroup": "dummy",
        "ServiceName": "dummy",
        "ServiceType": "AWS::Lambda::Function",
        "Foo": "Bar",
        "executionEnvironment": "",
        "memorySize": "",
        "functionVersion": "",
        "logStreamId": "",
        "_aws": {
            "Timestamp": 1600271848937,
            "CloudWatchMetrics": [
                {
                    "Dimensions": [
                        [
                            "LogGroup",
                            "ServiceName",
                            "ServiceType",
                            "Foo"
                        ]
                    ],
                    "Metrics": [
                        {
                            "Name": "TestMetric",
                            "Unit": "Count"
                        }
                    ],
                    "Namespace": "aws-embedded-metrics"
                }
            ]
        },
        "TestMetric": [
            1,
            1
        ]
    }
    
  6. heitorlessa commented on Sep 16, 2020

    @heitorlessa
    Contributor
  7. added
    bug
    feature-request
    and removed
    feature-request
    bug
    on Sep 16, 2020
  8. heitorlessa commented on Sep 16, 2020

    @heitorlessa
    Contributor

    @Dunedan just confirmed with the CloudWatch team - This was added recently, it wasn't in the original implementation.

    Added the feature request label, and we'll implement this to be available in the next release 1.6.0 this month.

    Thanks for flagging it, much much appreciated

  9. added
    pending-release
    on Sep 18, 2020
  10. heitorlessa commented on Sep 18, 2020

    @heitorlessa
    Contributor

    hey @Dunedan - @cakepietoast just finished implementing it, and as I mentioned before, it'll be available in the next release.

    Thanks for flagging this and letting us know about the EMF update, much appreciated!

  11. mwarkentin commented on Sep 22, 2020

    @mwarkentin

    This can be closed now :)

  12. to-mc commented on Sep 22, 2020

    @to-mc
    Contributor

    Fixed in 1.6.0

  13. removed
    pending-release
    on Oct 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

feature-request

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions