summary refs log tree commit diff
path: root/nixos/modules/services/databases/victoriametrics.nix
blob: 0513dcff172b3a8bba00ff1f7a77d3f68cab93a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{ config, pkgs, lib, ... }:
let cfg = config.services.victoriametrics; in
{
  options.services.victoriametrics = with lib; {
    enable = mkEnableOption "victoriametrics";
    package = mkOption {
      type = types.package;
      default = pkgs.victoriametrics;
      defaultText = literalExpression "pkgs.victoriametrics";
      description = ''
        The VictoriaMetrics distribution to use.
      '';
    };
    listenAddress = mkOption {
      default = ":8428";
      type = types.str;
      description = ''
        The listen address for the http interface.
      '';
    };
    retentionPeriod = mkOption {
      type = types.int;
      default = 1;
      description = ''
        Retention period in months.
      '';
    };
    extraOptions = mkOption {
      type = types.listOf types.str;
      default = [];
      description = ''
        Extra options to pass to VictoriaMetrics. See the README: <link
        xlink:href="https://github.com/VictoriaMetrics/VictoriaMetrics/blob/master/README.md" />
        or <command>victoriametrics -help</command> for more
        information.
      '';
    };
  };
  config = lib.mkIf cfg.enable {
    systemd.services.victoriametrics = {
      description = "VictoriaMetrics time series database";
      after = [ "network.target" ];
      startLimitBurst = 5;
      serviceConfig = {
        Restart = "on-failure";
        RestartSec = 1;
        StateDirectory = "victoriametrics";
        DynamicUser = true;
        ExecStart = ''
          ${cfg.package}/bin/victoria-metrics \
              -storageDataPath=/var/lib/victoriametrics \
              -httpListenAddr ${cfg.listenAddress} \
              -retentionPeriod ${toString cfg.retentionPeriod} \
              ${lib.escapeShellArgs cfg.extraOptions}
        '';
        # victoriametrics 1.59 with ~7GB of data seems to eventually panic when merging files and then
        # begins restart-looping forever. Set LimitNOFILE= to a large number to work around this issue.
        #
        # panic: FATAL: unrecoverable error when merging small parts in the partition "/var/lib/victoriametrics/data/small/2021_08":
        # cannot open source part for merging: cannot open values file in stream mode:
        # cannot open file "/var/lib/victoriametrics/data/small/2021_08/[...]/values.bin":
        # open /var/lib/victoriametrics/data/small/2021_08/[...]/values.bin: too many open files
        LimitNOFILE = 1048576;
      };
      wantedBy = [ "multi-user.target" ];

      postStart =
        let
          bindAddr = (lib.optionalString (lib.hasPrefix ":" cfg.listenAddress) "127.0.0.1") + cfg.listenAddress;
        in
        lib.mkBefore ''
          until ${lib.getBin pkgs.curl}/bin/curl -s -o /dev/null http://${bindAddr}/ping; do
            sleep 1;
          done
        '';
    };
  };
}