From 44786ad08c28c78b415b06e511c93078ed8e74dc Mon Sep 17 00:00:00 2001 From: ekneg54 Date: Mon, 23 Oct 2023 17:20:35 +0000 Subject: [PATCH] add metric tests for gauge and histogram --- logprep/framework/pipeline.py | 5 +- tests/unit/metrics/test_metrics.py | 88 +++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 6 deletions(-) diff --git a/logprep/framework/pipeline.py b/logprep/framework/pipeline.py index 1952b72a3..1f5e8d4da 100644 --- a/logprep/framework/pipeline.py +++ b/logprep/framework/pipeline.py @@ -347,15 +347,12 @@ def process_event(self, event: dict): """process all processors for one event""" """ - ToDos: - - Processor Specific Metrics (Pseudonymizer, Amides, DomainResolver) - - Fix Pseudonymizer str has no match + TODOs: - Tests - delete SharedCounter (Events in last 5 min: n) - create Grafana Dashboards - add pipelinemanager metrics (pipeline restarts) - clean up PrometheusExporter ("remove stale metric files" stil needed?) - - add Kafka librdkafka metrics - add version info to metrics - enforce rule_id should be unique """ diff --git a/tests/unit/metrics/test_metrics.py b/tests/unit/metrics/test_metrics.py index 4da624e12..6ff05aadd 100644 --- a/tests/unit/metrics/test_metrics.py +++ b/tests/unit/metrics/test_metrics.py @@ -5,7 +5,13 @@ import re import pytest -from prometheus_client import CollectorRegistry, Counter, Histogram, generate_latest +from prometheus_client import ( + CollectorRegistry, + Counter, + Gauge, + Histogram, + generate_latest, +) from logprep.abc.component import Component from logprep.metrics.metrics import CounterMetric, GaugeMetric, HistogramMetric @@ -79,7 +85,7 @@ def test_counter_metric_increments_correctly(self): metric_output = generate_latest(self.custom_registry).decode("utf-8") assert 'logprep_bla_total{pipeline="1"} 1.0' in metric_output - def test_counter_metric_increments_twice(self): + def test_counter_metric_increments_twice_adds_metric(self): metric = CounterMetric( name="bla", description="empty description", @@ -150,6 +156,84 @@ def test_init_tracker_raises_on_try_to_overwrite_tracker_with_different_type(sel ) +class TestGaugeMetric: + def setup_method(self): + self.custom_registry = CollectorRegistry() + + def test_init_tracker_returns_collector(self): + metric = GaugeMetric( + name="testmetric", + description="empty description", + labels={"A": "a"}, + registry=self.custom_registry, + ) + assert isinstance(metric.tracker, Gauge) + + def test_gauge_metric_increments_correctly(self): + metric = GaugeMetric( + name="bla", + description="empty description", + labels={"pipeline": "1"}, + registry=self.custom_registry, + ) + metric += 1 + metric_output = generate_latest(self.custom_registry).decode("utf-8") + assert 'logprep_bla{pipeline="1"} 1.0' in metric_output + + def test_gauge_metric_increment_twice_sets_metric(self): + metric = GaugeMetric( + name="bla", + description="empty description", + labels={"pipeline": "1"}, + registry=self.custom_registry, + ) + metric += 1 + metric += 1 + metric_output = generate_latest(self.custom_registry).decode("utf-8") + assert 'logprep_bla{pipeline="1"} 1.0' in metric_output + + +class TestHistogramMetric: + def setup_method(self): + self.custom_registry = CollectorRegistry() + + def test_init_tracker_returns_collector(self): + metric = HistogramMetric( + name="testmetric", + description="empty description", + labels={"A": "a"}, + registry=self.custom_registry, + ) + assert isinstance(metric.tracker, Histogram) + + def test_gauge_metric_increments_correctly(self): + metric = HistogramMetric( + name="bla", + description="empty description", + labels={"pipeline": "1"}, + registry=self.custom_registry, + ) + metric += 1 + metric_output = generate_latest(self.custom_registry).decode("utf-8") + assert re.search(r'logprep_bla_sum\{pipeline="1"\} 1\.0', metric_output) + assert re.search(r'logprep_bla_count\{pipeline="1"\} 1\.0', metric_output) + assert re.search(r'logprep_bla_bucket\{le=".*",pipeline="1"\} \d+', metric_output) + + def test_gauge_metric_increment_twice_sets_metric(self): + metric = HistogramMetric( + name="bla", + description="empty description", + labels={"pipeline": "1"}, + registry=self.custom_registry, + ) + metric += 1 + metric += 1 + metric_output = generate_latest(self.custom_registry).decode("utf-8") + assert re.search(r'logprep_bla_sum\{pipeline="1"\} 2\.0', metric_output) + assert re.search(r'logprep_bla_count\{pipeline="1"\} 2\.0', metric_output) + assert re.search(r'logprep_bla_bucket\{le=".*",pipeline="1"\} \d+', metric_output) + + class TestComponentMetric: class Metrics(Component.Metrics): """test class"""