Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Break the Rules monolith. Add support for recording rules. #433

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
364 changes: 243 additions & 121 deletions lib/charms/prometheus_k8s/v0/prometheus_remote_write.py

Large diffs are not rendered by default.

558 changes: 353 additions & 205 deletions lib/charms/prometheus_k8s/v0/prometheus_scrape.py

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ jsonschema
ops
pyaml
requests
typing_extensions
lightkube >= 0.11
lightkube-models >= 1.22.0.4
1 change: 1 addition & 0 deletions tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ def setUp(self, *unused):

self.rel_id = self.harness.add_relation(RELATION_NAME, "remote-app")
self.harness.add_relation_unit(self.rel_id, "remote-app/0")
self.harness.set_leader(True)

@patch("charm.KubernetesServicePatch", lambda x, y: None)
@k8s_resource_multipatch
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def test_a_bad_alert_rules_logs_an_error(self):

messages = sorted(logger.output)
self.assertEqual(len(messages), 1)
self.assertIn("Failed to read alert rules from bad_yaml.rule", messages[0])
self.assertIn("Failed to read rules from bad_yaml.rule", messages[0])


def sorted_matchers(matchers) -> str:
Expand Down Expand Up @@ -841,6 +841,6 @@ def test_alert_rules(self):
baked_in_alert_rules_as_they_appear_in_reldata = json.loads(data["alert_rules"])

tool = self.harness.charm.tool
valid, errs = tool.validate_alert_rules(baked_in_alert_rules_as_they_appear_in_reldata)
valid, errs = tool.validate_rules(baked_in_alert_rules_as_they_appear_in_reldata)
self.assertEqual(valid, True)
self.assertEqual(errs, "")
13 changes: 11 additions & 2 deletions tests/unit/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@

import copy
import unittest
from unittest import mock

import deepdiff
from charms.prometheus_k8s.v0.prometheus_scrape import _dedupe_job_names
from charms.prometheus_k8s.v0.prometheus_scrape import MetricsEndpointConsumer


class TestFunctions(unittest.TestCase):
def setUp(self):
self.consumer = mock.create_autospec(MetricsEndpointConsumer)
self.consumer._dedupe_job_names = lambda x: MetricsEndpointConsumer._dedupe_job_names(
self.consumer, x
)

def test_dedupe_job_names(self):
jobs = [
{
Expand Down Expand Up @@ -60,6 +67,8 @@ def test_dedupe_job_names(self):
"static_configs": [{"targets": ["localhost:9090"]}],
},
]
self.assertTrue(len(deepdiff.DeepDiff(_dedupe_job_names(jobs), expected)) == 0)
self.assertTrue(
len(deepdiff.DeepDiff(self.consumer._dedupe_job_names(jobs), expected)) == 0
)
# Make sure the function does not modify its argument
self.assertEqual(jobs, jobs_original)
4 changes: 2 additions & 2 deletions tests/unit/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def setUp(self):
@unittest.mock.patch("platform.machine", lambda: "x86_64")
def test_returns_errors_on_bad_rule_file(self):
tool = self.harness.charm.tool
valid, errs = tool.validate_alert_rules(
valid, errs = tool.validate_rules(
{
"groups": [
{
Expand All @@ -171,7 +171,7 @@ def test_returns_errors_on_bad_rule_file(self):
@unittest.mock.patch("platform.machine", lambda: "x86_64")
def test_successfully_validates_good_alert_rules(self):
tool = self.harness.charm.tool
valid, errs = tool.validate_alert_rules(
valid, errs = tool.validate_rules(
{
"groups": [
{
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ deps =
integration: pytest-operator==1.0.0b1
commands =
charm: mypy {[vars]src_path} {posargs}
lib: mypy --python-version 3.5 {[vars]lib_path} {posargs}
lib: mypy --python-version 3.8 {[vars]lib_path} {posargs}
unit: mypy {[vars]tst_path}/unit {posargs}
integration: mypy {[vars]tst_path}/integration {posargs}

Expand Down