Skip to content

Feature request: support high resolution metrics  #1908

Closed
@dreamorosi

Description

@dreamorosi

Use case

Amazon CloudWatch announced support for high resolution metric extraction from structured logs (EMF). Customers can now provide an optional StorageResolution parameter within the EMF specification with a value of 1 or 60 (default) to indicate the desired resolution (in seconds) of the metric.

We should consider adding support for this new optional parameter to Metrics.

The EMF log should have this format:

{
  "_aws": {
    "CloudWatchMetrics": [
      {
        "Metrics": [
          {
            "Name": "Time",
            "Unit": "Milliseconds",
            "StorageResolution": 60 // <- new key
          }
        ],
        ...
      }
    ]
  },
  "Time": 1
}

As part of this issue we should also update the API docs, documentation, and unit/integration tests.

Solution/User Experience

from aws_lambda_powertools import Metrics
from aws_lambda_powertools.metrics import MetricUnit, MetricResolution
from aws_lambda_powertools.utilities.typing import LambdaContext

metrics = Metrics()


@metrics.log_metrics  # ensures metrics are flushed upon request completion/failure
def lambda_handler(event: dict, context: LambdaContext):
    # Publish a metric with standard resolution i.e. StorageResolution = 60
    metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1, resolution=MetricResolution.Standard)

    # Publish a metric with high resolution i.e. StorageResolution = 1
    metrics.add_metric(name="FailedBooking", unit=MetricUnit.Count, value=1, resolution=MetricResolution.High)

    # The last parameter (storage resolution) is optional
    metrics.add_metric(name="SuccessfulUpgrade", unit=MetricUnit.Count, value=1)

To support the proposal, a new MetricResolution enum should be added:

class MetricResolution(Enum):
    Standard = 60
    High = 1

and changes should be made to the Metrics.add_metric method to allow for a new optional parameter.

Note
The EMF specification states that if a value is not provided, then a default value of 60 is assumed (aka standard resolution). For this reason, if the resolution parameter is not specified we won't add the resolution.

Alternative solutions

No response

Acknowledgment

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions