1# Primarily reference the implementation of <nixos/tests/prometheus/remote-write.nix>
2{ lib, pkgs, ... }:
3let
4 username = "vmtest";
5 password = "fsddfy8233rb"; # random string
6 passwordFile = pkgs.writeText "password-file" password;
7in
8{
9 name = "victoriametrics-remote-write";
10 meta = with lib.maintainers; {
11 maintainers = [
12 yorickvp
13 ryan4yin
14 ];
15 };
16
17 nodes = {
18 victoriametrics =
19 { pkgs, ... }:
20 {
21 environment.systemPackages = [ pkgs.jq ];
22 networking.firewall.allowedTCPPorts = [ 8428 ];
23 services.victoriametrics = {
24 enable = true;
25 basicAuthUsername = username;
26 basicAuthPasswordFile = toString passwordFile;
27 };
28 };
29
30 vmagent =
31 { config, pkgs, ... }:
32 {
33 environment.systemPackages = [ pkgs.jq ];
34 services.vmagent = {
35 enable = true;
36 remoteWrite = {
37 url = "http://victoriametrics:8428/api/v1/write";
38 basicAuthUsername = username;
39 basicAuthPasswordFile = toString passwordFile;
40 };
41
42 prometheusConfig = {
43 global = {
44 scrape_interval = "2s";
45 };
46 scrape_configs = [
47 {
48 job_name = "node";
49 static_configs = [
50 {
51 targets = [
52 "node:${toString config.services.prometheus.exporters.node.port}"
53 ];
54 }
55 ];
56 }
57 ];
58 };
59 };
60 };
61
62 node = {
63 services.prometheus.exporters.node = {
64 enable = true;
65 openFirewall = true;
66 };
67 };
68 };
69
70 testScript = ''
71 node.wait_for_unit("prometheus-node-exporter")
72 node.wait_for_open_port(9100)
73
74 victoriametrics.wait_for_unit("victoriametrics")
75 victoriametrics.wait_for_open_port(8428)
76
77 vmagent.wait_for_unit("vmagent")
78
79 # check remote write
80 victoriametrics.wait_until_succeeds(
81 "curl --user '${username}:${password}' -sf 'http://localhost:8428/api/v1/query?query=node_exporter_build_info\{instance=\"node:9100\"\}' | "
82 + "jq '.data.result[0].value[1]' | grep '\"1\"'"
83 )
84 '';
85}