Skip to content

Commit

Permalink
Send events from PrometheusRemoteWriteProvider (#440)
Browse files Browse the repository at this point in the history
  • Loading branch information
rbarry82 authored Jan 26, 2023
1 parent ccc5385 commit cb7fdd2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
49 changes: 44 additions & 5 deletions lib/charms/prometheus_k8s/v0/prometheus_remote_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

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


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -721,6 +721,33 @@ def endpoints(self) -> List[Dict[str, str]]:
return endpoints


class PrometheusRemoteWriteAlertsChangedEvent(EventBase):
"""Event emitted when Prometheus remote_write alerts change."""

def __init__(self, handle, relation_id):
super().__init__(handle)
self.relation_id = relation_id

def snapshot(self):
"""Save Prometheus remote_write information."""
return {"relation_id": self.relation_id}

def restore(self, snapshot):
"""Restore Prometheus remote_write information."""
self.relation_id = snapshot["relation_id"]


class PrometheusRemoteWriteProviderConsumersChangedEvent(EventBase):
"""Event emitted when Prometheus remote_write alerts change."""


class PrometheusRemoteWriteProviderEvents(ObjectEvents):
"""Event descriptor for events raised by `PrometheusRemoteWriteProvider`."""

alert_rules_changed = EventSource(PrometheusRemoteWriteAlertsChangedEvent)
consumers_changed = EventSource(PrometheusRemoteWriteProviderConsumersChangedEvent)


class PrometheusRemoteWriteProvider(Object):
"""API that manages a provided `prometheus_remote_write` relation.
Expand Down Expand Up @@ -760,6 +787,8 @@ def __init__(self, *args):
name to differentiate between "incoming" and "outgoing" remote write interactions is necessary.
"""

on = PrometheusRemoteWriteProviderEvents()

def __init__(
self,
charm: CharmBase,
Expand Down Expand Up @@ -810,15 +839,25 @@ def __init__(
on_relation = self._charm.on[self._relation_name]
self.framework.observe(
on_relation.relation_created,
self._on_relation_change,
self._on_consumers_changed,
)
self.framework.observe(
on_relation.relation_joined,
self._on_relation_change,
self._on_consumers_changed,
)
self.framework.observe(
on_relation.relation_changed,
self._on_relation_changed,
)

def _on_consumers_changed(self, event: RelationEvent) -> None:
if not isinstance(event, RelationBrokenEvent):
self.update_endpoint(event.relation)
self.on.consumers_changed.emit()

def _on_relation_change(self, event: RelationEvent) -> None:
self.update_endpoint(event.relation)
def _on_relation_changed(self, event: RelationEvent) -> None:
"""Flag Providers that data has changed so they can re-read alerts."""
self.on.alert_rules_changed.emit(event.relation.id)

def update_endpoint(self, relation: Optional[Relation] = None) -> None:
"""Triggers programmatically the update of the relation data.
Expand Down
5 changes: 2 additions & 3 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,8 @@ def __init__(self, *args):
self.framework.observe(self.on.update_status, self._update_status)
self.framework.observe(self.ingress.on.ready_for_unit, self._on_ingress_ready)
self.framework.observe(self.ingress.on.revoked_for_unit, self._on_ingress_revoked)
self.framework.observe(self.on.receive_remote_write_relation_created, self._configure)
self.framework.observe(self.on.receive_remote_write_relation_changed, self._configure)
self.framework.observe(self.on.receive_remote_write_relation_broken, self._configure)
self.framework.observe(self.remote_write_provider.on.alert_rules_changed, self._configure)
self.framework.observe(self.remote_write_provider.on.consumers_changed, self._configure)
self.framework.observe(self.metrics_consumer.on.targets_changed, self._configure)
self.framework.observe(self.alertmanager_consumer.on.cluster_changed, self._configure)
self.framework.observe(self.resources_patch.on.patch_failed, self._on_k8s_patch_failed)
Expand Down

0 comments on commit cb7fdd2

Please sign in to comment.