Skip to content

Commit

Permalink
fix metrics tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Oct 13, 2023
1 parent 8f1ef5a commit 434d9d1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
27 changes: 20 additions & 7 deletions tests/unit/metrics/test_metrics.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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
23 changes: 7 additions & 16 deletions tests/unit/processor/base.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit 434d9d1

Please sign in to comment.