Skip to content

Bug: Custom Metrics with different Dimensions treated as Multi-value Metrics when using log_metrics Decorator. #5361

@rastogin-rp

Description

@rastogin-rp

Expected Behaviour

While using the AWS Powertools for Metrics, I've encountered an issue with how multiple metrics EMFs are handled when they share the same name but differ by their dimensions while using the log_metrics decorator.

Current Behaviour

We have a custom high-resolution Lambda Execute metric where a custom dimension called Type alternates between the values Start and End.

The package currently treats these as multi-value metrics, combining them into one metric EMF when log metrics are called, even though they represent distinct operations and should be treated as separate EMFs with individual timestamps.

Code snippet

Given the following setup:

from aws_lambda_powertools import Metrics

metrics = Metrics(namespace="MyApp", service="SomeLambda")

@metrics.log_metrics
def lambda_handler(event, context):
    # Metric for starting the lambda execution
    metrics.add_metric(name="Lambda Execute", unit="None", value=1, resolution=1)
    metrics.add_dimension(name="Type", value="Start")

    # Business logic or operations
    # ...

    # Metric for completing the lambda execution
    metrics.add_metric(name="Lambda Execute", unit="None", value=1, resolution=1)
    metrics.add_dimension(name="Type", value="End")
    
    return {
        'statusCode': 200,
        'body': "Execution complete"
    }

I should have received two EMF logs:

{"_aws":{"Timestamp":1728566819588,"CloudWatchMetrics":[{"Namespace":"MyApp","Dimensions":[["Type"]],"Metrics":[{"Name":"Lambda Execute","Unit":"None","StorageResolution":1}]}]},"Type":"Start","Lambda Execute":[1.0]}

...some other logs from code...

{"_aws":{"Timestamp":1728566822038,"CloudWatchMetrics":[{"Namespace":"MyApp","Dimensions":[["Type"]],"Metrics":[{"Name":"Lambda Execute","Unit":"None","StorageResolution":1}]}]},"Type":"End","Lambda Execute":[1.0]}

But instead, I get only one EMF as a multi-value metric as follows:

{"_aws":{"Timestamp":1728566822038,"CloudWatchMetrics":[{"Namespace":"MyApp","Dimensions":[["Type"]],"Metrics":[{"Name":"Lambda Execute","Unit":"None","StorageResolution":1}]}]},"Type":"End","Lambda Execute":[1.0, 1.0]}

Possible Solution

Add logic to check for varying dimension values to identify whether it's a unique metric or multi-value metric EMF.

Steps to Reproduce

  1. Define two Lambda Execute custom metrics with a Type dimension alternating between start and end.
  2. Use the AWS Powertools metrics decorator on the Lambda function to flush metrics.
  3. Notice that it generates a single EMF with multiple values, with the last set type dimension value, instead of two distinct EMFs as one would expect in the logs.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.12

Packaging format used

PyPi

Debugging logs

No response

Metadata

Metadata

Labels

Type

No type

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions