Skip to content

fix: metrics not being flushed on every invocation #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# HISTORY

## May 29th

**0.9.4**

* **Metrics**: Bugfix - Metrics were not correctly flushed, and cleared on every invocation

## May 16th

**0.9.3**
Expand Down
5 changes: 2 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ build-docs:
@$(MAKE) build-docs-website
@$(MAKE) build-docs-api

build-docs-api:
pip install pdoc3~=0.7.5
pdoc3 --html --output-dir dist/api/ ./aws_lambda_powertools --force
build-docs-api: dev
poetry run pdoc --html --output-dir dist/api/ ./aws_lambda_powertools --force
mv dist/api/aws_lambda_powertools/* dist/api/
rm -rf dist/api/aws_lambda_powertools

Expand Down
9 changes: 6 additions & 3 deletions aws_lambda_powertools/metrics/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class MetricManager:
"""

def __init__(self, metric_set: Dict[str, str] = None, dimension_set: Dict = None, namespace: str = None):
self.metric_set = metric_set or {}
self.dimension_set = dimension_set or {}
self.metric_set = metric_set if metric_set is not None else {}
self.dimension_set = dimension_set if dimension_set is not None else {}
self.namespace = os.getenv("POWERTOOLS_METRICS_NAMESPACE") or namespace
self._metric_units = [unit.value for unit in MetricUnit]
self._metric_unit_options = list(MetricUnit.__members__)
Expand Down Expand Up @@ -116,7 +116,10 @@ def add_metric(self, name: str, unit: MetricUnit, value: Union[float, int]):
logger.debug(f"Exceeded maximum of {MAX_METRICS} metrics - Publishing existing metric set")
metrics = self.serialize_metric_set()
print(json.dumps(metrics))
self.metric_set = {}

# clear metric set only as opposed to metrics and dimensions set
# since we could have more than 100 metrics
self.metric_set.clear()

def serialize_metric_set(self, metrics: Dict = None, dimensions: Dict = None) -> Dict:
"""Serializes metric and dimensions set
Expand Down
12 changes: 10 additions & 2 deletions aws_lambda_powertools/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,15 @@ def do_something():
_metrics = {}
_dimensions = {}

def __init__(self, metric_set=None, dimension_set=None, namespace=None):
super().__init__(metric_set=self._metrics, dimension_set=self._dimensions, namespace=namespace)
def __init__(self):
self.metric_set = self._metrics
self.dimension_set = self._dimensions
super().__init__(metric_set=self.metric_set, dimension_set=self.dimension_set)

def clear_metrics(self):
logger.debug("Clearing out existing metric set from memory")
self.metric_set.clear()
self.dimension_set.clear()

def log_metrics(self, lambda_handler: Callable[[Any, Any], Any] = None):
"""Decorator to serialize and publish metrics at the end of a function execution.
Expand Down Expand Up @@ -101,6 +108,7 @@ def decorate(*args, **kwargs):
response = lambda_handler(*args, **kwargs)
finally:
metrics = self.serialize_metric_set()
self.clear_metrics()
logger.debug("Publishing metrics", {"metrics": metrics})
print(json.dumps(metrics))

Expand Down
19 changes: 19 additions & 0 deletions docs/content/core/metrics.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ def lambda_handler(evt, ctx):
...
```

## Flushing metrics manually

If you prefer not to use `log_metrics` because you might want to encapsulate additional logic when doing so, you can manually flush and clear metrics as follows:

```python:title=manual_metric_serialization.py
import json
from aws_lambda_powertools.metrics import Metrics, MetricUnit

metrics = Metrics()
metrics.add_metric(name="ColdStart", unit="Count", value=1)
metrics.add_dimension(name="service", value="booking")

# highlight-start
your_metrics_object = metrics.serialize_metric_set()
metrics.clear_metrics()
print(json.dumps(your_metrics_object))
# highlight-end
```

## Testing your code

Use `POWERTOOLS_METRICS_NAMESPACE` env var when unit testing your code to ensure a metric namespace object is created, and your code doesn't fail validation.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "aws_lambda_powertools"
version = "0.9.3"
version = "0.9.4"
description = "Python utilities for AWS Lambda functions including but not limited to tracing, logging and custom metric"
authors = ["Amazon Web Services"]
classifiers=[
Expand Down
Loading