metrics: use matrix-lib to simplify generation of prometheus scrape config

This commit is contained in:
Daniel Olsen 2023-01-20 08:24:02 +01:00
parent efed13c810
commit 1a0880086a
2 changed files with 32 additions and 19 deletions

View File

@ -1,12 +1,15 @@
{ {
"nodes": { "nodes": {
"matrix-next": { "matrix-next": {
"inputs": {
"nixpkgs-lib": "nixpkgs-lib"
},
"locked": { "locked": {
"lastModified": 1671663871, "lastModified": 1674198693,
"narHash": "sha256-06G6xYTFPVuvmN/k2QDeBk9XIp4LDxEKWRL3aLAFFNo=", "narHash": "sha256-xjIJ4LinPNbV+0UYwhCldWUiL1e/B+hN4SR8cxOvxZA=",
"owner": "dali99", "owner": "dali99",
"repo": "nixos-matrix-modules", "repo": "nixos-matrix-modules",
"rev": "b6f0a026a78200c0e526aa73279c228e08673437", "rev": "07e95170e8887b918c288c9751e46d3e0b6ab34f",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -17,11 +20,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1673785634, "lastModified": 1674091526,
"narHash": "sha256-4SPGYVNutklnlpSMaqL+GA2x5DJ+QL85T+hOF6MHAZE=", "narHash": "sha256-eLhLKOpF1ix5xZeFF9g8uE1stdyxuBLJvWQ20gLbDto=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "54d5d59cb19728a0321efbcd22c539109489965b", "rev": "fc5b90fd72177d9bcf435b10c12bb943549748c6",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -31,6 +34,21 @@
"type": "github" "type": "github"
} }
}, },
"nixpkgs-lib": {
"locked": {
"lastModified": 1673743903,
"narHash": "sha256-sloY6KYyVOozJ1CkbgJPpZ99TKIjIvM+04V48C04sMQ=",
"owner": "nix-community",
"repo": "nixpkgs.lib",
"rev": "7555e2dfcbac1533f047021f1744ac8871150f9f",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "nixpkgs.lib",
"type": "github"
}
},
"nixpkgs-stable": { "nixpkgs-stable": {
"locked": { "locked": {
"lastModified": 1673740915, "lastModified": 1673740915,
@ -78,11 +96,11 @@
}, },
"unstable": { "unstable": {
"locked": { "locked": {
"lastModified": 1673855649, "lastModified": 1674101896,
"narHash": "sha256-Pc1VumquuFMDR1Ers1QOVDDabL/trVwfqWXeKJPXLQg=", "narHash": "sha256-xWLaexT6IHhOJru54wrOMeBbkKeJzOZ4Pqrxctf82q0=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "c85d08692966cf022b0a741a794cb1650602d8af", "rev": "a841e262264e48722dccc8469f066068146e406b",
"type": "github" "type": "github"
}, },
"original": { "original": {

View File

@ -1,8 +1,10 @@
{ config, lib, pkgs, values, ... }: { config, lib, pkgs, values, inputs, ... }:
let let
cfg = config.services.matrix-synapse-next; cfg = config.services.matrix-synapse-next;
matrix-lib = inputs.matrix-next.lib;
imap0Attrs = with lib; f: set: imap0Attrs = with lib; f: set:
listToAttrs (imap0 (i: attr: nameValuePair attr (f i attr set.${attr})) (attrNames set)); listToAttrs (imap0 (i: attr: nameValuePair attr (f i attr set.${attr})) (attrNames set));
in { in {
@ -172,15 +174,8 @@ in {
services.nginx.virtualHosts."matrix.pvv.ntnu.no" = lib.mkMerge [({ services.nginx.virtualHosts."matrix.pvv.ntnu.no" = lib.mkMerge [({
locations = let locations = let
isListenerType = type: listener: lib.lists.any (r: lib.lists.any (n: n == type) r.names) listener.resources; connectionInfo = w: matrix-lib.workerConnectionResource "metrics" w;
isMetricsListener = l: isListenerType "metrics" l; socketAddress = w: let c = connectionInfo w; in "${c.host}:${toString (c.port)}";
firstMetricsListener = w: lib.lists.findFirst isMetricsListener (throw "No metrics endpoint on worker") w.settings.worker_listeners;
wAddress = w: lib.lists.findFirst (_: true) (throw "No address in receiver") (firstMetricsListener w).bind_addresses;
wPort = w: (firstMetricsListener w).port;
socketAddress = w: "${wAddress w}:${toString (wPort w)}";
metricsPath = w: "/metrics/${w.type}/${toString w.index}"; metricsPath = w: "/metrics/${w.type}/${toString w.index}";
proxyPath = w: "http://${socketAddress w}/_synapse/metrics"; proxyPath = w: "http://${socketAddress w}/_synapse/metrics";