feat: prometheus nixos module & test-container

for testing grafana in container
export QEMU_NET_OPTS="hostfwd=tcp::3000-:3000"
 nixos-rebuild build-vm --flake .#test-container
and then run the container, login is test/test

and the grafana port is forwarded to host machine, yay
This commit is contained in:
efim 2023-12-02 08:19:13 +00:00
parent 0a8db09fe8
commit 42c73c5902
2 changed files with 47 additions and 15 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@
/result /result
/routes/static/out.css /routes/static/out.css
/nixos.qcow2 /nixos.qcow2
/data/

View File

@ -70,15 +70,33 @@
host = "some-automoderation.sunshine.industries"; host = "some-automoderation.sunshine.industries";
useNginx = false; useNginx = false;
port = 9090; port = 9090;
metricsPort = 9091;
redisPort = 9999; redisPort = 9999;
enablePrometheus = true;
};
services.prometheus = {
enable = true;
port = 9998;
# scrape config will be set up by the module
};
services.grafana = {
enable = true;
settings.server.http_port = 3000;
settings.server.http_addr = "0.0.0.0";
provision.datasources = {
settings.datasources = [{
name = "local-prometheus";
type = "prometheus";
url = "http://localhost:9998";
}];
};
}; };
}) })
]; ];
}; };
}; };
nixosModules.some-automoderation-module = { lib, pkgs, config, ... }: nixosModules.some-automoderation-module = { lib, pkgs, config, ... }:
let let cfg = config.services.${pname};
cfg = config.services.${pname};
in { in {
options.services.${pname} = { options.services.${pname} = {
enable = enable =
@ -91,8 +109,7 @@
useNginx = lib.mkOption { useNginx = lib.mkOption {
type = lib.types.bool; type = lib.types.bool;
default = true; default = true;
description = description = "Whether to set up nginx reverse proxy";
"Whether to set up nginx reverse proxy";
}; };
port = lib.mkOption { port = lib.mkOption {
type = lib.types.int; type = lib.types.int;
@ -105,6 +122,16 @@
default = 7777; default = 7777;
description = "Port on which to connect to redis database."; description = "Port on which to connect to redis database.";
}; };
metricsPort = lib.mkOption {
type = lib.types.int;
default = 8091;
description = "Port on which server exposes metrics.";
};
enablePrometheus = lib.mkOption {
type = lib.types.bool;
default = false;
description = "Whether to add scrape rules to prometheus";
};
useHostTls = lib.mkOption { useHostTls = lib.mkOption {
type = lib.types.bool; type = lib.types.bool;
default = false; default = false;
@ -112,12 +139,10 @@
"Whether virtual host should enable NixOS ACME certs"; "Whether virtual host should enable NixOS ACME certs";
}; };
}; };
config = config = let
let
username = "${pname}"; username = "${pname}";
groupname = "${pname}"; groupname = "${pname}";
in in lib.mkIf cfg.enable {
lib.mkIf cfg.enable {
users.groups."${groupname}" = { }; users.groups."${groupname}" = { };
users.users."${username}" = { users.users."${username}" = {
isNormalUser = true; # needed to allow for home dir isNormalUser = true; # needed to allow for home dir
@ -131,8 +156,9 @@
startLimitBurst = 10; startLimitBurst = 10;
serviceConfig = { serviceConfig = {
ExecStart = let ExecStart = let
serveCliArg = serveCliArg = "--port ${toString cfg.port} --redisPort ${
"--port ${toString cfg.port} --redisPort ${toString cfg.redisPort}"; toString cfg.redisPort
} --metricsPort ${toString cfg.metricsPort}";
in "${packages.some-automoderation}/bin/${pname} ${serveCliArg}"; in "${packages.some-automoderation}/bin/${pname} ${serveCliArg}";
Restart = "on-failure"; Restart = "on-failure";
User = "${username}"; User = "${username}";
@ -157,10 +183,15 @@
enable = true; enable = true;
user = "${username}"; user = "${username}";
port = cfg.redisPort; port = cfg.redisPort;
settings = { settings = { notify-keyspace-events = "KEA"; };
notify-keyspace-events = "KEA"; };
} services.prometheus = lib.mkIf cfg.enablePrometheus {
; scrapeConfigs = [{
job_name = "some-automoderation";
static_configs = [{
targets = [ "localhost:${toString cfg.metricsPort}" ];
}];
}];
}; };
}; };
}; };