Skip to content

Commit 67f3432

Browse files
committed
Fix
1 parent 320f312 commit 67f3432

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lambda_powertools/prometheus.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ def filter_metrics(m):
4646
dir_m = dir(m)
4747
if "_value" in dir_m and m._value._value == 0:
4848
return False
49-
if "_metrics" in dir_m and len(m._metrics.values()) == 0:
50-
return False
5149
if "_buckets" in dir_m and m._sum._value == 0:
5250
return False
51+
if "_metrics" in dir_m:
52+
for metric in m._metrics.values():
53+
if ("_value" in dir(metric) and metric._value._value > 0) or ("_sum" in dir(metric) and metric._sum._value > 0):
54+
return True
55+
return False
5356

5457
return True
5558

tests/test_prometheus.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import os
12
import pytest
2-
from lambda_powertools.prometheus import get_metrics, reset
3+
from unittest.mock import patch
4+
5+
from lambda_powertools.prometheus import get_metrics, reset, flush_metrics
36
from prometheus_client import Counter, Histogram, Gauge
47

58
counter_no_labels = Counter(
@@ -43,6 +46,13 @@ def before_each(monkeypatch):
4346

4447

4548
def test_prometheus_get_metrics_does_not_return_empty_metrics():
49+
gauge.set(1) # Gauge is not supported yet
50+
counter_no_labels.inc(1)
51+
counter_with_labels.labels("bar").inc(2)
52+
histogram_no_labels.observe(2)
53+
histogram_no_labels.observe(5)
54+
histogram_with_labels.labels("bar").observe(2)
55+
4656
reset()
4757

4858
metrics = get_metrics()
@@ -85,3 +95,14 @@ def test_prometheus_get_metrics_returns_non_empty_metrics():
8595
"""
8696

8797
assert metrics == expected
98+
99+
100+
@patch('builtins.print')
101+
@patch.dict(os.environ, {"PYTEST_CURRENT_TEST": ""})
102+
def test_prometheus_flush_metrics(mock_print):
103+
counter_no_labels.inc(1)
104+
counter_with_labels.labels("bar").inc(2)
105+
106+
flush_metrics()
107+
108+
mock_print.assert_called_with('PROMLOG ["# HELP prometheus_spec_counter_no_labels_total Prometheus example counter without labels\\n# TYPE prometheus_spec_counter_no_labels_total counter\\nprometheus_spec_counter_no_labels_total 1.0\\n# HELP prometheus_spec_counter_with_labels_total Prometheus example counter with labels\\n# TYPE prometheus_spec_counter_with_labels_total counter\\nprometheus_spec_counter_with_labels_total{foo=\\"bar\\"} 2.0\\n"]')

0 commit comments

Comments
 (0)