AWS Embedded Metrics

3.5.0 · active · verified Sat Apr 11

The AWS Embedded Metrics Python library provides an easy way to emit custom metrics asynchronously, allowing users to consolidate logs and metrics into a single data stream without complex metric client setup. It formats metric data in a structured log format (EMF) for consumption by CloudWatch Logs and then CloudWatch. The current version is 3.5.0, and it follows an active release cadence with frequent updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `@metric_scope` decorator to capture custom metrics. Metrics, dimensions, and properties defined within the decorated function will be flushed as a single Embedded Metric Format (EMF) log entry at the end of the function's execution. Locally, these entries are printed to stdout; in AWS environments, they are typically picked up by the CloudWatch Agent.

from aws_embedded_metrics import metric_scope

@metric_scope
def process_data(metrics):
    metrics.set_namespace("MyApplication")
    metrics.put_dimensions({"Service": "DataProcessor", "Operation": "Transform"})

    # Add multiple metric values for a single metric
    metrics.put_metric("ProcessingLatency", 100, "Milliseconds")
    metrics.put_metric("ProcessingLatency", 95, "Milliseconds")
    metrics.put_metric("ProcessingLatency", 105, "Milliseconds")

    metrics.set_property("Region", "us-east-1")
    metrics.set_property("ContainerId", "abc-123")

    print("Processing data...")

if __name__ == "__main__":
    # In a deployed AWS environment, these metrics would be collected by the CloudWatch Agent
    # and sent to CloudWatch. Locally, they are printed to stdout in EMF format.
    process_data()

view raw JSON →