aws-powertools / powertools-lambda-java

Powertools is a developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/java/
MIT No Attribution
285 stars 89 forks source link

Feature request: support high resolution metrics #1041

Open dreamorosi opened 1 year ago

dreamorosi commented 1 year ago

Is your feature request related to a problem? Please describe.

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.

Describe the solution you'd like

import software.amazon.lambda.powertools.metrics.Metrics;
import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger;

public class MetricsEnabledHandler implements RequestHandler<Object, Object> {

    MetricsLogger metricsLogger = MetricsUtils.metricsLogger();

    @Override
    @Metrics(namespace = "ExampleApplication", service = "booking")
    public Object handleRequest(Object input, Context context) {
        // Publish a metric with standard resolution i.e. StorageResolution = 60
        metricsLogger.putMetric("SuccessfulBooking", 1, Unit.COUNT, Resolution.STANDARD);
        // Publish a metric with high resolution i.e. StorageResolution = 1
        metricsLogger.putMetric("SuccessfulBooking", 1, Unit.COUNT, Resolution.HIGH);
        // The last parameter (storage resolution) is optional
        metricsLogger.putMetric("SuccessfulBooking", 1, Unit.COUNT);
    }
}

Describe alternatives you've considered

Additional context

msailes commented 1 year ago

This feature depends on a major version upgrade of aws-embedded-metrics-java v4.1.0.

The upgrade involves a breaking change, described in https://github.com/awslabs/aws-lambda-powertools-java/issues/944.