Skip to content

Commit 8d72b47

Browse files
committed
docs: add functional testing section
1 parent 157a767 commit 8d72b47

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

docs/core/metrics.md

+40-3
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,11 @@ If you prefer setting environment variable for specific tests, and are using Pyt
391391
yield
392392
```
393393

394-
### Inspecting metrics
394+
### Functional testing
395395

396-
As metrics are logged to standard output, you can read stdoutput and assert whether metrics are present. Here's an example using `pytest` with `capsys` built-in fixture:
396+
As metrics are logged to standard output, you can read standard output and assert whether metrics are present. Here's an example using `pytest` with `capsys` built-in fixture:
397397

398-
=== "pytest_metrics_assertion.py"
398+
=== "Assert single EMF blob with pytest.py"
399399

400400
```python hl_lines="6 9-10 23-34"
401401
from aws_lambda_powertools import Metrics
@@ -424,4 +424,41 @@ As metrics are logged to standard output, you can read stdoutput and assert whet
424424
assert "SuccessfulBooking" in metrics_output["_aws"]["CloudWatchMetrics"][0]["Metrics"][0]["Name"]
425425
```
426426

427+
=== "Assert multiple EMF blobs with pytest"
428+
429+
```python hl_lines="8-9 11 21-23 25 29-30 32"
430+
from aws_lambda_powertools import Metrics
431+
from aws_lambda_powertools.metrics import MetricUnit
432+
433+
from collections import namedtuple
434+
435+
import json
436+
437+
def capture_metrics_output_multiple_emf_objects(capsys):
438+
return [json.loads(line.strip()) for line in capsys.readouterr().out.split("\n") if line]
439+
440+
def test_log_metrics(capsys):
441+
# GIVEN Metrics is initialized
442+
metrics = Metrics(namespace="ServerlessAirline")
443+
444+
# WHEN log_metrics is used with capture_cold_start_metric
445+
@metrics.log_metrics(capture_cold_start_metric=True)
446+
def lambda_handler(evt, ctx):
447+
metrics.add_metric(name="SuccessfulBooking", unit=MetricUnit.Count, value=1)
448+
metrics.add_dimension(name="environment", value="prod")
449+
450+
# log_metrics uses function_name property from context to add as a dimension for cold start metric
451+
LambdaContext = namedtuple("LambdaContext", "function_name")
452+
lambda_handler({}, LambdaContext("example_fn")
453+
454+
cold_start_blob, custom_metrics_blob = capture_metrics_output_multiple_emf_objects(capsys)
455+
456+
# THEN ColdStart metric and function_name dimension should be logged
457+
# in a separate EMF blob than the application metrics
458+
assert cold_start_blob["ColdStart"] == [1.0]
459+
assert cold_start_blob["function_name"] == "example_fn"
460+
461+
assert "SuccessfulBooking" in custom_metrics_blob # as per previous example
462+
```
463+
427464
!!! tip "For more elaborate assertions and comparisons, check out [our functional testing for Metrics utility](https://github.com/awslabs/aws-lambda-powertools-python/blob/develop/tests/functional/test_metrics.py)"

0 commit comments

Comments
 (0)