Skip to content
Draft
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
17 changes: 15 additions & 2 deletions lib/charms/loki_k8s/v1/loki_push_api.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LIBPATCH needs to be bumped

Original file line number Diff line number Diff line change
Expand Up @@ -1328,11 +1328,24 @@ def __init__(

@staticmethod
def _inject_extra_labels_to_alert_rules(rules: Dict, extra_alert_labels: Dict) -> Dict:
"""Return a copy of the rules dict with extra labels injected."""
"""Return a copy of the rules dict with extra labels injected.

Labels whose value is None or an empty string are removed from every
rule rather than being set. If removing labels leaves the ``labels``
dict empty, the key is dropped from the rule entirely.
"""
result = copy.deepcopy(rules)
labels_to_drop = {k for k, v in extra_alert_labels.items() if v is None or v == ""}
labels_to_set = {k: v for k, v in extra_alert_labels.items() if k not in labels_to_drop}

for group in result.get("groups", []):
for rule in group.get("rules", []):
rule.setdefault("labels", {}).update(extra_alert_labels)
rule_labels = rule.setdefault("labels", {})
rule_labels.update(labels_to_set)
for key in labels_to_drop:
rule_labels.pop(key, None)
if not rule_labels:
del rule["labels"]
return result

def _handle_alert_rules(self, relation):
Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dev = [
"pyright",
# Unit
"pytest",
"pytest-bdd",
"coverage[toml]",
"ops[testing]",
"responses",
Expand Down Expand Up @@ -86,3 +87,8 @@ markers = ["setup", "work", "teardown"]
[tool.codespell]
skip = ".git,.tox,build,venv*"
ignore-words-list = "assertIn"

[dependency-groups]
dev = [
"pytest-bdd>=8.1.0",
]
123 changes: 123 additions & 0 deletions tests/unit/features/inject_extra_labels.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
Feature: Inject extra labels to alert rules

Scenario: Extra labels are appended to all rules
Given rules
"""
groups:
- rules:
- labels:
severity: critical
- labels:
severity: warning
"""
When extra labels are injected
"""
environment: production
team: observability
"""
Then modified rules match
"""
groups:
- rules:
- labels:
severity: critical
environment: production
team: observability
- labels:
severity: warning
environment: production
team: observability
"""

Scenario: Extra labels override existing labels with the same key
Given rules
"""
groups:
- rules:
- labels:
severity: warning
team: old-team
"""
When extra labels are injected
"""
team: new-team
"""
Then modified rules match
"""
groups:
- rules:
- labels:
severity: warning
team: new-team
"""

Scenario: Empty extra labels leaves rules unchanged
Given rules
"""
groups:
- rules:
- labels:
severity: warning
team: some-team
"""
When extra labels are injected
"""
---
"""
Then modified rules match
"""
groups:
- rules:
- labels:
severity: warning
team: some-team
"""

Scenario: Rules without a labels key get labels added
Given rules
"""
groups:
- rules:
- alert: NoLabelsAlert
"""
When extra labels are injected
"""
env: staging
"""
Then modified rules match
"""
groups:
- rules:
- alert: NoLabelsAlert
labels:
env: staging
"""

Scenario: Empty label values removes the label
Given rules
"""
groups:
- rules:
- name: first
labels:
env: env1
- name: second
labels:
env: env2
foo: bar
severity: warning
"""
When extra labels are injected
"""
env: null
foo: ""
"""
Then modified rules match
"""
groups:
- rules:
- name: first
- name: second
labels:
severity: warning
"""
31 changes: 31 additions & 0 deletions tests/unit/test_inject_extra_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Tests for ConsumerBase._inject_extra_labels_to_alert_rules using pytest-bdd."""


import yaml
from charms.loki_k8s.v1.loki_push_api import ConsumerBase
from pytest_bdd import given, scenarios, then, when

scenarios("features/inject_extra_labels.feature")


@given("rules", target_fixture="rules")
def given_rules(docstring):
"""Parse the alert rules from the docstring fixture."""
return yaml.safe_load(docstring)


@when("extra labels are injected", target_fixture="modified_rules")
def when_extra_labels_injected(docstring, rules):
"""Inject extra labels using the method under test."""
extra_labels = yaml.safe_load(docstring) or {}

return ConsumerBase._inject_extra_labels_to_alert_rules(
rules, extra_labels
)


@then("modified rules match")
def then_modified_rules_match(docstring, modified_rules):
"""Verify every rule has the extra labels."""
expected = yaml.safe_load(docstring)
assert expected == modified_rules
71 changes: 71 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading