Skip to content

Commit 974ea77

Browse files
authored
feat(grafana-loki): init (#626)
Adds Grafana Loki as a service. https://grafana.com/oss/loki/
1 parent f21943d commit 974ea77

6 files changed

Lines changed: 188 additions & 0 deletions

File tree

doc/loki.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Grafana Loki
2+
3+
[Grafana Loki](https://grafana.com/docs/loki/latest/) is a log aggregation system designed to store and query logs from all your applications and infrastructure.
4+
5+
## Getting Started
6+
7+
```nix
8+
# In `perSystem.process-compose.<name>`
9+
{
10+
services.loki."tp1".enable = true;
11+
}
12+
```
13+
14+
{#tips}
15+
## Tips & Tricks
16+
17+
{#usage-with-grafana}
18+
### Usage with Grafana
19+
20+
To add loki as a datasource to #[[grafana]], we can use the following config:
21+
22+
```nix
23+
{
24+
services.loki.tp1.enable = true;
25+
services.grafana.gf1 = {
26+
enable = true;
27+
datasources = with config.services.loki.tp1; [{
28+
name = "Loki";
29+
type = "loki";
30+
access = "proxy";
31+
url = "http://${httpAddress}:${builtins.toString httpPort}";
32+
}];
33+
};
34+
settings.processes."gf1".depends_on."tp1".condition = "process_healthy";
35+
}
36+
```

doc/services.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ short-title: Services
1111
- [[elasticsearch]]#
1212
- [[grafana]]#
1313
- [[tempo]]
14+
- [[loki]]
1415
- [[memcached]]#
1516
- [[minio]]#
1617
- [[mongodb]]#

nix/services/default.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ in
2828
./weaviate.nix
2929
./searxng.nix
3030
./tika.nix
31+
./loki.nix
3132
./phpfpm.nix
3233
./pubsub-emulator.nix
3334
]) ++ [

nix/services/loki.nix

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
{ pkgs
2+
, lib
3+
, name
4+
, config
5+
, ...
6+
}:
7+
let
8+
inherit (lib) types;
9+
yamlFormat = pkgs.formats.yaml { };
10+
in
11+
{
12+
options = {
13+
package = lib.mkPackageOption pkgs "grafana-loki" { };
14+
15+
httpAddress = lib.mkOption {
16+
type = types.str;
17+
description = "Which address to access loki from.";
18+
default = "localhost";
19+
};
20+
21+
httpPort = lib.mkOption {
22+
type = types.port;
23+
description = "Which HTTP port to loki should listen on.";
24+
default = 3100;
25+
};
26+
27+
grpcPort = lib.mkOption {
28+
type = types.port;
29+
description = "Which gRPC port to run loki should listen on.";
30+
default = 9096;
31+
};
32+
33+
extraConfig = lib.mkOption {
34+
type = yamlFormat.type;
35+
default = { };
36+
description = ''
37+
Specify the configuration for Loki in Nix.
38+
39+
See https://grafana.com/docs/loki/latest/configuration/ for available options.
40+
'';
41+
};
42+
43+
extraFlags = lib.mkOption {
44+
type = types.listOf types.str;
45+
default = [ ];
46+
example = lib.literalExpression ''
47+
[ "-config.expand-env=true" ]
48+
'';
49+
description = ''
50+
Additional flags to pass to loki.
51+
'';
52+
};
53+
};
54+
55+
config = {
56+
outputs = {
57+
settings = {
58+
processes."${name}" =
59+
let
60+
lokiConfig = lib.recursiveUpdate
61+
{
62+
server = {
63+
http_listen_port = config.httpPort;
64+
grpc_listen_port = config.grpcPort;
65+
};
66+
common = {
67+
instance_addr = config.httpAddress;
68+
path_prefix = "${config.dataDir}/";
69+
storage = {
70+
filesystem = {
71+
chunks_directory = "${config.dataDir}/chunks";
72+
rules_directory = "${config.dataDir}/rules";
73+
};
74+
};
75+
replication_factor = 1;
76+
ring = {
77+
kvstore = {
78+
store = "inmemory";
79+
};
80+
};
81+
};
82+
schema_config = {
83+
configs = [
84+
{
85+
from = "2020-10-24";
86+
store = "tsdb";
87+
object_store = "filesystem";
88+
schema = "v13";
89+
index = {
90+
prefix = "index_";
91+
period = "24h";
92+
};
93+
}
94+
];
95+
};
96+
}
97+
config.extraConfig;
98+
lokiConfigYaml = yamlFormat.generate "loki.yaml" lokiConfig;
99+
in
100+
{
101+
command = "${config.package}/bin/loki --config.file=${lokiConfigYaml} ${lib.escapeShellArgs config.extraFlags}";
102+
readiness_probe = {
103+
http_get = {
104+
host = config.httpAddress;
105+
scheme = "http";
106+
port = config.httpPort;
107+
path = "/ready";
108+
};
109+
initial_delay_seconds = 15;
110+
period_seconds = 10;
111+
timeout_seconds = 2;
112+
success_threshold = 1;
113+
failure_threshold = 5;
114+
};
115+
availability = {
116+
restart = "on_failure";
117+
max_restarts = 5;
118+
};
119+
};
120+
};
121+
};
122+
};
123+
}

nix/services/loki_test.nix

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{ pkgs, config, ... }:
2+
{
3+
services.loki."tp1" = {
4+
enable = true;
5+
};
6+
7+
settings.processes.test =
8+
let
9+
cfg = config.services.loki."tp1";
10+
in
11+
{
12+
command = pkgs.writeShellApplication {
13+
runtimeInputs = [
14+
cfg.package
15+
pkgs.gnugrep
16+
pkgs.curl
17+
];
18+
text = ''
19+
ROOT_URL="http://${cfg.httpAddress}:${builtins.toString cfg.httpPort}";
20+
curl -sSfN $ROOT_URL/ready | grep "ready"
21+
'';
22+
name = "loki-test";
23+
};
24+
depends_on."tp1".condition = "process_healthy";
25+
};
26+
}

test/flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"${inputs.services-flake}/nix/services/redis-cluster_test.nix"
6161
"${inputs.services-flake}/nix/services/searxng_test.nix"
6262
"${inputs.services-flake}/nix/services/tempo_test.nix"
63+
"${inputs.services-flake}/nix/services/loki_test.nix"
6364
"${inputs.services-flake}/nix/services/tika_test.nix"
6465
"${inputs.services-flake}/nix/services/weaviate_test.nix"
6566
"${inputs.services-flake}/nix/services/zookeeper_test.nix"

0 commit comments

Comments
 (0)