From 0917773fc6d5d33714a0336e94a04026b12896d8 Mon Sep 17 00:00:00 2001 From: Noctua <98737499+observability-noctua-bot@users.noreply.github.com> Date: Fri, 10 Mar 2023 05:27:41 +0100 Subject: [PATCH] chore: update charm libraries (#129) Co-authored-by: Github Actions --- lib/charms/grafana_k8s/v0/grafana_dashboard.py | 14 +++++++++----- .../prometheus_k8s/v0/prometheus_scrape.py | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/charms/grafana_k8s/v0/grafana_dashboard.py b/lib/charms/grafana_k8s/v0/grafana_dashboard.py index 16235175..cc984900 100644 --- a/lib/charms/grafana_k8s/v0/grafana_dashboard.py +++ b/lib/charms/grafana_k8s/v0/grafana_dashboard.py @@ -218,7 +218,7 @@ def __init__(self, *args): # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 24 +LIBPATCH = 25 logger = logging.getLogger(__name__) @@ -366,7 +366,7 @@ def __init__(self, *args): REACTIVE_CONVERTER = { # type: ignore "allValue": None, "datasource": "${prometheusds}", - "definition": 'label_values(up{juju_model="$juju_model",juju_model_uuid="$juju_model_uuid",juju_application="$juju_application"},host)', + "definition": 'label_values(up{juju_model=~"$juju_model",juju_model_uuid=~"$juju_model_uuid",juju_application=~"$juju_application"},host)', "description": None, "error": None, "hide": 0, @@ -376,7 +376,7 @@ def __init__(self, *args): "name": "host", "options": [], "query": { - "query": 'label_values(up{juju_model="$juju_model",juju_model_uuid="$juju_model_uuid",juju_application="$juju_application"},host)', + "query": 'label_values(up{juju_model=~"$juju_model",juju_model_uuid=~"$juju_model_uuid",juju_application=~"$juju_application"},host)', "refId": "StandardVariableQuery", }, "refresh": 1, @@ -1713,7 +1713,7 @@ def _strip_existing_datasources(self, template: dict) -> dict: # noqa: C901 `dashboard["__inputs"]` is a property sometimes set when exporting dashboards from the Grafana UI. It is not present in earlier Grafana versions, and can be disabled in 5.3.4 and above (optionally). If set, any values present will be substituted on - import. Some reactive charms use this for Prometheus. LMA2 uses dropdown selectors + import. Some reactive charms use this for Prometheus. COS uses dropdown selectors for datasources, and leaving this present results in "default" datasource values which are broken. @@ -1799,7 +1799,11 @@ def _handle_reactive_dashboards(self, event: RelationEvent) -> Optional[Dict]: # Grafana won't load it. We have to unbox: # event.relation.data[event.]["request_*"]["dashboard"]["dashboard"], # and the final unboxing is below. - dash = json.dumps(t["dashboard"]) + # + # Apparently SOME newer dashboards (such as Ceph) do not have this double nesting, so + # now we get to account for both :toot: + dash = t.get("dashboard", {}) or t + dash = json.dumps(dash) # Replace the old-style datasource templates dash = re.sub(r"<< datasource >>", r"${prometheusds}", dash) diff --git a/lib/charms/prometheus_k8s/v0/prometheus_scrape.py b/lib/charms/prometheus_k8s/v0/prometheus_scrape.py index 28ce5075..752cfce6 100644 --- a/lib/charms/prometheus_k8s/v0/prometheus_scrape.py +++ b/lib/charms/prometheus_k8s/v0/prometheus_scrape.py @@ -368,7 +368,7 @@ def _on_scrape_targets_changed(self, event): # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 30 +LIBPATCH = 31 logger = logging.getLogger(__name__) @@ -1936,6 +1936,9 @@ def _set_prometheus_data(self, event): `MetricsEndpointAggregator`, that Prometheus unit is provided with the complete set of existing scrape jobs and alert rules. """ + if not self._charm.unit.is_leader(): + return + jobs = [] + _type_convert_stored( self._stored.jobs ) # list of scrape jobs, one per relation @@ -1982,6 +1985,9 @@ def set_target_job_data(self, targets: dict, app_name: str, **kwargs) -> None: targets: a `dict` containing target information app_name: a `str` identifying the application """ + if not self._charm.unit.is_leader(): + return + # new scrape job for the relation that has changed updated_job = self._static_scrape_job(targets, app_name, **kwargs) @@ -2015,6 +2021,9 @@ def remove_prometheus_jobs(self, job_name: str, unit_name: Optional[str] = ""): For NRPE, the job name is calculated from an ID sent via the NRPE relation, and is sufficient to uniquely identify the target. """ + if not self._charm.unit.is_leader(): + return + for relation in self.model.relations[self._prometheus_relation]: jobs = json.loads(relation.data[self._charm.app].get("scrape_jobs", "[]")) if not jobs: @@ -2195,6 +2204,9 @@ def set_alert_rule_data(self, name: str, unit_rules: dict, label_rules: bool = T The unit rules should be a dict, which is has additional Juju topology labels added. For rules generated by the NRPE exporter, they are pre-labeled so lookups can be performed. """ + if not self._charm.unit.is_leader(): + return + if label_rules: rules = self._label_alert_rules(unit_rules, name) else: @@ -2229,6 +2241,9 @@ def _on_alert_rules_departed(self, event): def remove_alert_rules(self, group_name: str, unit_name: str) -> None: """Remove an alert rule group from relation data.""" + if not self._charm.unit.is_leader(): + return + for relation in self.model.relations[self._prometheus_relation]: alert_rules = json.loads(relation.data[self._charm.app].get("alert_rules", "{}")) if not alert_rules: