1{ config, pkgs, lib, ... }:
2
3with lib;
4
5let
6 cfg = config.services.prometheus.pushgateway;
7
8 cmdlineArgs =
9 opt "web.listen-address" cfg.web.listen-address
10 ++ opt "web.telemetry-path" cfg.web.telemetry-path
11 ++ opt "web.external-url" cfg.web.external-url
12 ++ opt "web.route-prefix" cfg.web.route-prefix
13 ++ optional cfg.persistMetrics ''--persistence.file="/var/lib/${cfg.stateDir}/metrics"''
14 ++ opt "persistence.interval" cfg.persistence.interval
15 ++ opt "log.level" cfg.log.level
16 ++ opt "log.format" cfg.log.format
17 ++ cfg.extraFlags;
18
19 opt = k : v : optional (v != null) ''--${k}="${v}"'';
20
21in {
22 options = {
23 services.prometheus.pushgateway = {
24 enable = mkEnableOption "Prometheus Pushgateway";
25
26 package = mkPackageOption pkgs "prometheus-pushgateway" { };
27
28 web.listen-address = mkOption {
29 type = types.nullOr types.str;
30 default = null;
31 description = ''
32 Address to listen on for the web interface, API and telemetry.
33
34 `null` will default to `:9091`.
35 '';
36 };
37
38 web.telemetry-path = mkOption {
39 type = types.nullOr types.str;
40 default = null;
41 description = ''
42 Path under which to expose metrics.
43
44 `null` will default to `/metrics`.
45 '';
46 };
47
48 web.external-url = mkOption {
49 type = types.nullOr types.str;
50 default = null;
51 description = ''
52 The URL under which Pushgateway is externally reachable.
53 '';
54 };
55
56 web.route-prefix = mkOption {
57 type = types.nullOr types.str;
58 default = null;
59 description = ''
60 Prefix for the internal routes of web endpoints.
61
62 Defaults to the path of
63 {option}`services.prometheus.pushgateway.web.external-url`.
64 '';
65 };
66
67 persistence.interval = mkOption {
68 type = types.nullOr types.str;
69 default = null;
70 example = "10m";
71 description = ''
72 The minimum interval at which to write out the persistence file.
73
74 `null` will default to `5m`.
75 '';
76 };
77
78 log.level = mkOption {
79 type = types.nullOr (types.enum ["debug" "info" "warn" "error" "fatal"]);
80 default = null;
81 description = ''
82 Only log messages with the given severity or above.
83
84 `null` will default to `info`.
85 '';
86 };
87
88 log.format = mkOption {
89 type = types.nullOr types.str;
90 default = null;
91 example = "logger:syslog?appname=bob&local=7";
92 description = ''
93 Set the log target and format.
94
95 `null` will default to `logger:stderr`.
96 '';
97 };
98
99 extraFlags = mkOption {
100 type = types.listOf types.str;
101 default = [];
102 description = ''
103 Extra commandline options when launching the Pushgateway.
104 '';
105 };
106
107 persistMetrics = mkOption {
108 type = types.bool;
109 default = false;
110 description = ''
111 Whether to persist metrics to a file.
112
113 When enabled metrics will be saved to a file called
114 `metrics` in the directory
115 `/var/lib/pushgateway`. The directory below
116 `/var/lib` can be set using
117 {option}`services.prometheus.pushgateway.stateDir`.
118 '';
119 };
120
121 stateDir = mkOption {
122 type = types.str;
123 default = "pushgateway";
124 description = ''
125 Directory below `/var/lib` to store metrics.
126
127 This directory will be created automatically using systemd's
128 StateDirectory mechanism when
129 {option}`services.prometheus.pushgateway.persistMetrics`
130 is enabled.
131 '';
132 };
133 };
134 };
135
136 config = mkIf cfg.enable {
137 assertions = [
138 {
139 assertion = !hasPrefix "/" cfg.stateDir;
140 message =
141 "The option services.prometheus.pushgateway.stateDir" +
142 " shouldn't be an absolute directory." +
143 " It should be a directory relative to /var/lib.";
144 }
145 ];
146 systemd.services.pushgateway = {
147 wantedBy = [ "multi-user.target" ];
148 after = [ "network.target" ];
149 serviceConfig = {
150 Restart = "always";
151 DynamicUser = true;
152 ExecStart = "${cfg.package}/bin/pushgateway" +
153 optionalString (length cmdlineArgs != 0) (" \\\n " +
154 concatStringsSep " \\\n " cmdlineArgs);
155 StateDirectory = if cfg.persistMetrics then cfg.stateDir else null;
156 };
157 };
158 };
159}