Merge pull request #252800 from 1000101/prometheus-pgbouncer-exporter

prometheus-pgbouncer-exporter: init at 0.7.0

authored by Mario Rodas and committed by GitHub 5103716b f3051b01

+237 -6
+38 -6
nixos/modules/services/monitoring/prometheus/exporters.nix
··· 58 58 "nut" 59 59 "openldap" 60 60 "openvpn" 61 + "pgbouncer" 61 62 "php-fpm" 62 63 "pihole" 63 64 "postfix" ··· 313 314 'services.prometheus.exporters.nextcloud.tokenFile' 314 315 ''; 315 316 } { 317 + assertion = cfg.pgbouncer.enable -> ( 318 + (cfg.pgbouncer.connectionStringFile != null || cfg.pgbouncer.connectionString != "") 319 + ); 320 + message = '' 321 + PgBouncer exporter needs either connectionStringFile or connectionString configured" 322 + ''; 323 + } { 324 + assertion = cfg.pgbouncer.enable -> ( 325 + config.services.pgbouncer.ignoreStartupParameters != null && builtins.match ".*extra_float_digits.*" config.services.pgbouncer.ignoreStartupParameters != null 326 + ); 327 + message = '' 328 + Prometheus PgBouncer exporter requires including `extra_float_digits` in services.pgbouncer.ignoreStartupParameters 329 + 330 + Example: 331 + services.pgbouncer.ignoreStartupParameters = extra_float_digits; 332 + 333 + See https://github.com/prometheus-community/pgbouncer_exporter#pgbouncer-configuration 334 + ''; 335 + } { 316 336 assertion = cfg.sql.enable -> ( 317 337 (cfg.sql.configFile == null) != (cfg.sql.configuration == null) 318 338 ); ··· 350 370 `openFirewall' is set to `true'! 351 371 ''; 352 372 })) ++ config.services.prometheus.exporters.assertions; 353 - warnings = [(mkIf (config.services.prometheus.exporters.idrac.enable && config.services.prometheus.exporters.idrac.configurationPath != null) '' 354 - Configuration file in `services.prometheus.exporters.idrac.configurationPath` may override 355 - `services.prometheus.exporters.idrac.listenAddress` and/or `services.prometheus.exporters.idrac.port`. 356 - Consider using `services.prometheus.exporters.idrac.configuration` instead. 357 - '' 358 - )] ++ config.services.prometheus.exporters.warnings; 373 + warnings = [ 374 + (mkIf (config.services.prometheus.exporters.idrac.enable && config.services.prometheus.exporters.idrac.configurationPath != null) '' 375 + Configuration file in `services.prometheus.exporters.idrac.configurationPath` may override 376 + `services.prometheus.exporters.idrac.listenAddress` and/or `services.prometheus.exporters.idrac.port`. 377 + Consider using `services.prometheus.exporters.idrac.configuration` instead. 378 + '' 379 + ) 380 + (mkIf 381 + (cfg.pgbouncer.enable && cfg.pgbouncer.connectionString != "") '' 382 + config.services.prometheus.exporters.pgbouncer.connectionString is insecure. Use connectionStringFile instead. 383 + '' 384 + ) 385 + (mkIf 386 + (cfg.pgbouncer.enable && config.services.pgbouncer.authType != "any") '' 387 + Admin user (with password or passwordless) MUST exist in the services.pgbouncer.authFile if authType other than any is used. 388 + '' 389 + ) 390 + ] ++ config.services.prometheus.exporters.warnings; 359 391 }] ++ [(mkIf config.services.minio.enable { 360 392 services.prometheus.exporters.minio.minioAddress = mkDefault "http://localhost:9000"; 361 393 services.prometheus.exporters.minio.minioAccessKey = mkDefault config.services.minio.accessKey;
+145
nixos/modules/services/monitoring/prometheus/exporters/pgbouncer.nix
··· 1 + { config, lib, pkgs, options }: 2 + 3 + with lib; 4 + 5 + let 6 + cfg = config.services.prometheus.exporters.pgbouncer; 7 + in 8 + { 9 + port = 9127; 10 + extraOpts = { 11 + 12 + telemetryPath = mkOption { 13 + type = types.str; 14 + default = "/metrics"; 15 + description = lib.mdDoc '' 16 + Path under which to expose metrics. 17 + ''; 18 + }; 19 + 20 + connectionString = mkOption { 21 + type = types.str; 22 + default = ""; 23 + example = "postgres://admin:@localhost:6432/pgbouncer?sslmode=require"; 24 + description = lib.mdDoc '' 25 + Connection string for accessing pgBouncer. 26 + 27 + NOTE: You MUST keep pgbouncer as database name (special internal db)!!! 28 + 29 + NOTE: Admin user (with password or passwordless) MUST exist 30 + in the services.pgbouncer.authFile if authType other than any is used. 31 + 32 + WARNING: this secret is stored in the world-readable Nix store! 33 + Use {option}`connectionStringFile` instead. 34 + ''; 35 + }; 36 + 37 + connectionStringFile = mkOption { 38 + type = types.nullOr types.path; 39 + default = null; 40 + example = "/run/keys/pgBouncer-connection-string"; 41 + description = lib.mdDoc '' 42 + File that contains pgBouncer connection string in format: 43 + postgres://admin:@localhost:6432/pgbouncer?sslmode=require 44 + 45 + NOTE: You MUST keep pgbouncer as database name (special internal db)!!! 46 + 47 + NOTE: Admin user (with password or passwordless) MUST exist 48 + in the services.pgbouncer.authFile if authType other than any is used. 49 + 50 + {option}`connectionStringFile` takes precedence over {option}`connectionString` 51 + ''; 52 + }; 53 + 54 + pidFile = mkOption { 55 + type = types.nullOr types.str; 56 + default = null; 57 + description = lib.mdDoc '' 58 + Path to PgBouncer pid file. 59 + 60 + If provided, the standard process metrics get exported for the PgBouncer 61 + process, prefixed with 'pgbouncer_process_...'. The pgbouncer_process exporter 62 + needs to have read access to files owned by the PgBouncer process. Depends on 63 + the availability of /proc. 64 + 65 + https://prometheus.io/docs/instrumenting/writing_clientlibs/#process-metrics. 66 + 67 + ''; 68 + }; 69 + 70 + webSystemdSocket = mkOption { 71 + type = types.bool; 72 + default = false; 73 + description = lib.mdDoc '' 74 + Use systemd socket activation listeners instead of port listeners (Linux only). 75 + ''; 76 + }; 77 + 78 + logLevel = mkOption { 79 + type = types.enum ["debug" "info" "warn" "error" ]; 80 + default = "info"; 81 + description = lib.mdDoc '' 82 + Only log messages with the given severity or above. 83 + ''; 84 + }; 85 + 86 + logFormat = mkOption { 87 + type = types.enum ["logfmt" "json"]; 88 + default = "logfmt"; 89 + description = lib.mdDoc '' 90 + Output format of log messages. One of: [logfmt, json] 91 + ''; 92 + }; 93 + 94 + webConfigFile = mkOption { 95 + type = types.nullOr types.path; 96 + default = null; 97 + description = lib.mdDoc '' 98 + Path to configuration file that can enable TLS or authentication. 99 + ''; 100 + }; 101 + 102 + extraFlags = mkOption { 103 + type = types.listOf types.str; 104 + default = [ ]; 105 + description = lib.mdDoc '' 106 + Extra commandline options when launching Prometheus. 107 + ''; 108 + }; 109 + 110 + }; 111 + 112 + serviceOpts = { 113 + after = [ "pgbouncer.service" ]; 114 + serviceConfig = let 115 + startScript = pkgs.writeShellScriptBin "pgbouncer-start" "${concatStringsSep " " ([ 116 + "${pkgs.prometheus-pgbouncer-exporter}/bin/pgbouncer_exporter" 117 + "--web.listen-address ${cfg.listenAddress}:${toString cfg.port}" 118 + "--pgBouncer.connectionString ${if cfg.connectionStringFile != null then 119 + "$(head -n1 ${cfg.connectionStringFile})" else "${escapeShellArg cfg.connectionString}"}" 120 + ] 121 + ++ optionals (cfg.telemetryPath != null) [ 122 + "--web.telemetry-path ${escapeShellArg cfg.telemetryPath}" 123 + ] 124 + ++ optionals (cfg.pidFile != null) [ 125 + "--pgBouncer.pid-file= ${escapeShellArg cfg.pidFile}" 126 + ] 127 + ++ optionals (cfg.logLevel != null) [ 128 + "--log.level ${escapeShellArg cfg.logLevel}" 129 + ] 130 + ++ optionals (cfg.logFormat != null) [ 131 + "--log.format ${escapeShellArg cfg.logFormat}" 132 + ] 133 + ++ optionals (cfg.webSystemdSocket != false) [ 134 + "--web.systemd-socket ${escapeShellArg cfg.webSystemdSocket}" 135 + ] 136 + ++ optionals (cfg.webConfigFile != null) [ 137 + "--web.config.file ${escapeShellArg cfg.webConfigFile}" 138 + ] 139 + ++ cfg.extraFlags)}"; 140 + in 141 + { 142 + ExecStart = "${startScript}/bin/pgbouncer-start"; 143 + }; 144 + }; 145 + }
+30
nixos/tests/prometheus-exporters.nix
··· 966 966 ''; 967 967 }; 968 968 969 + pgbouncer = { 970 + exporterConfig = { 971 + enable = true; 972 + connectionString = "postgres://admin:@localhost:6432/pgbouncer?sslmode=disable"; 973 + }; 974 + 975 + metricProvider = { 976 + services.postgresql.enable = true; 977 + services.pgbouncer = { 978 + # https://github.com/prometheus-community/pgbouncer_exporter#pgbouncer-configuration 979 + ignoreStartupParameters = "extra_float_digits"; 980 + enable = true; 981 + listenAddress = "*"; 982 + databases = { postgres = "host=/run/postgresql/ port=5432 auth_user=postgres dbname=postgres"; }; 983 + authType = "any"; 984 + maxClientConn = 99; 985 + }; 986 + }; 987 + exporterTest = '' 988 + wait_for_unit("postgresql.service") 989 + wait_for_unit("pgbouncer.service") 990 + wait_for_unit("prometheus-pgbouncer-exporter.service") 991 + wait_for_open_port(9127) 992 + succeed("curl -sSf http://localhost:9127/metrics | grep 'pgbouncer_up 1'") 993 + succeed( 994 + "curl -sSf http://localhost:9127/metrics | grep 'pgbouncer_config_max_client_connections 99'" 995 + ) 996 + ''; 997 + }; 998 + 969 999 php-fpm = { 970 1000 nodeName = "php_fpm"; 971 1001 exporterConfig = {
+23
pkgs/servers/monitoring/prometheus/pgbouncer-exporter.nix
··· 1 + { lib, buildGoModule, fetchFromGitHub }: 2 + 3 + buildGoModule rec { 4 + pname = "pgbouncer-exporter"; 5 + version = "0.7.0"; 6 + 7 + src = fetchFromGitHub { 8 + owner = "prometheus-community"; 9 + repo = "pgbouncer_exporter"; 10 + rev = "v${version}"; 11 + sha256 = "sha256-2N8FaGk6AU39j4q22B2Om5E7BeR7iw9drl3PTOBO2kg="; 12 + }; 13 + 14 + vendorSha256 = "sha256-2aaUlOokqYkjMpcM12mU+O+N09/mDPlIrJ4Z1iXJAyk="; 15 + 16 + meta = with lib; { 17 + description = "Prometheus exporter for PgBouncer"; 18 + homepage = "https://github.com/prometheus-community/pgbouncer_exporter"; 19 + license = licenses.mit; 20 + maintainers = with maintainers; [ _1000101 ]; 21 + platforms = platforms.linux; 22 + }; 23 + }
+1
pkgs/top-level/all-packages.nix
··· 27339 27339 prometheus-nut-exporter = callPackage ../servers/monitoring/prometheus/nut-exporter.nix { }; 27340 27340 prometheus-openldap-exporter = callPackage ../servers/monitoring/prometheus/openldap-exporter.nix { } ; 27341 27341 prometheus-openvpn-exporter = callPackage ../servers/monitoring/prometheus/openvpn-exporter.nix { }; 27342 + prometheus-pgbouncer-exporter = callPackage ../servers/monitoring/prometheus/pgbouncer-exporter.nix { }; 27342 27343 prometheus-php-fpm-exporter = callPackage ../servers/monitoring/prometheus/php-fpm-exporter.nix { }; 27343 27344 prometheus-pihole-exporter = callPackage ../servers/monitoring/prometheus/pihole-exporter.nix { }; 27344 27345 prometheus-postfix-exporter = callPackage ../servers/monitoring/prometheus/postfix-exporter.nix { };