Skip to content

Commit

Permalink
fix amides tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ekneg54 committed Oct 19, 2023
1 parent a8266a3 commit 7b8a29f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion logprep/metrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Metric(ABC):
)
trackers: dict = None
_registry: CollectorRegistry = field(default=None)
_prefix: str = "logprep_"
_prefix: str = field(default="logprep_")

@property
def fullname(self):
Expand Down
14 changes: 7 additions & 7 deletions logprep/processor/amides/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,49 +132,49 @@ class Metrics(Processor.Metrics):
total_cmdlines: CounterMetric = field(
factory=lambda: CounterMetric(
description="Total number of command lines processed.",
name="total_cmdlines",
name="amides_total_cmdlines",
)
)
"""Total number of command lines processed."""
new_results: GaugeMetric = field(
factory=lambda: GaugeMetric(
description="Number of command lines that triggered detection and rule attribution.",
name="new_results",
name="amides_new_results",
)
)
"""Number of command lines that triggered detection and rule attribution."""
cached_results: GaugeMetric = field(
factory=lambda: GaugeMetric(
description="Number of command lines that could be resolved from cache.",
name="cached_results",
name="amides_cached_results",
)
)
"""Number of command lines that could be resolved from cache."""
num_cache_entries: GaugeMetric = field(
factory=lambda: GaugeMetric(
description="Absolute number of current cache entries.",
name="num_cache_entries",
name="amides_num_cache_entries",
)
)
"""Absolute number of current cache entries."""
cache_load: GaugeMetric = field(
factory=lambda: GaugeMetric(
description="Mean processing time of command lines classified by the misuse detector.",
name="cache_load",
name="amides_cache_load",
)
)
"""Relative cache load."""
mean_misuse_detection_time: HistogramMetric = field(
factory=lambda: HistogramMetric(
description="Mean processing time of command lines classified by the misuse detector.",
name="mean_misuse_detection_time",
name="amides_mean_misuse_detection_time",
)
)
"""Mean processing time of command lines classified by the misuse detector."""
mean_rule_attribution_time: HistogramMetric = field(
factory=lambda: HistogramMetric(
description="Mean processing time of command lines attributed by the rule attributor.",
name="mean_rule_attribution_time",
name="amides_mean_rule_attribution_time",
)
)
"""Mean processing time of command lines attributed by the rule attributor."""
Expand Down
25 changes: 23 additions & 2 deletions tests/unit/processor/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

import itertools
import json
import re
from copy import deepcopy
from functools import partial
from logging import getLogger
from pathlib import Path
from typing import Callable
Expand All @@ -20,6 +22,7 @@
from logprep.factory import Factory
from logprep.framework.rule_tree.rule_tree import RuleTree
from logprep.metrics.metrics import CounterMetric, HistogramMetric, Metric
from logprep.util.helper import camel_to_snake
from logprep.util.json_handling import list_json_files_in_directory
from tests.unit.component.base import BaseComponentTestCase

Expand Down Expand Up @@ -279,9 +282,10 @@ def test_rule_has_metric(self, metric_name, metric_class):
assert isinstance(metric_instance, metric_class)

@staticmethod
def asdict_filter(attribute, value):
def asdict_filter(attribute, value, block_list=None):
"""Returns all attributes not in block list or None"""
block_list = ["_labels", "_prefix"]
if block_list is None:
block_list = ["_labels", "_prefix"]
return not any((attribute.name in block_list, value is None, isinstance(value, Callable)))

def test_custom_metrics_are_metric_objects(self):
Expand All @@ -295,3 +299,20 @@ def test_no_metrics_with_same_name(self):
pairs = itertools.combinations(metric_attributes.values(), 2)
for metric1, metric2 in pairs:
assert metric1.name != metric2.name, f"{metric1.name} == {metric2.name}"

def test_custom_metrics_adds_custom_prefix_to_metrics_name(self):
block_list = [
"_labels",
"_prefix",
"processing_time_per_event",
"number_of_processed_events",
]
metric_attributes = asdict(
self.object.metrics,
filter=partial(self.asdict_filter, block_list=block_list),
recurse=False,
)
for attribute in metric_attributes.values():
assert attribute.fullname.startswith(
f"logprep_{camel_to_snake(self.object.__class__.__name__)}"
), f"{attribute.fullname}, logprep_{camel_to_snake(self.object.__class__.__name__)}"

0 comments on commit 7b8a29f

Please sign in to comment.