Skip to content

Commit 157a767

Browse files
committed
feat: address Pablo's feedback with a decorator param
1 parent 23d13b1 commit 157a767

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

aws_lambda_powertools/metrics/metrics.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ def log_metrics(
128128
lambda_handler: Callable[[Any, Any], Any] = None,
129129
capture_cold_start_metric: bool = False,
130130
raise_on_empty_metrics: bool = False,
131+
default_dimensions: Dict[str, str] = None,
131132
):
132133
"""Decorator to serialize and publish metrics at the end of a function execution.
133134
@@ -150,11 +151,13 @@ def handler(event, context):
150151
Parameters
151152
----------
152153
lambda_handler : Callable[[Any, Any], Any], optional
153-
Lambda function handler, by default None
154+
lambda function handler, by default None
154155
capture_cold_start_metric : bool, optional
155-
Captures cold start metric, by default False
156+
captures cold start metric, by default False
156157
raise_on_empty_metrics : bool, optional
157-
Raise exception if no metrics are emitted, by default False
158+
raise exception if no metrics are emitted, by default False
159+
default_dimensions: Dict[str, str], optional
160+
metric dimensions as key=value that will always be present
158161
159162
Raises
160163
------
@@ -170,11 +173,14 @@ def handler(event, context):
170173
self.log_metrics,
171174
capture_cold_start_metric=capture_cold_start_metric,
172175
raise_on_empty_metrics=raise_on_empty_metrics,
176+
default_dimensions=default_dimensions,
173177
)
174178

175179
@functools.wraps(lambda_handler)
176180
def decorate(event, context):
177181
try:
182+
if default_dimensions:
183+
self.set_default_dimensions(**default_dimensions)
178184
response = lambda_handler(event, context)
179185
if capture_cold_start_metric:
180186
self.__add_cold_start_metric(context=context)

docs/core/metrics.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,11 @@ You can create metrics using `add_metric`, and you can create dimensions for all
107107

108108
### Adding default dimensions
109109

110-
You can add default metric dimensions to ensure they are persisted across Lambda invocations using `set_default_dimenions`.
110+
You can use either `set_default_dimensions` method or `default_permissions` parameter in `log_metrics` decorator to persist dimensions across Lambda invocations.
111111

112112
If you'd like to remove them at some point, you can use `clear_default_dimensions` method.
113113

114-
=== "Default dimensions"
114+
=== "set_default_dimensions method"
115115

116116
```python hl_lines="5"
117117
from aws_lambda_powertools import Metrics
@@ -124,6 +124,19 @@ If you'd like to remove them at some point, you can use `clear_default_dimension
124124
def lambda_handler(evt, ctx):
125125
metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1)
126126
```
127+
=== "with log_metrics decorator"
128+
129+
```python hl_lines="5 7"
130+
from aws_lambda_powertools import Metrics
131+
from aws_lambda_powertools.metrics import MetricUnit
132+
133+
metrics = Metrics(namespace="ExampleApplication", service="booking")
134+
DEFAULT_DIMENSIONS = {"environment": "prod", "another": "one"}
135+
136+
@metrics.log_metrics(default_dimensions=DEFAULT_DIMENSIONS)
137+
def lambda_handler(evt, ctx):
138+
metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1)
139+
```
127140

128141
### Flushing metrics
129142

tests/functional/test_metrics.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,3 +798,27 @@ def test_default_dimensions_across_instances(namespace):
798798

799799
# THEN default dimensions should also be present
800800
assert "environment" in same_metrics.default_dimensions
801+
802+
803+
def test_log_metrics_with_default_dimensions(capsys, metrics, dimensions, namespace):
804+
# GIVEN Metrics is initialized
805+
my_metrics = Metrics(namespace=namespace)
806+
default_dimensions = {"environment": "test", "log_group": "/lambda/test"}
807+
808+
# WHEN we utilize log_metrics with default dimensions to serialize
809+
# and flush metrics and clear all metrics and dimensions from memory
810+
# at the end of a function execution
811+
@my_metrics.log_metrics(default_dimensions=default_dimensions)
812+
def lambda_handler(evt, ctx):
813+
for metric in metrics:
814+
my_metrics.add_metric(**metric)
815+
816+
lambda_handler({}, {})
817+
first_invocation = capture_metrics_output(capsys)
818+
819+
lambda_handler({}, {})
820+
second_invocation = capture_metrics_output(capsys)
821+
822+
# THEN we should have default dimensions in both outputs
823+
assert "environment" in first_invocation
824+
assert "environment" in second_invocation

0 commit comments

Comments
 (0)