nixos/prometheus: check configuration before starting service
With `promtool` we can check the validity of a configuration before deploying it. This avoids situations where you would end up with a broken monitoring system without noticing it - since the monitoring broke down. :-)
This commit is contained in:
parent
0de150e0f2
commit
6795bdd58c
@ -10,6 +10,13 @@ let
|
|||||||
# Get a submodule without any embedded metadata:
|
# Get a submodule without any embedded metadata:
|
||||||
_filter = x: filterAttrs (k: v: k != "_module") x;
|
_filter = x: filterAttrs (k: v: k != "_module") x;
|
||||||
|
|
||||||
|
# a wrapper that verifies that the configuration is valid
|
||||||
|
promtoolCheck = what: name: file: pkgs.runCommand "${name}-${what}-checked"
|
||||||
|
{ buildInputs = [ cfg.package ]; } ''
|
||||||
|
ln -s ${file} $out
|
||||||
|
promtool ${what} $out
|
||||||
|
'';
|
||||||
|
|
||||||
# Pretty-print JSON to a file
|
# Pretty-print JSON to a file
|
||||||
writePrettyJSON = name: x:
|
writePrettyJSON = name: x:
|
||||||
pkgs.runCommand name { } ''
|
pkgs.runCommand name { } ''
|
||||||
@ -19,18 +26,19 @@ let
|
|||||||
# This becomes the main config file
|
# This becomes the main config file
|
||||||
promConfig = {
|
promConfig = {
|
||||||
global = cfg.globalConfig;
|
global = cfg.globalConfig;
|
||||||
rule_files = cfg.ruleFiles ++ [
|
rule_files = map (promtoolCheck "check-rules" "rules") (cfg.ruleFiles ++ [
|
||||||
(pkgs.writeText "prometheus.rules" (concatStringsSep "\n" cfg.rules))
|
(pkgs.writeText "prometheus.rules" (concatStringsSep "\n" cfg.rules))
|
||||||
];
|
]);
|
||||||
scrape_configs = cfg.scrapeConfigs;
|
scrape_configs = cfg.scrapeConfigs;
|
||||||
};
|
};
|
||||||
|
|
||||||
generatedPrometheusYml = writePrettyJSON "prometheus.yml" promConfig;
|
generatedPrometheusYml = writePrettyJSON "prometheus.yml" promConfig;
|
||||||
|
|
||||||
prometheusYml =
|
prometheusYml = let
|
||||||
if cfg.configText != null then
|
yml = if cfg.configText != null then
|
||||||
pkgs.writeText "prometheus.yml" cfg.configText
|
pkgs.writeText "prometheus.yml" cfg.configText
|
||||||
else generatedPrometheusYml;
|
else generatedPrometheusYml;
|
||||||
|
in promtoolCheck "check-config" "prometheus.yml" yml;
|
||||||
|
|
||||||
cmdlineArgs = cfg.extraFlags ++ [
|
cmdlineArgs = cfg.extraFlags ++ [
|
||||||
"-storage.local.path=${cfg.dataDir}/metrics"
|
"-storage.local.path=${cfg.dataDir}/metrics"
|
||||||
|
Loading…
Reference in New Issue
Block a user