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
/routes/static/out.css
/nixos.qcow2
/data/

View File

@ -70,15 +70,33 @@
host = "some-automoderation.sunshine.industries";
useNginx = false;
port = 9090;
metricsPort = 9091;
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, ... }:
let
cfg = config.services.${pname};
let cfg = config.services.${pname};
in {
options.services.${pname} = {
enable =
@ -91,8 +109,7 @@
useNginx = lib.mkOption {
type = lib.types.bool;
default = true;
description =
"Whether to set up nginx reverse proxy";
description = "Whether to set up nginx reverse proxy";
};
port = lib.mkOption {
type = lib.types.int;
@ -105,6 +122,16 @@
default = 7777;
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 {
type = lib.types.bool;
default = false;
@ -112,12 +139,10 @@
"Whether virtual host should enable NixOS ACME certs";
};
};
config =
let
config = let
username = "${pname}";
groupname = "${pname}";
in
lib.mkIf cfg.enable {
in lib.mkIf cfg.enable {
users.groups."${groupname}" = { };
users.users."${username}" = {
isNormalUser = true; # needed to allow for home dir
@ -131,8 +156,9 @@
startLimitBurst = 10;
serviceConfig = {
ExecStart = let
serveCliArg =
"--port ${toString cfg.port} --redisPort ${toString cfg.redisPort}";
serveCliArg = "--port ${toString cfg.port} --redisPort ${
toString cfg.redisPort
} --metricsPort ${toString cfg.metricsPort}";
in "${packages.some-automoderation}/bin/${pname} ${serveCliArg}";
Restart = "on-failure";
User = "${username}";
@ -157,10 +183,15 @@
enable = true;
user = "${username}";
port = cfg.redisPort;
settings = {
notify-keyspace-events = "KEA";
}
;
settings = { notify-keyspace-events = "KEA"; };
};
services.prometheus = lib.mkIf cfg.enablePrometheus {
scrapeConfigs = [{
job_name = "some-automoderation";
static_configs = [{
targets = [ "localhost:${toString cfg.metricsPort}" ];
}];
}];
};
};
};