aboutsummaryrefslogtreecommitdiffstats
path: root/modules/by-name/mo/monitoring/components/loki.nix
blob: 9a285c51f71957cd547d736a1885d0bf93e1ade3 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
{
  config,
  lib,
  pkgs,
  ...
}: let
  cfg = config.vhack.monitoring.loki;
in {
  options.vhack.monitoring.loki = {
    enable = lib.mkEnableOption "loki";

    port = lib.mkOption {
      type = lib.types.port;
      description = "Port where Loki listens to HTTP requests.";
      default = 3002;
    };
  };

  config = lib.mkIf cfg.enable {
    services = {
      loki = {
        enable = true;
        dataDir = "/var/lib/loki";
        package = pkgs.grafana-loki;
        configuration = {
          auth_enabled = false;

          server.http_listen_port = cfg.port;

          common = {
            ring = {
              instance_addr = "127.0.0.1";
              kvstore = {
                store = "inmemory";
              };
            };

            replication_factor = 1;
            path_prefix = "/tmp/loki";
          };

          schema_config = {
            configs = [
              {
                from = "2020-05-15";
                store = "tsdb";
                object_store = "filesystem";
                schema = "v13";
                index = {
                  prefix = "index_";
                  period = "24h";
                };
              }
            ];
          };

          storage_config = {
            filesystem.directory = "/tmp/loki/chunks";
          };
        };
      };

      # I decided to switch to fluent-bit because it can be tested locally https://docs.fluentbit.io/manual/local-testing/logging-pipeline
      fluent-bit = {
        enable = true;
        settings = {
          service = {
            flush = 1;
            log_level = "info";
            http_server = "true";
            http_listen = "127.0.0.1";
            http_port = 9080;
            grace = 30;
          };

          pipeline = {
            inputs = [
              {
                name = "systemd";

                # The asterisk appends the _SYSTEMD_UNIT to the prefix.
                tag = "systemd.*";

                # Read logs from this systemd journal directory.
                path = "/var/log/journal";

                # Database file to keep track of the journald cursor.
                db = "/var/fluent-bit/systemd.db";

                # Start reading new entries. Skip entries already stored in journald.
                read_from_tail = true;

                # Max entries to lookback on start.
                max_entries = 10000;
              }
            ];

            outputs = [
              {
                name = "loki";

                match = "systemd.*";

                host = "localhost";
                port = config.services.loki.configuration.server.http_listen_port;

                labels = lib.concatMapAttrsStringSep ", " (n: v: "${n}=${v}") {
                  job = "systemd-journal";
                  inherit (config.vhack.monitoring.grafana) fqdn;
                  hostname = config.networking.hostName;
                };

                label_keys = "$unit";
              }
            ];
          };
        };
        graceLimit = "1m";
      };
    };
  };
}