git.iko.soy/typeable/nixpkgs
on master help sign in
nixos/prometheus/pushgateway: add module and test
commitd1940beb3a0547d5892f0e063a515a3923a92be5 authorBas van Dijk <v.dijk.bas@gmail.com> dateMon 15 Apr 2019, 23:46 (8 years ago) parent888f32f
3 files changed+207−7
nixos/modules/module-list.nix+1 nixos/modules/services/monitoring/prometheus/pushgateway.nix+166 nixos/tests/prometheus-2.nix+40−7
nixos/modules/module-list.nix+1
@@ -491,6 +491,7 @@
491491 ./services/monitoring/prometheus/default.nix
492492 ./services/monitoring/prometheus/alertmanager.nix
493493 ./services/monitoring/prometheus/exporters.nix
494+ ./services/monitoring/prometheus/pushgateway.nix
494495 ./services/monitoring/riemann.nix
495496 ./services/monitoring/riemann-dash.nix
496497 ./services/monitoring/riemann-tools.nix
nixos/modules/services/monitoring/prometheus/pushgateway.nixnew file+166
@@ -0,0 +1,166 @@
1+{ config, pkgs, lib, ... }:
2+
3+with lib;
4+
5+let
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+
21+in {
22+ options = {
23+ services.prometheus.pushgateway = {
24+ enable = mkEnableOption "Prometheus Pushgateway";
25+
26+ package = mkOption {
27+ type = types.package;
28+ default = pkgs.prometheus-pushgateway;
29+ defaultText = "pkgs.prometheus-pushgateway";
30+ description = ''
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 = ''
39+ Address to listen on for the web interface, API and telemetry.
40+
41+ <literal>null</literal> will default to <literal>:9091</literal>.
42+ '';
43+ };
44+
45+ web.telemetry-path = mkOption {
46+ type = types.nullOr types.str;
47+ default = null;
48+ description = ''
49+ Path under which to expose metrics.
50+
51+ <literal>null</literal> will default to <literal>/metrics</literal>.
52+ '';
53+ };
54+
55+ web.external-url = mkOption {
56+ type = types.nullOr types.str;
57+ default = null;
58+ description = ''
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 = ''
67+ Prefix for the internal routes of web endpoints.
68+
69+ Defaults to the path of
70+ <option>services.prometheus.pushgateway.web.external-url</option>.
71+ '';
72+ };
73+
74+ persistence.interval = mkOption {
75+ type = types.nullOr types.str;
76+ default = null;
77+ example = "10m";
78+ description = ''
79+ The minimum interval at which to write out the persistence file.
80+
81+ <literal>null</literal> will default to <literal>5m</literal>.
82+ '';
83+ };
84+
85+ log.level = mkOption {
86+ type = types.nullOr (types.enum ["debug" "info" "warn" "error" "fatal"]);
87+ default = null;
88+ description = ''
89+ Only log messages with the given severity or above.
90+
91+ <literal>null</literal> will default to <literal>info</literal>.
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 = ''
100+ Set the log target and format.
101+
102+ <literal>null</literal> will default to <literal>logger:stderr</literal>.
103+ '';
104+ };
105+
106+ extraFlags = mkOption {
107+ type = types.listOf types.str;
108+ default = [];
109+ description = ''
110+ Extra commandline options when launching the Pushgateway.
111+ '';
112+ };
113+
114+ persistMetrics = mkOption {
115+ type = types.bool;
116+ default = false;
117+ description = ''
118+ Whether to persist metrics to a file.
119+
120+ When enabled metrics will be saved to a file called
121+ <literal>metrics</literal> in the directory
122+ <literal>/var/lib/pushgateway</literal>. The directory below
123+ <literal>/var/lib</literal> can be set using
124+ <option>services.prometheus.pushgateway.stateDir</option>.
125+ '';
126+ };
127+
128+ stateDir = mkOption {
129+ type = types.str;
130+ default = "pushgateway";
131+ description = ''
132+ Directory below <literal>/var/lib</literal> to store metrics.
133+
134+ This directory will be created automatically using systemd's
135+ StateDirectory mechanism when
136+ <option>services.prometheus.pushgateway.persistMetrics</option>
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+}
nixos/tests/prometheus-2.nix+40−7
@@ -3,15 +3,29 @@ import ./make-test.nix {
33
44 nodes = {
55 one = { pkgs, ... }: {
6+ environment.systemPackages = [ pkgs.jq ];
67 services.prometheus2 = {
78 enable = true;
8− scrapeConfigs = [{
9− job_name = "prometheus";
10− static_configs = [{
11− targets = [ "127.0.0.1:9090" ];
12− labels = { instance = "localhost"; };
13− }];
14− }];
9+ scrapeConfigs = [
10+ {
11+ job_name = "prometheus";
12+ static_configs = [
13+ {
14+ targets = [ "127.0.0.1:9090" ];
15+ labels = { instance = "localhost"; };
16+ }
17+ ];
18+ }
19+ {
20+ job_name = "pushgateway";
21+ scrape_interval = "1s";
22+ static_configs = [
23+ {
24+ targets = [ "127.0.0.1:9091" ];
25+ }
26+ ];
27+ }
28+ ];
1529 rules = [
1630 ''
1731 groups:
@@ -22,6 +36,12 @@ import ./make-test.nix {
2236 ''
2337 ];
2438 };
39+ services.prometheus.pushgateway = {
40+ enable = true;
41+ persistMetrics = true;
42+ persistence.interval = "1s";
43+ stateDir = "prometheus-pushgateway";
44+ };
2545 };
2646 };
2747
@@ -30,5 +50,18 @@ import ./make-test.nix {
3050 $one->waitForUnit("prometheus2.service");
3151 $one->waitForOpenPort(9090);
3252 $one->succeed("curl -s http://127.0.0.1:9090/metrics");
53+
54+ # Let's test if pushing a metric to the pushgateway succeeds
55+ # and whether that metric gets ingested by prometheus.
56+ $one->waitForUnit("pushgateway.service");
57+ $one->succeed(
58+ "echo 'some_metric 3.14' | " .
59+ "curl --data-binary \@- http://127.0.0.1:9091/metrics/job/some_job");
60+ $one->waitUntilSucceeds(
61+ "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=some_metric' " .
62+ "| jq '.data.result[0].value[1]' | grep '\"3.14\"'");
63+
64+ # Let's test if the pushgateway persists metrics to the configured location.
65+ $one->waitUntilSucceeds("test -e /var/lib/prometheus-pushgateway/metrics");
3366 '';
3467}