Skip to content

Metrics

CLASS DESCRIPTION
Metrics

Metrics create an CloudWatch EMF object with up to 100 metrics

Metrics

Metrics(
    service: str | None = None,
    namespace: str | None = None,
    provider: AmazonCloudWatchEMFProvider | None = None,
    function_name: str | None = None,
)

Metrics create an CloudWatch EMF object with up to 100 metrics

Use Metrics when you need to create multiple metrics that have dimensions in common (e.g. service_name="payment").

Metrics up to 100 metrics in memory and are shared across all its instances. That means it can be safely instantiated outside of a Lambda function, or anywhere else.

A decorator (log_metrics) is provided so metrics are published at the end of its execution. If more than 100 metrics are added at a given function execution, these metrics are serialized and published before adding a given metric to prevent metric truncation.

Example

Creates a few metrics and publish at the end of a function execution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from aws_lambda_powertools import Metrics

metrics = Metrics(namespace="ServerlessAirline", service="payment")

@metrics.log_metrics(capture_cold_start_metric=True)
def lambda_handler():
    metrics.add_metric(name="BookingConfirmation", unit="Count", value=1)
    metrics.add_dimension(name="function_version", value="$LATEST")

    return True
Environment variables

POWERTOOLS_METRICS_NAMESPACE : str metric namespace POWERTOOLS_SERVICE_NAME : str service name used for default dimension POWERTOOLS_METRICS_DISABLED: bool Powertools metrics disabled (e.g. "true", "True", "TRUE")

PARAMETER DESCRIPTION
service

service name to be used as metric dimension, by default "service_undefined"

TYPE: str DEFAULT: None

namespace

Namespace for metrics

TYPE: str DEFAULT: None

provider

Pre-configured AmazonCloudWatchEMFProvider provider

TYPE: AmazonCloudWatchEMFProvider | None DEFAULT: None

RAISES DESCRIPTION
MetricUnitError

When metric unit isn't supported by CloudWatch

MetricResolutionError

When metric resolution isn't supported by CloudWatch

MetricValueError

When metric value isn't a number

SchemaValidationError

When metric object fails EMF schema validation

METHOD DESCRIPTION
set_timestamp

Set the timestamp for the metric.

Source code in aws_lambda_powertools/metrics/metrics.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def __init__(
    self,
    service: str | None = None,
    namespace: str | None = None,
    provider: AmazonCloudWatchEMFProvider | None = None,
    function_name: str | None = None,
):
    self.metric_set = self._metrics
    self.metadata_set = self._metadata
    self.default_dimensions = self._default_dimensions
    self.dimension_set = self._dimensions

    self.dimension_set.update(**self._default_dimensions)

    if provider is None:
        self.provider = AmazonCloudWatchEMFProvider(
            namespace=namespace,
            service=service,
            metric_set=self.metric_set,
            dimension_set=self.dimension_set,
            metadata_set=self.metadata_set,
            default_dimensions=self._default_dimensions,
            function_name=function_name,
        )
    else:
        self.provider = provider

set_timestamp

set_timestamp(timestamp: int)

Set the timestamp for the metric.

Source code in aws_lambda_powertools/metrics/metrics.py
136
137
138
139
140
141
142
143
144
145
146
147
def set_timestamp(self, timestamp: int):
    """
    Set the timestamp for the metric.

    Parameters:
    -----------
    timestamp: int | datetime.datetime
        The timestamp to create the metric.
        If an integer is provided, it is assumed to be the epoch time in milliseconds.
        If a datetime object is provided, it will be converted to epoch time in milliseconds.
    """
    self.provider.set_timestamp(timestamp=timestamp)