Skip to content

Commit

Permalink
add metric tests for gauge and histogram
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Oct 23, 2023
1 parent cfd9a89 commit 44786ad
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
5 changes: 1 addition & 4 deletions logprep/framework/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
"""
Expand Down
88 changes: 86 additions & 2 deletions tests/unit/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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"""
Expand Down

0 comments on commit 44786ad

Please sign in to comment.