Module aws_lambda_powertools.metrics.functions

Functions

def convert_timestamp_to_emf_format(timestamp: int | datetime) ‑> int

Converts a timestamp to EMF compatible format.

Parameters

timestamp : int | datetime
The timestamp to convert. If already in epoch milliseconds format, returns it as is. If datetime object, converts it to milliseconds since Unix epoch.

Returns:

int The timestamp converted to EMF compatible format (milliseconds since Unix epoch).

def extract_cloudwatch_metric_resolution_value(metric_resolutions: List, resolution: int | MetricResolution) ‑> int

Return metric value from CloudWatch metric unit whether that's str or MetricResolution enum

Parameters

unit : Union[int, MetricResolution]
Metric resolution

Returns

int
Metric resolution value must be 1 or 60

Raises

MetricResolutionError
When metric resolution is not supported by CloudWatch
def extract_cloudwatch_metric_unit_value(metric_units: List, metric_valid_options: List, unit: str | MetricUnit) ‑> str

Return metric value from CloudWatch metric unit whether that's str or MetricUnit enum

Parameters

unit : Union[str, MetricUnit]
Metric unit

Returns

str
Metric unit value (e.g. "Seconds", "Count/Second")

Raises

MetricUnitError
When metric unit is not supported by CloudWatch
def validate_emf_timestamp(timestamp: int | datetime) ‑> bool

Validates a given timestamp based on CloudWatch Timestamp guidelines.

Timestamp must meet CloudWatch requirements, otherwise an InvalidTimestampError will be raised. See Timestamps for valid values.

Parameters:

timestamp: int | datetime Datetime object or epoch time in milliseconds representing the timestamp to validate.

Returns

bool
Valid or not timestamp values