Skip to content

Commit

Permalink
Grafana needs to know scrape_interval for $__rate_interval
Browse files Browse the repository at this point in the history
Grafana assumes the scrape interval as 15s by default but COS stack
assumes 1m. Without telling Grafana that COS Prometheus uses 1m as the
global scrape interval, many graphs will break since $__rate_interval
won't be calculated properly.

Hardcoding 1m itself is in question, but this patch fixes many graphs
using $__rate_interval without touching the fundamental pieces.

ref:
https://grafana.com/docs/grafana/latest/datasources/prometheus/configure-prometheus-data-source/#interval-behavior
https://grafana.com/blog/2020/09/28/new-in-grafana-7.2-__rate_interval-for-prometheus-rate-queries-that-just-work/
#544

Closes: #543
  • Loading branch information
nobuto-m committed Nov 1, 2023
1 parent 16ba0e8 commit e6f0803
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@

PROMETHEUS_DIR = "/etc/prometheus"
PROMETHEUS_CONFIG = f"{PROMETHEUS_DIR}/prometheus.yml"
PROMETHEUS_GLOBAL_SCRAPE_INTERVAL = "1m"
RULES_DIR = f"{PROMETHEUS_DIR}/rules"
CONFIG_HASH_PATH = f"{PROMETHEUS_DIR}/config.sha256"
ALERTS_HASH_PATH = f"{PROMETHEUS_DIR}/alerts.sha256"
Expand Down Expand Up @@ -171,6 +172,7 @@ def __init__(self, *args):
source_type="prometheus",
source_url=self.internal_url, # https://github.com/canonical/operator/issues/970
refresh_event=self.cert_handler.on.cert_changed,
extra_fields={"timeInterval": PROMETHEUS_GLOBAL_SCRAPE_INTERVAL},
)

self.catalogue = CatalogueConsumer(charm=self, item=self._catalogue_item)
Expand Down Expand Up @@ -827,7 +829,10 @@ def _prometheus_global_config(self) -> dict:
a dictionary consisting of global configuration for Prometheus.
"""
config = self.model.config
global_config = {"scrape_interval": "1m", "scrape_timeout": "10s"}
global_config = {
"scrape_interval": PROMETHEUS_GLOBAL_SCRAPE_INTERVAL,
"scrape_timeout": "10s",
}

if config.get("evaluation_interval") and self._is_valid_timespec(
config["evaluation_interval"]
Expand Down

0 comments on commit e6f0803

Please sign in to comment.