From 434d9d11fb6c511a43e96ee68e1905f9d25a6def Mon Sep 17 00:00:00 2001 From: ekneg54 Date: Fri, 13 Oct 2023 15:17:11 +0000 Subject: [PATCH] fix metrics tests --- tests/unit/metrics/test_metrics.py | 27 ++++++++++++++++++++------- tests/unit/processor/base.py | 23 +++++++---------------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/tests/unit/metrics/test_metrics.py b/tests/unit/metrics/test_metrics.py index b27b6adc8..023929de5 100644 --- a/tests/unit/metrics/test_metrics.py +++ b/tests/unit/metrics/test_metrics.py @@ -1,18 +1,19 @@ # pylint: disable=missing-docstring -# pylint: disable=no-self-use # pylint: disable=protected-access +# pylint: disable=attribute-defined-outside-init + from prometheus_client import CollectorRegistry, Counter, generate_latest -import logprep.metrics.metrics as metrics +from logprep.metrics import metrics from logprep.metrics.metrics import CounterMetric -custom_registry = CollectorRegistry() - -metrics.LOGPREP_REGISTRY = custom_registry - class TestsMetrics: + def setup_method(self): + self.custom_registry = CollectorRegistry() + metrics.LOGPREP_REGISTRY = self.custom_registry + def test_init_tracker_creates_metric(self): metric = CounterMetric( name="testmetric", @@ -40,5 +41,17 @@ def test_counter_metric_increments_correctly(self): ) metric.init_tracker() metric += 1 - metric_output = generate_latest(custom_registry).decode("utf-8") + 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_second(self): + metric = CounterMetric( + name="bla", + description="empty description", + labels={"pipeline": "1"}, + ) + metric.init_tracker() + metric += 1 + metric += 1 + metric_output = generate_latest(self.custom_registry).decode("utf-8") + assert 'logprep_bla_total{pipeline="1"} 2.0' in metric_output diff --git a/tests/unit/processor/base.py b/tests/unit/processor/base.py index 8c22b0e32..bf7dbf73c 100644 --- a/tests/unit/processor/base.py +++ b/tests/unit/processor/base.py @@ -1,10 +1,8 @@ # pylint: disable=missing-module-docstring # pylint: disable=protected-access +# pylint: disable=attribute-defined-outside-init import json -import os -import shutil -import tempfile from copy import deepcopy from logging import getLogger from pathlib import Path @@ -13,20 +11,13 @@ import pytest import requests import responses -from prometheus_client import ( - REGISTRY, - CollectorRegistry, - generate_latest, - multiprocess, - values, -) +from prometheus_client import CollectorRegistry, generate_latest from ruamel.yaml import YAML -from logprep import metrics from logprep.abc.processor import Processor from logprep.factory import Factory from logprep.framework.rule_tree.rule_tree import RuleTree -from logprep.metrics.metrics import Metric +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 @@ -98,8 +89,8 @@ def setup_method(self) -> None: """ TimeMeasurement.TIME_MEASUREMENT_ENABLED = False TimeMeasurement.APPEND_TO_EVENT = False - self.registry = CollectorRegistry() - metrics.LOGPREP_REGISTRY = self.registry + self.custom_registry = CollectorRegistry() + metrics.LOGPREP_REGISTRY = self.custom_registry self.patchers = [] for name, kwargs in self.mocks.items(): patcher = mock.patch(name, **kwargs) @@ -120,14 +111,14 @@ def test_is_a_processor_implementation(self): assert isinstance(self.object, Processor) def test_process(self): - before = generate_latest(self.registry) + 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(self.registry) + after = generate_latest() assert self.object.metrics.number_of_processed_events == count + 1 def test_generic_specific_rule_trees(self):