aboutsummaryrefslogtreecommitdiffstats
path: root/modules/by-name/mo/monitoring/components/loki.nix
blob: 149e577a0a3f9c0ba15b07e8c0accba23eb057f7 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
{
  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 {
    vhack = {
      persist.directories = [
        {
          directory = "/var/lib/loki";
          user = "loki";
          group = "loki";
          mode = "0700";
        }
        {
          directory = "/var/lib/fluent-bit";
          user = "fluent-bit";
          group = "fluent-bit";
          mode = "0700";
        }
      ];
    };

    users = {
      groups = {
        fluent-bit = {
          gid = config.vhack.constants.ids.gids.fluent-bit;
        };
        loki = {
          gid = config.vhack.constants.ids.gids.loki;
        };
      };
      users = {
        fluent-bit = {
          isSystemUser = true;
          group = "fluent-bit";
          uid = config.vhack.constants.ids.uids.fluent-bit;
        };
        loki = {
          isSystemUser = true;
          group = "loki";
          uid = config.vhack.constants.ids.uids.loki;
        };
      };
    };

    # I decided to switch to fluent-bit because it can be tested locally https://docs.fluentbit.io/manual/local-testing/logging-pipeline
    systemd.services.fluent-bit = {
      serviceConfig = {
        StateDirectory = "fluent-bit";
        User = "fluent-bit";
        Group = "fluent-bit";
        DynamicUser = lib.mkForce false;
      };
    };
    services = {
      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/lib/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";
      };

      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";
          };
        };
      };
    };
  };
}