summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorAndreas Rammhold <andreas@rammhold.de>2018-11-04 14:30:47 +0100
committerRobin Gloster <mail@glob.in>2018-11-04 15:08:44 +0100
commit6795bdd58ca1f1504833428365f32bd023bb1aeb (patch)
tree8ed13627872ba15cf57e429f54d2544fe507fa0f /nixos
parent0de150e0f2878995716f398eec04ddca220d6cfc (diff)
downloadnixlib-6795bdd58ca1f1504833428365f32bd023bb1aeb.tar
nixlib-6795bdd58ca1f1504833428365f32bd023bb1aeb.tar.gz
nixlib-6795bdd58ca1f1504833428365f32bd023bb1aeb.tar.bz2
nixlib-6795bdd58ca1f1504833428365f32bd023bb1aeb.tar.lz
nixlib-6795bdd58ca1f1504833428365f32bd023bb1aeb.tar.xz
nixlib-6795bdd58ca1f1504833428365f32bd023bb1aeb.tar.zst
nixlib-6795bdd58ca1f1504833428365f32bd023bb1aeb.zip
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. :-)
Diffstat (limited to 'nixos')
-rw-r--r--nixos/modules/services/monitoring/prometheus/default.nix18
1 files changed, 13 insertions, 5 deletions
diff --git a/nixos/modules/services/monitoring/prometheus/default.nix b/nixos/modules/services/monitoring/prometheus/default.nix
index 63b7d1c0bd4e..bf4dfc666bb6 100644
--- a/nixos/modules/services/monitoring/prometheus/default.nix
+++ b/nixos/modules/services/monitoring/prometheus/default.nix
@@ -10,6 +10,13 @@ let
   # Get a submodule without any embedded metadata:
   _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
   writePrettyJSON = name: x:
     pkgs.runCommand name { } ''
@@ -19,18 +26,19 @@ let
   # This becomes the main config file
   promConfig = {
     global = cfg.globalConfig;
-    rule_files = cfg.ruleFiles ++ [
+    rule_files = map (promtoolCheck "check-rules" "rules") (cfg.ruleFiles ++ [
       (pkgs.writeText "prometheus.rules" (concatStringsSep "\n" cfg.rules))
-    ];
+    ]);
     scrape_configs = cfg.scrapeConfigs;
   };
 
   generatedPrometheusYml = writePrettyJSON "prometheus.yml" promConfig;
 
-  prometheusYml =
-    if cfg.configText != null then
+  prometheusYml = let
+    yml =  if cfg.configText != null then
       pkgs.writeText "prometheus.yml" cfg.configText
-    else generatedPrometheusYml;
+      else generatedPrometheusYml;
+    in promtoolCheck "check-config" "prometheus.yml" yml;
 
   cmdlineArgs = cfg.extraFlags ++ [
     "-storage.local.path=${cfg.dataDir}/metrics"