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

feat: add support for disabling alert rules forwarding #252

Merged
merged 19 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions charmcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,8 @@ config:
Ref (__path_exclude__): https://grafana.com/docs/loki/latest/send-data/promtail/scraping/
type: string
default: ""
disable_forwarding_alert_rules:
description: >
Toggle forwarding of alert rules.
type: boolean
default: false
13 changes: 10 additions & 3 deletions lib/charms/prometheus_k8s/v1/prometheus_remote_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 5
LIBPATCH = 6

PYDEPS = ["cosl"]

Expand Down Expand Up @@ -401,6 +401,7 @@ def __init__(
charm: CharmBase,
relation_name: str = DEFAULT_CONSUMER_NAME,
alert_rules_path: str = DEFAULT_ALERT_RULES_RELATIVE_PATH,
disable_forwarding_alert_rules: bool = False,
):
"""API to manage a required relation with the `prometheus_remote_write` interface.

Expand All @@ -409,6 +410,7 @@ def __init__(
relation_name: Name of the relation with the `prometheus_remote_write` interface as
defined in metadata.yaml.
alert_rules_path: Path of the directory containing the alert rules.
disable_forwarding_alert_rules: Flag to toggle alert rule forwarding.

Raises:
RelationNotFoundError: If there is no relation in the charm's metadata.yaml
Expand Down Expand Up @@ -437,6 +439,7 @@ def __init__(
self._charm = charm
self._relation_name = relation_name
self._alert_rules_path = alert_rules_path
self._disable_alerts = disable_forwarding_alert_rules

self.topology = JujuTopology.from_charm(charm)

Expand All @@ -453,6 +456,9 @@ def __init__(
self.framework.observe(
self._charm.on.upgrade_charm, self._push_alerts_to_all_relation_databags
)
self.framework.observe(
self._charm.on.config_changed, self._push_alerts_to_all_relation_databags
)

def _on_relation_broken(self, event: RelationBrokenEvent) -> None:
self.on.endpoints_changed.emit(relation_id=event.relation.id)
Expand Down Expand Up @@ -484,11 +490,12 @@ def _push_alerts_to_relation_databag(self, relation: Relation) -> None:
return

alert_rules = AlertRules(query_type="promql", topology=self.topology)
alert_rules.add_path(self._alert_rules_path)
if not self._disable_alerts:
alert_rules.add_path(self._alert_rules_path)

alert_rules_as_dict = alert_rules.as_dict()

if alert_rules_as_dict:
if alert_rules_as_dict or self._disable_alerts:
relation.data[self._charm.app]["alert_rules"] = json.dumps(alert_rules_as_dict)

def reload_alerts(self) -> None:
Expand Down
6 changes: 5 additions & 1 deletion src/grafana_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ def __init__(self, *args):
self.status = CompoundStatus()

charm_root = self.charm_dir.absolute()
self._disable_alerts = cast(bool, self.config["disable_forwarding_alert_rules"])
self.loki_rules_paths = RulesMapping(
# TODO how to inject topology only for this charm's own rules?
# FIXED: this is already handled by reusing the *Rules classes
Expand All @@ -155,8 +156,11 @@ def __init__(self, *args):
rules.src.mkdir(parents=True, exist_ok=True)
shutil.copytree(rules.src, rules.dest, dirs_exist_ok=True)

alert_rules_path = self.metrics_rules_paths.dest
self._remote_write = PrometheusRemoteWriteConsumer(
self, alert_rules_path=self.metrics_rules_paths.dest
self,
alert_rules_path=alert_rules_path,
disable_forwarding_alert_rules=self._disable_alerts,
)

self._loki_consumer = LokiPushApiConsumer(
Expand Down
Loading