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 #490

Merged
merged 8 commits into from
Feb 10, 2025
45 changes: 40 additions & 5 deletions lib/charms/loki_k8s/v1/loki_push_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def __init__(self, ...):
RelationRole,
WorkloadEvent,
)
from ops.framework import EventBase, EventSource, Object, ObjectEvents
from ops.framework import BoundEvent, EventBase, EventSource, Object, ObjectEvents
from ops.jujuversion import JujuVersion
from ops.model import Container, ModelError, Relation
from ops.pebble import APIError, ChangeError, Layer, PathError, ProtocolError
Expand All @@ -546,7 +546,7 @@ def __init__(self, ...):

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

PYDEPS = ["cosl"]

Expand Down Expand Up @@ -1543,10 +1543,13 @@ def __init__(
alert_rules_path: str = DEFAULT_ALERT_RULES_RELATIVE_PATH,
recursive: bool = False,
skip_alert_topology_labeling: bool = False,
*,
forward_alert_rules: bool = True,
):
super().__init__(charm, relation_name)
self._charm = charm
self._relation_name = relation_name
self._forward_alert_rules = forward_alert_rules
self.topology = JujuTopology.from_charm(charm)

try:
Expand All @@ -1569,7 +1572,8 @@ def _handle_alert_rules(self, relation):
alert_rules = (
AlertRules(None) if self._skip_alert_topology_labeling else AlertRules(self.topology)
)
alert_rules.add_path(self._alert_rules_path, recursive=self._recursive)
if self._forward_alert_rules:
alert_rules.add_path(self._alert_rules_path, recursive=self._recursive)
alert_rules_as_dict = alert_rules.as_dict()

relation.data[self._charm.app]["metadata"] = json.dumps(self.topology.as_dict())
Expand Down Expand Up @@ -1617,6 +1621,9 @@ def __init__(
alert_rules_path: str = DEFAULT_ALERT_RULES_RELATIVE_PATH,
recursive: bool = True,
skip_alert_topology_labeling: bool = False,
*,
refresh_event: Optional[Union[BoundEvent, List[BoundEvent]]] = None,
forward_alert_rules: bool = True,
):
"""Construct a Loki charm client.

Expand All @@ -1642,6 +1649,9 @@ def __init__(
alert_rules_path: a string indicating a path where alert rules can be found
recursive: Whether to scan for rule files recursively.
skip_alert_topology_labeling: whether to skip the alert topology labeling.
forward_alert_rules: a boolean flag to toggle forwarding of charmed alert rules.
refresh_event: an optional bound event or list of bound events which
will be observed to re-set scrape job data (IP address and others)

Raises:
RelationNotFoundError: If there is no relation in the charm's metadata.yaml
Expand All @@ -1667,14 +1677,26 @@ def __init__(
charm, relation_name, RELATION_INTERFACE_NAME, RelationRole.requires
)
super().__init__(
charm, relation_name, alert_rules_path, recursive, skip_alert_topology_labeling
charm,
relation_name,
alert_rules_path,
recursive,
skip_alert_topology_labeling,
forward_alert_rules=forward_alert_rules,
)
events = self._charm.on[relation_name]
self.framework.observe(self._charm.on.upgrade_charm, self._on_lifecycle_event)
self.framework.observe(self._charm.on.config_changed, self._on_lifecycle_event)
self.framework.observe(events.relation_joined, self._on_logging_relation_joined)
self.framework.observe(events.relation_changed, self._on_logging_relation_changed)
self.framework.observe(events.relation_departed, self._on_logging_relation_departed)

if refresh_event:
if not isinstance(refresh_event, list):
refresh_event = [refresh_event]
for ev in refresh_event:
self.framework.observe(ev, self._on_lifecycle_event)

def _on_lifecycle_event(self, _: HookEvent):
"""Update require relation data on charm upgrades and other lifecycle events.

Expand Down Expand Up @@ -2550,10 +2572,17 @@ def __init__(
alert_rules_path: str = DEFAULT_ALERT_RULES_RELATIVE_PATH,
recursive: bool = True,
skip_alert_topology_labeling: bool = False,
refresh_event: Optional[Union[BoundEvent, List[BoundEvent]]] = None,
forward_alert_rules: bool = True,
):
_PebbleLogClient.check_juju_version()
super().__init__(
charm, relation_name, alert_rules_path, recursive, skip_alert_topology_labeling
charm,
relation_name,
alert_rules_path,
recursive,
skip_alert_topology_labeling,
forward_alert_rules=forward_alert_rules,
)
self._charm = charm
self._relation_name = relation_name
Expand All @@ -2564,6 +2593,12 @@ def __init__(
self.framework.observe(on.relation_departed, self._update_logging)
self.framework.observe(on.relation_broken, self._update_logging)

if refresh_event:
if not isinstance(refresh_event, list):
refresh_event = [refresh_event]
for ev in refresh_event:
self.framework.observe(ev, self._update_logging)

for container_name in self._charm.meta.containers.keys():
snake_case_container_name = container_name.replace("-", "_")
self.framework.observe(
Expand Down
Loading