Skip to content

Commit

Permalink
chore: update charm libraries (#129)
Browse files Browse the repository at this point in the history
Co-authored-by: Github Actions <github-actions@github.com>
  • Loading branch information
observability-noctua-bot and Github Actions authored Mar 10, 2023
1 parent 38697e5 commit 0917773
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 9 additions & 5 deletions lib/charms/grafana_k8s/v0/grafana_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.<type>]["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)
Expand Down
17 changes: 16 additions & 1 deletion lib/charms/prometheus_k8s/v0/prometheus_scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 0917773

Please sign in to comment.