Skip to content

Commit

Permalink
[CCXDEV-13089] Add a metric to count rapid recommendations archives b…
Browse files Browse the repository at this point in the history
…y version (#252)

* add a metric to count rapid recommendations archives by version

* fix pre-commit

* don't expect empty result

* unregister metric

* fix some UTs

* fix UT
  • Loading branch information
juandspy authored Aug 30, 2024
1 parent 19439d7 commit 0dd9e99
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ccx_messaging/engines/ocp_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def __init__(
super().__init__(formatter, target_components, extract_timeout, extract_tmp_dir)

def process(self, broker, path):
"""Get results from applying Insihgts rules to the downloaded archive.
"""Get results from applying Insights rules to the downloaded archive.
The archive is extracted and processed if the `openshift_lightspeed.json`
file is not found within its root directory. The JSON resulting from applying
Expand Down
24 changes: 24 additions & 0 deletions ccx_messaging/watchers/stats_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import os
import tarfile
import time
import json

from insights.core.archives import TarExtractor, ZipExtractor
from insights_messaging.watchers import EngineWatcher
Expand All @@ -30,6 +31,7 @@
# Label to differentiate between OCP, OLS and HyperShift tarballs
ARCHIVE_TYPE_LABEL = "archive"
ARCHIVE_TYPE_VALUES = ["ocp", "hypershift", "ols"]
IO_GATHERING_REMOTE_CONFIG_LABEL = "version"


# pylint: disable=too-many-instance-attributes
Expand Down Expand Up @@ -109,6 +111,12 @@ def __init__(self, prometheus_port=8000):
[ARCHIVE_TYPE_LABEL],
)

self._gathering_conditions_remote_configuration_version = Counter(
"gathering_conditions_remote_configuration_version",
"Counter of times a given configuration is seen",
[IO_GATHERING_REMOTE_CONFIG_LABEL],
)

self._start_time = None
self._downloaded_time = None
self._processed_time = None
Expand Down Expand Up @@ -173,6 +181,21 @@ def on_extract_with_extractor(self, extraction: TarExtractor | ZipExtractor) ->
self._archive_metadata["size"]
)

# Set the IO remote configuration version
remote_config_path = os.path.join(
extraction.tmp_dir,
"insights-operator",
"remote-configuration.json")
try:
with open(remote_config_path, "rb") as f:
data = json.load(f)
version = data["version"]
self._gathering_conditions_remote_configuration_version.labels(version).inc()
except FileNotFoundError:
LOG.debug("this archive didn't use remote-configurations")
except Exception as e:
LOG.error("cannot read remote-configuration.json", exc_info=e)

def on_download(self, path):
"""On downloaded event handler."""
LOG.debug("Receiving 'on_download' callback")
Expand Down Expand Up @@ -278,3 +301,4 @@ def __del__(self):
REGISTRY.unregister(self._process_duration)
REGISTRY.unregister(self._publish_duration)
REGISTRY.unregister(self._processed_timeout_total)
REGISTRY.unregister(self._gathering_conditions_remote_configuration_version)
19 changes: 18 additions & 1 deletion test/engines/ocp_engine_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pytest

from ccx_messaging.engines.ocp_engine import OCPEngine
from ccx_messaging.watchers.stats_watcher import StatsWatcher


def test_init():
Expand Down Expand Up @@ -68,7 +69,7 @@ def test_process_extract_correct_data():


def test_process_extract_ols_archive():
"""Basic test for OCPEngine."""
"""Basic test for OCPEngine with an OLS file."""
e = OCPEngine(HumanReadableFormat)
e.watchers = []
e.extract_tmp_dir = ""
Expand All @@ -78,3 +79,19 @@ def test_process_extract_ols_archive():

result = e.process(broker, path)
assert result == "{}"


def test_process_extract_rapid_recommendation_archive():
"""Basic test for OCPEngine with a rapid recommendation file."""
e = OCPEngine(HumanReadableFormat)
sw = StatsWatcher()
e.watchers = [sw]
e.extract_tmp_dir = ""

broker = None
path = "test/rapid-recommendations.tar.gz"

result = e.process(broker, path)
assert result != "{}"
assert sw._gathering_conditions_remote_configuration_version.labels(
"1.1.0")._value.get() == 1
Binary file added test/rapid-recommendations.tar.gz
Binary file not shown.

0 comments on commit 0dd9e99

Please sign in to comment.