summary refs log tree commit diff
path: root/modules/monitoring/synapse.nix
diff options
context:
space:
mode:
Diffstat (limited to 'modules/monitoring/synapse.nix')
-rw-r--r--modules/monitoring/synapse.nix111
1 files changed, 111 insertions, 0 deletions
diff --git a/modules/monitoring/synapse.nix b/modules/monitoring/synapse.nix
new file mode 100644
index 0000000..12c5562
--- /dev/null
+++ b/modules/monitoring/synapse.nix
@@ -0,0 +1,111 @@
+{ lib, config, ... }:
+let
+  cfg = config.monitoring;
+in
+{
+  config = lib.mkIf (cfg.monitorAll && config.services.matrix-synapse.enable) {
+    services.matrix-synapse.settings.listeners = [
+      {
+        type = "http";
+        port = 9200;
+        resources = [
+          {
+            names = [ "metrics" ];
+            compress = false;
+          }
+        ];
+      }
+    ];
+
+    #services.matrix-synapse.workers = (
+    #  let
+    #    workerNames = config.services.matrix-synapse.workers;
+
+    #    hasMetricsListener =
+    #      (workerName:
+    #      lib.any (
+    #        listener: listener.type == "http" && (lib.any (resourceName: resourceName == "metrics") listener.resources)
+    #      ) config.services.matrix-synapse.workers.${workerName}.worker_listeners);
+
+    #    workerNamesWithoutMetrics = lib.traceVal lib.filter (workerName: (!hasMetricsListener workerName)) workerNames;
+    #  in
+    #  lib.listToAttrs (
+    #    lib.imap (index: workerName: {
+    #      name = workerName;
+    #      value = {
+    #        worker_listeners = [
+    #          {
+    #            type = "http";
+    #            port = 9200 + index;
+    #            resources = [
+    #              {
+    #                names = [ "metrics" ];
+    #                compress = false;
+    #              }
+    #            ];
+    #          }
+    #        ];
+    #      };
+    #    }) workerNamesWithoutMetrics
+    #  )
+    #);
+
+    services.matrix-synapse.workers = (
+      lib.listToAttrs (
+        lib.imap (index: workerName: {
+          name = workerName;
+          value = {
+            worker_listeners = [
+              {
+                type = "http";
+                port = 9200 + index + 1;
+                resources = [
+                  {
+                    names = [ "metrics" ];
+                    compress = false;
+                  }
+                ];
+              }
+            ];
+          };
+        }) config.monitoring.synapse.workerNames
+      )
+    );
+
+    services.prometheus.scrapeConfigs = (
+      (lib.map (interval: {
+        job_name = "synapse-main-${toString interval}s";
+        scrape_interval = "${toString interval}s";
+        static_configs = [ { targets = [ "localhost:9200" ]; } ];
+      }) cfg.prometheusScrapeIntervals)
+      ++ lib.flatten (
+        lib.imap (
+          index: workerName:
+          lib.map (interval: {
+            job_name = "synapse-${workerName}-${toString interval}s";
+            scrape_interval = "${toString interval}s";
+            static_configs = [ { targets = [ "localhost:${toString (9200 + index + 1)}" ]; } ];
+          }) cfg.prometheusScrapeIntervals
+        ) config.monitoring.synapse.workerNames
+      )
+    );
+
+    services.grafana.provision.dashboards.settings = {
+      apiVersion = 1;
+      providers = [
+        {
+          name = "matrix-synapse";
+          orgId = 1;
+          type = "file";
+          options = {
+            path = builtins.fetchurl {
+              url = "https://raw.githubusercontent.com/element-hq/synapse/master/contrib/grafana/synapse.json";
+              sha256 = "07qlr0waw9phmyd38bv22bn5v303w3397b89l44l3lzwhpnhs16s";
+            };
+          };
+        }
+      ];
+    };
+
+  };
+}