feat(monitoring): cloudflared metrics

This commit is contained in:
arcnmx 2024-06-24 12:11:07 -07:00
parent 1d19f0821d
commit ec7e322e2d
10 changed files with 124 additions and 29 deletions

View file

@ -1,38 +1,67 @@
{
let
tunnelModule = {pkgs, config, lib, ...}: let
inherit (lib.options) mkOption mkEnableOption;
settingsFormat = pkgs.formats.json {};
in {
options = with lib.types; {
extraArgs = mkOption {
type = listOf str;
default = [];
};
extraTunnel = {
enable =
mkEnableOption "extra tunnels"
// {
default = config.extraTunnel.ingress != {};
};
ingress = mkOption {
inherit (settingsFormat) type;
default = {};
};
};
};
};
in {
pkgs,
config,
utils,
gensokyo-zone,
lib,
...
}: let
inherit (lib.attrsets) mapAttrsToList mapAttrs' nameValuePair filterAttrsRecursive;
inherit (lib.lists) singleton;
inherit (lib.modules) mkIf mkMerge mkForce;
inherit (lib.options) mkOption mkEnableOption;
inherit (lib.options) mkOption;
cfg = config.services.cloudflared;
settingsFormat = pkgs.formats.json {};
in {
options.services.cloudflared = with lib.types; {
tunnels = let
tunnelModule = {config, ...}: {
options = {
extraTunnel = {
enable =
mkEnableOption "extra tunnels"
// {
default = config.extraTunnel.ingress != {};
};
ingress = mkOption {
inherit (settingsFormat) type;
default = {};
};
};
metricsPort = mkOption {
type = nullOr port;
default = null;
};
metricsBind = mkOption {
type = str;
default = "127.0.0.1";
};
extraArgs = mkOption {
type = listOf str;
default = [];
};
tunnels = mkOption {
type = attrsOf (submoduleWith {
modules = [tunnelModule];
shorthandOnlyDefinesConfig = true;
specialArgs = {
inherit pkgs utils gensokyo-zone;
};
};
in
mkOption {
type = attrsOf (submodule tunnelModule);
};
});
};
};
config.services.cloudflared = {
extraArgs = mkIf (cfg.metricsPort != null) [
"--metrics" "${cfg.metricsBind}:${toString cfg.metricsPort}"
];
};
config.systemd.services = let
filterConfig = filterAttrsRecursive (_: v: ! builtins.elem v [null [] {}]);
@ -44,29 +73,38 @@ in {
in
mkIf cfg.enable (mapAttrs' (uuid: tunnel: let
RuntimeDirectory = "cloudflared-tunnel-${uuid}";
configPath = "/run/${RuntimeDirectory}/config.yml";
settings = {
tunnel = uuid;
credentials-file = tunnel.credentialsFile;
warp-routing = filterConfig tunnel.warp-routing;
originRequest = filterConfig tunnel.originRequest;
ingress =
mapAttrsToList mapIngress tunnel.ingress
++ mapAttrsToList mapIngress tunnel.extraTunnel.ingress
++ singleton {service = tunnel.default;};
};
configPath =
if tunnel.extraTunnel.enable
then "/run/${RuntimeDirectory}/config.yml"
else pkgs.writeText "cloudflared.yml" (builtins.toJSON settings);
args = [
"--config=${configPath}"
"--no-autoupdate"
] ++ cfg.extraArgs ++ tunnel.extraArgs;
in
nameValuePair "cloudflared-tunnel-${uuid}" (mkMerge [
{
after = mkIf config.services.tailscale.enable ["tailscale-autoconnect.service"];
serviceConfig = {
RestartSec = 10;
ExecStart = mkForce [
"${cfg.package}/bin/cloudflared tunnel ${utils.escapeSystemdExecArgs args} run"
];
};
}
(mkIf tunnel.extraTunnel.enable {
serviceConfig = {
inherit RuntimeDirectory;
ExecStart = mkForce [
"${cfg.package}/bin/cloudflared tunnel --config=${configPath} --no-autoupdate run"
];
ExecStartPre = [
(pkgs.writeShellScript "cloudflared-tunnel-${uuid}-prepare" ''
${utils.genJqSecretsReplacementSnippet settings configPath}

View file

@ -0,0 +1,43 @@
{
lib,
gensokyo-zone,
...
}: let
inherit (gensokyo-zone.lib) mkAlmostOptionDefault;
inherit (lib.modules) mkIf;
in {
config.exports.services.cloudflared = {config, systemConfig, ...}: let
assertMetrics = nixosConfig: let
cfg = nixosConfig.services.cloudflared;
metricsPort =
if config.ports.metrics.enable
then config.ports.metrics.port
else null;
in {
assertion = metricsPort == cfg.metricsPort;
message = "metricsPort mismatch";
};
in {
displayName = mkAlmostOptionDefault "Cloudflare Tunnel/${systemConfig.name}";
nixos = {
serviceAttr = "cloudflared";
assertions = mkIf config.enable [
assertMetrics
];
};
defaults.port.listen = mkAlmostOptionDefault "lan";
ports = {
metrics = {
port = mkAlmostOptionDefault 3011;
protocol = "http";
status = {
enable = true;
gatus.http = {
statusCondition = mkAlmostOptionDefault "[STATUS] == 404";
};
};
prometheus.exporter.enable = mkAlmostOptionDefault true;
};
};
};
}

View file

@ -3,9 +3,8 @@
gensokyo-zone,
...
}: let
inherit (gensokyo-zone.lib) mapAlmostOptionDefaults mkAlmostOptionDefault;
inherit (gensokyo-zone.lib) mkAlmostOptionDefault;
inherit (lib.modules) mkIf;
inherit (lib.attrsets) mapAttrs;
inherit (lib.lists) all imap0;
inherit (lib.trivial) id;
in {