Skip to content

feat: Alert Knob - #205

Open
sinapah wants to merge 6 commits into
mainfrom
feat/rules-customization
Open

sinapah wants to merge 6 commits into
mainfrom
feat/rules-customization

Conversation

@sinapah

@sinapah sinapah commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Issue

See canonical/prometheus-k8s-operator#857 for a test of how this feature would be implemented and used.

COS charms (prometheus-k8s, loki-k8s, mimir-coordinator, loki-coordinator) process alert rules provided by relations, applying Juju topology matchers and validation automatically. However, there is no standardized way for administrators to fine-tune these rules once deployed. This PR addresses the need for "Day-2" flexibility, allowing operators to patch or remove specific relation-derived alerts or inject custom rules without disrupting built-in monitoring or recording rules.

Solution

Adds a new cosl.rules_customization module with a single public class:

  • AlertRulesCustomization.from_yaml(config_string): parses the admin's YAML config and validates the schema at parse time.
  • AlertRulesCustomization.apply(relation_alerts): pure transformation that takes a Mapping[str, OfficialRuleFileFormat] (the same dict relation libraries already produce) and returns a Dict[str, OfficialRuleFileFormat] in the same shape, with remove/patch/add applied.

The config supports three optional top-level keys:

  • remove: drop matching alerting rules by exact match on alert, group, labels, or annotations. When group is the only selector, the entire group (including recording rules) is dropped.
  • patch: modify matching rules — rename (alert), replace expression (expr), change duration (for), or merge labels/annotations.
  • add: insert admin-authored rule groups under a fixed key (custom_alert_rules). No automatic topology injection. NOTE: WE HAVE DECIDED TO DROP THIS.

Operations are applied in order: remove → patch. Inputs are never mutated (deep-copied). Validation of the resulting rule expressions is the charm's responsibility after calling apply().

Checklist

  • I have added or updated relevant documentation.
  • PR title makes an appropriate release note and follows conventional commits syntax.
  • Merge target is the correct branch, and relevant tandem backport PRs opened.
  • This change warrants a release, so I have updated the project.version field in the pyproject.toml file.

Context

Testing Instructions

Upgrade Notes

CI resolves black 24.8.0 (Python 3.8) while local dev on newer interpreters
resolves black 26.x; the two styles disagree on multiline string literals
passed directly as call arguments. Assign such configs to local variables
before passing them to from_yaml(), which both styles format identically.
Comment thread src/cosl/__init__.py
Comment on lines +22 to +23
"AlertRulesCustomization",
"AlertRulesCustomizationError",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Look at canonical/prometheus-k8s-operator#857 to see how these are intended to be used.

@sinapah
sinapah marked this pull request as ready for review August 24, 2026 19:07
@sinapah
sinapah requested a review from a team as a code owner August 24, 2026 19:07
Comment thread src/cosl/rules_customization.py Outdated

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.

Would it help to have a feature file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Implemented in #206.

@sinapah sinapah mentioned this pull request Aug 28, 2026
4 tasks

## Overview

This module provides :class:`AlertRulesCustomization`, a pure transformation helper that

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.

Would it also work with recording rules?

``MetricsConsumer.alerts`` produce) and an admin-provided YAML customization config, and
returns the modified rules in the same format.

The customization config supports two top-level keys:

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.

Suggested change
The customization config supports two top-level keys:
The customization config supports the following top-level keys:

group_name,
identifier,
", ".join(changes),
)

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.

Should we impose an annotation indicating the rule went through AK? E.g.

annotations:
  _transformed_by_cos_lib: "expr changed; labels changed"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As discussed in real time, we seem to lean towards not doing this. At least not just yet.

WDYT @marcusboden? Would you say having this annotation would be helpful?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hi @sinapah. It might actually be helpful. But it's not too important. But we can also bring it in in a later iteration.

* feat: alert knob using pydantic

* fix: comments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants