{ lib, config, ... }: let cfg = config.monitoring; in { config = lib.mkIf (cfg.monitorAll && config.services.matrix-synapse.enable) { services.matrix-synapse.settings.enable_metrics = true; services.matrix-synapse.settings.listeners = [ { type = "metrics"; port = 9200; tls = false; resources = [ ]; } ]; services.matrix-synapse.workers = ( lib.listToAttrs ( lib.imap (index: workerName: { name = workerName; value = { worker_listeners = [ { type = "metrics"; port = 9200 + index + 1; resources = [ ]; } ]; }; }) config.monitoring.synapse.workerNames ) ); services.prometheus.scrapeConfigs = [ { job_name = "synapse-main"; scrape_interval = "${toString cfg.prometheusScrapeInterval}s"; static_configs = [ { targets = [ "localhost:9200" ]; labels.instance = "synapse"; } ]; metrics_path = "_synapse/metrics"; } ] ++ lib.flatten ( lib.imap (index: workerName: { job_name = "synapse-${workerName}s"; scrape_interval = "${toString cfg.prometheusScrapeInterval}s"; static_configs = [ { targets = [ "localhost:${toString (9200 + index + 1)}" ]; labels.instance = "synapse"; } ]; metrics_path = "_synapse/metrics"; }) 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"; }; }; } ]; }; }; }