|
| 1 | +import os |
1 | 2 | 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 |
3 | 6 | from prometheus_client import Counter, Histogram, Gauge
|
4 | 7 |
|
5 | 8 | counter_no_labels = Counter(
|
@@ -43,6 +46,13 @@ def before_each(monkeypatch):
|
43 | 46 |
|
44 | 47 |
|
45 | 48 | 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 | + |
46 | 56 | reset()
|
47 | 57 |
|
48 | 58 | metrics = get_metrics()
|
@@ -85,3 +95,14 @@ def test_prometheus_get_metrics_returns_non_empty_metrics():
|
85 | 95 | """
|
86 | 96 |
|
87 | 97 | 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