Skip to content

Commit

Permalink
chore: fmt and static fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelThamm committed Dec 20, 2024
1 parent 082a34c commit dbf9341
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 59 deletions.
66 changes: 33 additions & 33 deletions lib/charms/prometheus_k8s/v0/prometheus_scrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,6 @@ def _on_scrape_targets_changed(self, event):
import socket
import subprocess
import tempfile
import textwrap
from collections import defaultdict
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
Expand Down Expand Up @@ -399,38 +398,39 @@ def _on_scrape_targets_changed(self, event):

DEFAULT_ALERT_RULES_RELATIVE_PATH = "./src/prometheus_alert_rules"

GENERIC_ALERT_RULES_GROUP = yaml.safe_load(
textwrap.dedent(
"""
groups:
- name: HostHealth
rules:
- alert: HostDown
expr: up < 1
for: 5m
labels:
severity: critical
annotations:
summary: Host '{{ $labels.instance }}' is down.
description: >-
Host '{{ $labels.instance }}' is down, failed to scrape.
VALUE = {{ $value }}
LABELS = {{ $labels }}
- alert: HostMetricsMissing
# This alert is applicable only when the provider is linked via an aggregator (such as grafana agent)
expr: absent(up)
for: 5m
labels:
severity: critical
annotations:
summary: Metrics not received from host '{{ $labels.instance }}', failed to remote write.
description: >-
Metrics not received from host '{{ $labels.instance }}', failed to remote write.
VALUE = {{ $value }}
LABELS = {{ $labels }}
"""
)
)
GENERIC_ALERT_RULES_GROUP = {
"groups": [
{
"name": "HostHealth",
"rules": [
{
"alert": "HostDown",
"expr": "up < 1",
"for": "5m",
"labels": {"severity": "critical"},
"annotations": {
"summary": "Host '{{ $labels.instance }}' is down.",
"description": """Host '{{ $labels.instance }}' is down, failed to scrape.
VALUE = {{ $value }}
LABELS = {{ $labels }}""",
},
},
{
"alert": "HostMetricsMissing",
"expr": "absent(up)",
"for": "5m",
"labels": {"severity": "critical"},
"annotations": {
"summary": "Metrics not received from host '{{ $labels.instance }}', failed to remote write.",
"description": """Metrics not received from host '{{ $labels.instance }}', failed to remote write.
VALUE = {{ $value }}
LABELS = {{ $labels }}""",
},
},
],
}
]
}


class PrometheusConfig:
Expand Down
44 changes: 22 additions & 22 deletions lib/charms/prometheus_k8s/v1/prometheus_remote_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import socket
import subprocess
import tempfile
import textwrap
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

Expand All @@ -47,7 +46,7 @@

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

PYDEPS = ["git+https://github.com/canonical/cos-lib.git@feature/generic-alerts#egg=cosl"]

Expand All @@ -61,26 +60,27 @@

DEFAULT_ALERT_RULES_RELATIVE_PATH = "./src/prometheus_alert_rules"

GENERIC_ALERT_RULES_GROUP = yaml.safe_load(
textwrap.dedent(
"""
groups:
- name: AggregatorHostHealth
rules:
- alert: HostMetricsMissing
expr: absent(up)
for: 5m
labels:
severity: critical
annotations:
summary: Metrics not received from host '{{ $labels.instance }}', failed to remote write.
description: >-
Metrics not received from host '{{ $labels.instance }}', failed to remote write.
VALUE = {{ $value }}
LABELS = {{ $labels }}
"""
)
)
GENERIC_ALERT_RULES_GROUP = {
"groups": [
{
"name": "AggregatorHostHealth",
"rules": [
{
"alert": "HostMetricsMissing",
"expr": "absent(up)",
"for": "5m",
"labels": {"severity": "critical"},
"annotations": {
"summary": "Metrics not received from host '{{ $labels.instance }}', failed to remote write.",
"description": """Metrics not received from host '{{ $labels.instance }}', failed to remote write.
VALUE = {{ $value }}
LABELS = {{ $labels }}""",
},
}
],
}
]
}


class RelationNotFoundError(Exception):
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_endpoint_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,11 +753,11 @@ def test_unit_label_is_retained_if_hard_coded(self):
# check unit topology is present in labels and in alert rule expression
relation = self.harness.charm.model.get_relation("metrics-endpoint")
alert_rules = json.loads(relation.data[self.harness.charm.app].get("alert_rules"))
from pprint import pprint
pprint(alert_rules)
for group in alert_rules["groups"]:
for rule in group["rules"]:
if "_HostHealth_alerts" not in group["name"]: # _HostHealth_alerts are injected alerts without juju_unit labels
if (
"_HostHealth_alerts" not in group["name"]
): # _HostHealth_alerts are injected alerts without juju_unit labels
self.assertIn("juju_unit", rule["labels"])
self.assertIn("juju_unit=", rule["expr"])

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ commands =
[testenv:static-{charm,lib,unit,integration}]
description = Run static analysis checks
deps =
cosl
git+https://github.com/canonical/cos-lib.git@feature/generic-alerts#egg=cosl
pyright
charm: -r{toxinidir}/requirements.txt
lib: ops
Expand Down

0 comments on commit dbf9341

Please sign in to comment.