From 6f887a7d4bf455a4c5295a7dd7eb5c4d0e0d1ea4 Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Sat, 6 Mar 2021 22:12:54 -0800 Subject: [PATCH] docs(metrics): Corrections to the code examples --- aws_lambda_powertools/metrics/base.py | 4 +--- aws_lambda_powertools/metrics/metric.py | 8 ++++---- aws_lambda_powertools/metrics/metrics.py | 11 ++++++----- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/aws_lambda_powertools/metrics/base.py b/aws_lambda_powertools/metrics/base.py index bfe88bf899b..b7f3862b590 100644 --- a/aws_lambda_powertools/metrics/base.py +++ b/aws_lambda_powertools/metrics/base.py @@ -197,7 +197,7 @@ def serialize_metric_set(self, metrics: Dict = None, dimensions: Dict = None, me metric_names_and_units.append({"Name": metric_name, "Unit": metric_unit}) metric_names_and_values.update({metric_name: metric_value}) - embedded_metrics_object = { + return { "_aws": { "Timestamp": int(datetime.datetime.now().timestamp() * 1000), # epoch "CloudWatchMetrics": [ @@ -213,8 +213,6 @@ def serialize_metric_set(self, metrics: Dict = None, dimensions: Dict = None, me **metric_names_and_values, # "single_metric": 1.0 } - return embedded_metrics_object - def add_dimension(self, name: str, value: str): """Adds given dimension to all metrics diff --git a/aws_lambda_powertools/metrics/metric.py b/aws_lambda_powertools/metrics/metric.py index 8e7ace1e5bd..353defab0c6 100644 --- a/aws_lambda_powertools/metrics/metric.py +++ b/aws_lambda_powertools/metrics/metric.py @@ -27,9 +27,9 @@ class SingleMetric(MetricManager): ------- **Creates cold start metric with function_version as dimension** - from aws_lambda_powertools.metrics import SingleMetric, MetricUnit import json - metric = Single_Metric(namespace="ServerlessAirline") + from aws_lambda_powertools.metrics import single_metric, MetricUnit + metric = single_metric(namespace="ServerlessAirline") metric.add_metric(name="ColdStart", unit=MetricUnit.Count, value=1) metric.add_dimension(name="function_version", value=47) @@ -72,7 +72,7 @@ def single_metric(name: str, unit: MetricUnit, value: float, namespace: str = No from aws_lambda_powertools.metrics import MetricUnit with single_metric(name="ColdStart", unit=MetricUnit.Count, value=1, namespace="ServerlessAirline") as metric: - metric.add_dimension(name="function_version", value=47) + metric.add_dimension(name="function_version", value="47") **Same as above but set namespace using environment variable** @@ -82,7 +82,7 @@ def single_metric(name: str, unit: MetricUnit, value: float, namespace: str = No from aws_lambda_powertools.metrics import MetricUnit with single_metric(name="ColdStart", unit=MetricUnit.Count, value=1) as metric: - metric.add_dimension(name="function_version", value=47) + metric.add_dimension(name="function_version", value="47") Parameters ---------- diff --git a/aws_lambda_powertools/metrics/metrics.py b/aws_lambda_powertools/metrics/metrics.py index 4f53231b84f..5178116a717 100644 --- a/aws_lambda_powertools/metrics/metrics.py +++ b/aws_lambda_powertools/metrics/metrics.py @@ -39,14 +39,13 @@ class Metrics(MetricManager): metrics.add_dimension(name="function_version", value="$LATEST") ... - @tracer.capture_lambda_handler @metrics.log_metrics() def lambda_handler(): - do_something() - return True + do_something() + return True def do_something(): - metrics.add_metric(name="Something", unit="Count", value=1) + metrics.add_metric(name="Something", unit="Count", value=1) Environment variables --------------------- @@ -111,12 +110,14 @@ def log_metrics( ------- **Lambda function using tracer and metrics decorators** + from aws_lambda_powertools import Metrics, Tracer + metrics = Metrics(service="payment") tracer = Tracer(service="payment") @tracer.capture_lambda_handler @metrics.log_metrics - def handler(event, context): + def handler(event, context): ... Parameters