1{ config, lib, pkgs, options }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.exporters.postgres;
7in
8{
9 port = 9187;
10 extraOpts = {
11 telemetryPath = mkOption {
12 type = types.str;
13 default = "/metrics";
14 description = ''
15 Path under which to expose metrics.
16 '';
17 };
18 dataSourceName = mkOption {
19 type = types.str;
20 default = "user=postgres database=postgres host=/run/postgresql sslmode=disable";
21 example = "postgresql://username:password@localhost:5432/postgres?sslmode=disable";
22 description = ''
23 Accepts PostgreSQL URI form and key=value form arguments.
24 '';
25 };
26 runAsLocalSuperUser = mkOption {
27 type = types.bool;
28 default = false;
29 description = ''
30 Whether to run the exporter as the local 'postgres' super user.
31 '';
32 };
33
34 # TODO perhaps LoadCredential would be more appropriate
35 environmentFile = mkOption {
36 type = types.nullOr types.path;
37 default = null;
38 example = "/root/prometheus-postgres-exporter.env";
39 description = ''
40 Environment file as defined in <citerefentry>
41 <refentrytitle>systemd.exec</refentrytitle><manvolnum>5</manvolnum>
42 </citerefentry>.
43
44 Secrets may be passed to the service without adding them to the
45 world-readable Nix store, by specifying placeholder variables as
46 the option value in Nix and setting these variables accordingly in the
47 environment file.
48
49 Environment variables from this file will be interpolated into the
50 config file using envsubst with this syntax:
51 <literal>$ENVIRONMENT ''${VARIABLE}</literal>
52
53 The main use is to set the DATA_SOURCE_NAME that contains the
54 postgres password
55
56 note that contents from this file will override dataSourceName
57 if you have set it from nix.
58
59 <programlisting>
60 # Content of the environment file
61 DATA_SOURCE_NAME=postgresql://username:password@localhost:5432/postgres?sslmode=disable
62 </programlisting>
63
64 Note that this file needs to be available on the host on which
65 this exporter is running.
66 '';
67 };
68
69 };
70 serviceOpts = {
71 environment.DATA_SOURCE_NAME = cfg.dataSourceName;
72 serviceConfig = {
73 DynamicUser = false;
74 User = mkIf cfg.runAsLocalSuperUser (mkForce "postgres");
75 EnvironmentFile = mkIf (cfg.environmentFile != null) [ cfg.environmentFile ];
76 ExecStart = ''
77 ${pkgs.prometheus-postgres-exporter}/bin/postgres_exporter \
78 --web.listen-address ${cfg.listenAddress}:${toString cfg.port} \
79 --web.telemetry-path ${cfg.telemetryPath} \
80 ${concatStringsSep " \\\n " cfg.extraFlags}
81 '';
82 };
83 };
84}