summary refs log tree commit diff
path: root/nixos/modules/services/monitoring/prometheus/default.nix
diff options
context:
space:
mode:
authorBas van Dijk <v.dijk.bas@gmail.com>2019-04-16 16:06:11 +0200
committerBas van Dijk <v.dijk.bas@gmail.com>2019-04-16 16:06:11 +0200
commita913d0891cae7557bf6b56d6fc7b251aff373b2e (patch)
tree0bd818b942009536e60f20c89af6a8c3bb812f12 /nixos/modules/services/monitoring/prometheus/default.nix
parenta23db5db08206e77c2861107bfaf600b8ff7f5ce (diff)
downloadnixpkgs-a913d0891cae7557bf6b56d6fc7b251aff373b2e.tar
nixpkgs-a913d0891cae7557bf6b56d6fc7b251aff373b2e.tar.gz
nixpkgs-a913d0891cae7557bf6b56d6fc7b251aff373b2e.tar.bz2
nixpkgs-a913d0891cae7557bf6b56d6fc7b251aff373b2e.tar.lz
nixpkgs-a913d0891cae7557bf6b56d6fc7b251aff373b2e.tar.xz
nixpkgs-a913d0891cae7557bf6b56d6fc7b251aff373b2e.tar.zst
nixpkgs-a913d0891cae7557bf6b56d6fc7b251aff373b2e.zip
nixos/prometheus: filter out empty srcape_configs attributes
This results in a smaller prometheus.yml config file.

It also allows us to use the same options for both prometheus-1 and
prometheus-2 since the new options for prometheus-2 default to null
and will be filtered out if they are not set.
Diffstat (limited to 'nixos/modules/services/monitoring/prometheus/default.nix')
-rw-r--r--nixos/modules/services/monitoring/prometheus/default.nix19
1 files changed, 17 insertions, 2 deletions
diff --git a/nixos/modules/services/monitoring/prometheus/default.nix b/nixos/modules/services/monitoring/prometheus/default.nix
index 525b0b18cec..e7ac12c07d3 100644
--- a/nixos/modules/services/monitoring/prometheus/default.nix
+++ b/nixos/modules/services/monitoring/prometheus/default.nix
@@ -54,7 +54,7 @@ let
     rule_files = map (promtoolCheck "check-rules" "rules") (cfg.ruleFiles ++ [
       (pkgs.writeText "prometheus.rules" (concatStringsSep "\n" cfg.rules))
     ]);
-    scrape_configs = cfg.scrapeConfigs;
+    scrape_configs = filterEmpty cfg.scrapeConfigs;
   };
 
   generatedPrometheusYml = writePrettyJSON "prometheus.yml" promConfig;
@@ -81,7 +81,7 @@ let
     rule_files = map (prom2toolCheck "check rules" "rules") (cfg2.ruleFiles ++ [
       (pkgs.writeText "prometheus.rules" (concatStringsSep "\n" cfg2.rules))
     ]);
-    scrape_configs = cfg2.scrapeConfigs;
+    scrape_configs = filterEmpty cfg2.scrapeConfigs;
     alerting = optionalAttrs (cfg2.alertmanagerURL != []) {
       alertmanagers = [{
         static_configs = [{
@@ -108,6 +108,21 @@ let
   ] ++
   optional (cfg2.webExternalUrl != null) "--web.external-url=${cfg2.webExternalUrl}";
 
+  filterEmpty = filterAttrsListRecursive (_n: v: !(v == null || v == [] || v == {}));
+  filterAttrsListRecursive = pred: x:
+    if isAttrs x then
+      listToAttrs (
+        concatMap (name:
+          let v = x.${name}; in
+          if pred name v then [
+            (nameValuePair name (filterAttrsListRecursive pred v))
+          ] else []
+        ) (attrNames x)
+      )
+    else if isList x then
+      map (filterAttrsListRecursive pred) x
+    else x;
+
   promTypes.globalConfig = types.submodule {
     options = {
       scrape_interval = mkOption {