diff --git a/logprep/metrics/metrics.py b/logprep/metrics/metrics.py index aa6c08b55..2c61e699e 100644 --- a/logprep/metrics/metrics.py +++ b/logprep/metrics/metrics.py @@ -79,6 +79,10 @@ def init_tracker(self): def __add__(self, other): """Add""" + @abstractmethod + def __eq__(self, __value: object) -> bool: + """Equal""" + @define(kw_only=True) class CounterMetric(Metric): @@ -86,6 +90,9 @@ def __add__(self, other): self.tracker.labels(**self.labels).inc(other) return self + def __eq__(self, __value: object) -> bool: + return int(self.tracker.collect()[-1].samples[0].value) == int(__value) + @define(kw_only=True) class HistogramMetric(Metric): diff --git a/tests/unit/metrics/test_metrics.py b/tests/unit/metrics/test_metrics.py index 023929de5..99e93663c 100644 --- a/tests/unit/metrics/test_metrics.py +++ b/tests/unit/metrics/test_metrics.py @@ -55,3 +55,15 @@ def test_counter_metric_increments_second(self): metric += 1 metric_output = generate_latest(self.custom_registry).decode("utf-8") assert 'logprep_bla_total{pipeline="1"} 2.0' in metric_output + + def test_metric_check_equality_integer(self): + metric = CounterMetric( + name="bla", + description="empty description", + labels={"pipeline": "1"}, + ) + metric.init_tracker() + assert metric == 0 + metric += 1 + assert metric == 1.0 + assert metric == 1 diff --git a/tests/unit/processor/base.py b/tests/unit/processor/base.py index bf7dbf73c..fa90ed647 100644 --- a/tests/unit/processor/base.py +++ b/tests/unit/processor/base.py @@ -11,13 +11,11 @@ import pytest import requests import responses -from prometheus_client import CollectorRegistry, generate_latest from ruamel.yaml import YAML from logprep.abc.processor import Processor from logprep.factory import Factory from logprep.framework.rule_tree.rule_tree import RuleTree -from logprep.metrics import metrics from logprep.processor.base.exceptions import ProcessingWarning from logprep.util.helper import camel_to_snake from logprep.util.json_handling import list_json_files_in_directory @@ -89,8 +87,6 @@ def setup_method(self) -> None: """ TimeMeasurement.TIME_MEASUREMENT_ENABLED = False TimeMeasurement.APPEND_TO_EVENT = False - self.custom_registry = CollectorRegistry() - metrics.LOGPREP_REGISTRY = self.custom_registry self.patchers = [] for name, kwargs in self.mocks.items(): patcher = mock.patch(name, **kwargs) @@ -111,14 +107,12 @@ def test_is_a_processor_implementation(self): assert isinstance(self.object, Processor) def test_process(self): - before = generate_latest() document = { "event_id": "1234", "message": "user root logged in", } count = self.object.metrics.number_of_processed_events self.object.process(document) - after = generate_latest() assert self.object.metrics.number_of_processed_events == count + 1 def test_generic_specific_rule_trees(self):