diff options
author | Justinas Stankevicius <justinas@justinas.org> | 2020-08-06 09:56:35 +0300 |
---|---|---|
committer | Maximilian Bosch <maximilian@mbosch.me> | 2020-11-06 16:35:38 +0100 |
commit | d447c2413cbace4fdcbeb6a364bc08d310309c4e (patch) | |
tree | 3523abef3fd6c8f4c081de238e6ad1e0b678cb0e /nixos/modules/services/monitoring/prometheus/exporters/sql.nix | |
parent | 01762fec0ab7576e3b371eda3dd3fd576e640798 (diff) | |
download | nixpkgs-d447c2413cbace4fdcbeb6a364bc08d310309c4e.tar nixpkgs-d447c2413cbace4fdcbeb6a364bc08d310309c4e.tar.gz nixpkgs-d447c2413cbace4fdcbeb6a364bc08d310309c4e.tar.bz2 nixpkgs-d447c2413cbace4fdcbeb6a364bc08d310309c4e.tar.lz nixpkgs-d447c2413cbace4fdcbeb6a364bc08d310309c4e.tar.xz nixpkgs-d447c2413cbace4fdcbeb6a364bc08d310309c4e.tar.zst nixpkgs-d447c2413cbace4fdcbeb6a364bc08d310309c4e.zip |
nixos/prometheus-sql-exporter: new module
Diffstat (limited to 'nixos/modules/services/monitoring/prometheus/exporters/sql.nix')
-rw-r--r-- | nixos/modules/services/monitoring/prometheus/exporters/sql.nix | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/nixos/modules/services/monitoring/prometheus/exporters/sql.nix b/nixos/modules/services/monitoring/prometheus/exporters/sql.nix new file mode 100644 index 00000000000..d9be724ebc0 --- /dev/null +++ b/nixos/modules/services/monitoring/prometheus/exporters/sql.nix @@ -0,0 +1,104 @@ +{ config, lib, pkgs, options }: +with lib; +let + cfg = config.services.prometheus.exporters.sql; + cfgOptions = { + options = with types; { + jobs = mkOption { + type = attrsOf (submodule jobOptions); + default = { }; + description = "An attrset of metrics scraping jobs to run."; + }; + }; + }; + jobOptions = { + options = with types; { + interval = mkOption { + type = str; + description = '' + How often to run this job, specified in + <link xlink:href="https://golang.org/pkg/time/#ParseDuration">Go duration</link> format. + ''; + }; + connections = mkOption { + type = listOf str; + description = "A list of connection strings of the SQL servers to scrape metrics from"; + }; + startupSql = mkOption { + type = listOf str; + default = []; + description = "A list of SQL statements to execute once after making a connection."; + }; + queries = mkOption { + type = attrsOf (submodule queryOptions); + description = "SQL queries to run."; + }; + }; + }; + queryOptions = { + options = with types; { + help = mkOption { + type = nullOr str; + default = null; + description = "A human-readable description of this metric."; + }; + labels = mkOption { + type = listOf str; + default = [ ]; + description = "A set of columns that will be used as Prometheus labels."; + }; + query = mkOption { + type = str; + description = "The SQL query to run."; + }; + values = mkOption { + type = listOf str; + description = "A set of columns that will be used as values of this metric."; + }; + }; + }; + + configFile = + if cfg.configFile != null + then cfg.configFile + else + let + nameInline = mapAttrsToList (k: v: v // { name = k; }); + renameStartupSql = j: removeAttrs (j // { startup_sql = j.startupSql; }) [ "startupSql" ]; + configuration = { + jobs = map renameStartupSql + (nameInline (mapAttrs (k: v: (v // { queries = nameInline v.queries; })) cfg.configuration.jobs)); + }; + in + builtins.toFile "config.yaml" (builtins.toJSON configuration); +in +{ + extraOpts = { + configFile = mkOption { + type = with types; nullOr path; + default = null; + description = '' + Path to configuration file. + ''; + }; + configuration = mkOption { + type = with types; nullOr (submodule cfgOptions); + default = null; + description = '' + Exporter configuration as nix attribute set. Mutually exclusive with 'configFile' option. + ''; + }; + }; + + port = 9237; + serviceOpts = { + serviceConfig = { + ExecStart = '' + ${pkgs.prometheus-sql-exporter}/bin/sql_exporter \ + -web.listen-address ${cfg.listenAddress}:${toString cfg.port} \ + -config.file ${configFile} \ + ${concatStringsSep " \\\n " cfg.extraFlags} + ''; + }; + }; +} |