Skip to content

Commit

Permalink
add countermetric equaltity
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Oct 13, 2023
1 parent 434d9d1 commit 3a622d4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 7 additions & 0 deletions logprep/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,20 @@ def init_tracker(self):
def __add__(self, other):
"""Add"""

@abstractmethod
def __eq__(self, __value: object) -> bool:
"""Equal"""


@define(kw_only=True)
class CounterMetric(Metric):
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):
Expand Down
12 changes: 12 additions & 0 deletions tests/unit/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 0 additions & 6 deletions tests/unit/processor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down

0 comments on commit 3a622d4

Please sign in to comment.