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