Skip to content

Commit

Permalink
fix some other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Oct 13, 2023
1 parent 3a622d4 commit 00e051d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion logprep/abc/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def process(self, event: dict):
A dictionary representing a log event.
"""
self.metrics.number_of_processed_events += 1
self._logger.debug(f"{self.describe()} processing event {event}")
self._process_rule_tree(event, self._specific_tree)
self._process_rule_tree(event, self._generic_tree)
Expand Down Expand Up @@ -189,7 +190,6 @@ def _process_rule(event, rule):
reduce(_process_rule, (event, *tree.get_matching_rules(event)))

def _apply_rules_wrapper(self, event: dict, rule: "Rule"):
self.metrics.number_of_processed_events += 1
try:
self._apply_rules(event, rule)
except ProcessingWarning as error:
Expand Down
9 changes: 8 additions & 1 deletion logprep/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,16 @@ def __eq__(self, __value: object) -> bool:

@define(kw_only=True)
class CounterMetric(Metric):
@property
def _value(self) -> int:
return int(self.tracker.collect()[-1].samples[0].value)

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)
return self._value == int(__value)


@define(kw_only=True)
Expand All @@ -100,6 +104,9 @@ def __add__(self, other):
self.tracker.labels(**self.labels).observe(other)
return self

def __eq__(self, __value: object) -> bool:
raise NotImplementedError


def calculate_new_average(current_average, next_sample, sample_counter):
"""Calculate a new average by combining a new sample with a sample counter"""
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/metrics/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def test_counter_metric_increments_second(self):
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):
def test_metric_check_equality(self):
metric = CounterMetric(
name="bla",
description="empty description",
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/processor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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 CounterMetric
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 @@ -107,13 +108,13 @@ def test_is_a_processor_implementation(self):
assert isinstance(self.object, Processor)

def test_process(self):
assert self.object.metrics.number_of_processed_events == 0
document = {
"event_id": "1234",
"message": "user root logged in",
}
count = self.object.metrics.number_of_processed_events
self.object.process(document)
assert self.object.metrics.number_of_processed_events == count + 1
assert self.object.metrics.number_of_processed_events == 1

def test_generic_specific_rule_trees(self):
assert isinstance(self.object._generic_tree, RuleTree)
Expand All @@ -124,7 +125,7 @@ def test_generic_specific_rule_trees_not_empty(self):
assert self.object._specific_tree.get_size() > 0

def test_event_processed_count(self):
assert isinstance(self.object.metrics.number_of_processed_events, int)
assert isinstance(self.object.metrics.number_of_processed_events, CounterMetric)

def test_events_processed_count_counts(self):
assert self.object.metrics.number_of_processed_events == 0
Expand All @@ -140,9 +141,8 @@ def test_field_exists(self):
event = {"a": {"b": "I do not matter"}}
assert self.object._field_exists(event, "a.b")

@mock.patch("logging.Logger.isEnabledFor", return_value=True)
@mock.patch("logging.Logger.debug")
def test_load_rules_with_debug(self, mock_debug, _):
def test_load_rules_with_debug(self, mock_debug):
self.object.load_rules(
specific_rules_targets=self.specific_rules_dirs,
generic_rules_targets=self.generic_rules_dirs,
Expand Down

0 comments on commit 00e051d

Please sign in to comment.