1{ pkgs, ... }:
2
3{
4 name = "prometheus-pushgateway";
5
6 nodes = {
7 prometheus =
8 { config, pkgs, ... }:
9 {
10 environment.systemPackages = [ pkgs.jq ];
11
12 networking.firewall.allowedTCPPorts = [ config.services.prometheus.port ];
13
14 services.prometheus = {
15 enable = true;
16 globalConfig.scrape_interval = "2s";
17
18 scrapeConfigs = [
19 {
20 job_name = "pushgateway";
21 static_configs = [ { targets = [ "pushgateway:9091" ]; } ];
22 }
23 ];
24 };
25 };
26
27 pushgateway =
28 { config, pkgs, ... }:
29 {
30 networking.firewall.allowedTCPPorts = [ 9091 ];
31
32 services.prometheus.pushgateway = {
33 enable = true;
34 };
35 };
36
37 client = { config, pkgs, ... }: { };
38 };
39
40 testScript = ''
41 pushgateway.wait_for_unit("pushgateway")
42 pushgateway.wait_for_open_port(9091)
43 pushgateway.wait_until_succeeds("curl -s http://127.0.0.1:9091/-/ready")
44 pushgateway.wait_until_succeeds("journalctl -o cat -u pushgateway.service | grep 'version=${pkgs.prometheus-pushgateway.version}'")
45
46 prometheus.wait_for_unit("prometheus")
47 prometheus.wait_for_open_port(9090)
48
49 prometheus.wait_until_succeeds(
50 "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=count(up\{job=\"pushgateway\"\})' | "
51 + "jq '.data.result[0].value[1]' | grep '\"1\"'"
52 )
53
54 prometheus.wait_until_succeeds(
55 "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=sum(pushgateway_build_info)%20by%20(version)' | "
56 + "jq '.data.result[0].metric.version' | grep '\"${pkgs.prometheus-pushgateway.version}\"'"
57 )
58
59 # Add a metric and check in Prometheus
60 client.wait_until_succeeds(
61 "echo 'some_metric 3.14' | curl --data-binary @- http://pushgateway:9091/metrics/job/some_job"
62 )
63
64 prometheus.wait_until_succeeds(
65 "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=some_metric' | "
66 + "jq '.data.result[0].value[1]' | grep '\"3.14\"'"
67 )
68
69 prometheus.wait_until_succeeds(
70 "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=absent(some_metric)' | "
71 + "jq '.data.result[0].value[1]' | grep 'null'"
72 )
73
74 # Delete the metric, check not in Prometheus
75 client.wait_until_succeeds(
76 "curl -X DELETE http://pushgateway:9091/metrics/job/some_job"
77 )
78
79 prometheus.wait_until_fails(
80 "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=some_metric' | "
81 + "jq '.data.result[0].value[1]' | grep '\"3.14\"'"
82 )
83
84 prometheus.wait_until_succeeds(
85 "curl -sf 'http://127.0.0.1:9090/api/v1/query?query=absent(some_metric)' | "
86 + "jq '.data.result[0].value[1]' | grep '\"1\"'"
87 )
88
89 pushgateway.log(pushgateway.succeed("systemd-analyze security pushgateway.service | grep -v '✓'"))
90 '';
91}