Skip to content

Commit

Permalink
Fix alerts assignment when event is None (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
sed-i authored Nov 27, 2023
1 parent 9803d99 commit b799ee0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def _downstream_prometheus_scrape_relation_broken(self, _):
self._stored.have_prometheus = False
self._set_status()

def _on_nrpe_targets_changed(self, event):
def _on_nrpe_targets_changed(self, event: Optional[NrpeTargetsChangedEvent]):
"""Send NRPE jobs over to MetricsEndpointAggregator."""
if event and isinstance(event, NrpeTargetsChangedEvent):
removed_targets = event.removed_targets
Expand All @@ -461,9 +461,11 @@ def _on_nrpe_targets_changed(self, event):
)

nrpes = cast(List[Dict[str, Any]], event.current_targets)
current_alerts = event.current_alerts
else:
# If the event arg is None, then the stored state value is already up-to-date.
nrpes = self.nrpe_exporter.endpoints()
current_alerts = self.nrpe_exporter.alerts()

self._modify_enrichment_file(endpoints=nrpes)

Expand All @@ -472,7 +474,7 @@ def _on_nrpe_targets_changed(self, event):
nrpe["target"], nrpe["app_name"], **nrpe["additional_fields"]
)

for alert in event.current_alerts:
for alert in current_alerts:
self.metrics_aggregator.set_alert_rule_data(
re.sub(r"/", "_", alert["labels"]["juju_unit"]),
alert,
Expand Down
16 changes: 16 additions & 0 deletions tests/unit/test_relation_monitors.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,19 @@ def test_monitors_changed(self):
]
)
self.assertEqual(expected, self.mock_enrichment_file.read_text())

def test_prometheus(self):
# GIVEN a post-startup charm
self.harness.begin_with_initial_hooks()

# WHEN "monitors" and "downstream-prometheus-scrape" relations join
rel_id_nrpe = self.harness.add_relation("monitors", "nrpe")
self.harness.add_relation_unit(rel_id_nrpe, "nrpe/0")
self.harness.update_relation_data(rel_id_nrpe, "nrpe/0", self.default_unit_data)

rel_id_prom = self.harness.add_relation("downstream-prometheus-scrape", "prom")
self.harness.add_relation_unit(rel_id_prom, "prom/0")

# THEN alert rules are transferred to prometheus over relation data
app_data = self.harness.get_relation_data(rel_id_prom, "cos-proxy")
self.assertIn("alert_rules", app_data)

0 comments on commit b799ee0

Please sign in to comment.