From f07ac86aa75686cd985b85cfca4c702d10c06ffb Mon Sep 17 00:00:00 2001 From: "xiaodong.jia" Date: Thu, 11 Jun 2026 19:03:55 +0800 Subject: [PATCH] prometheus-xray-exporter: init at 0.2.0 --- .../monitoring/prometheus/exporters.nix | 1 + .../monitoring/prometheus/exporters/xray.nix | 77 +++++++++++++++++++ .../pr/prometheus-xray-exporter/package.nix | 28 +++++++ 3 files changed, 106 insertions(+) create mode 100644 nixos/modules/services/monitoring/prometheus/exporters/xray.nix create mode 100644 pkgs/by-name/pr/prometheus-xray-exporter/package.nix diff --git a/nixos/modules/services/monitoring/prometheus/exporters.nix b/nixos/modules/services/monitoring/prometheus/exporters.nix index b0a5b92ace6c..a625111fc2f0 100644 --- a/nixos/modules/services/monitoring/prometheus/exporters.nix +++ b/nixos/modules/services/monitoring/prometheus/exporters.nix @@ -133,6 +133,7 @@ let "v2ray" "varnish" "wireguard" + "xray" "zfs-siebenmann" "zfs" ] diff --git a/nixos/modules/services/monitoring/prometheus/exporters/xray.nix b/nixos/modules/services/monitoring/prometheus/exporters/xray.nix new file mode 100644 index 000000000000..b497c41f7ba3 --- /dev/null +++ b/nixos/modules/services/monitoring/prometheus/exporters/xray.nix @@ -0,0 +1,77 @@ +{ + config, + lib, + pkgs, + options, + ... +}: + +let + cfg = config.services.prometheus.exporters.xray; + inherit (lib) + mkOption + types + concatStringsSep + optionalString + ; +in +{ + port = 9550; + + extraOpts = { + xrayEndpoint = mkOption { + type = types.str; + default = "127.0.0.1:8080"; + description = '' + Xray gRPC API endpoint. + ''; + }; + + metricsPath = mkOption { + type = types.str; + default = "/scrape"; + description = '' + Path under which to expose metrics. + ''; + }; + + scrapeTimeout = mkOption { + type = types.int; + default = 5; + description = '' + Timeout in seconds for every individual scrape. + ''; + }; + + logPath = mkOption { + type = types.str; + default = "/var/log/xray/access.log"; + description = '' + Path to Xray access log file. Set to empty string to disable user metrics. + ''; + }; + + logTimeWindow = mkOption { + type = types.int; + default = 5; + description = '' + Time window in minutes for user metrics. + ''; + }; + }; + + serviceOpts = { + serviceConfig = { + ExecStart = '' + ${pkgs.prometheus-xray-exporter}/bin/xray-exporter \ + --listen ${cfg.listenAddress}:${toString cfg.port} \ + --metrics-path ${cfg.metricsPath} \ + --xray-endpoint ${cfg.xrayEndpoint} \ + --scrape-timeout ${toString cfg.scrapeTimeout} \ + ${optionalString (cfg.logPath != "") "--log-path ${cfg.logPath}"} \ + --log-time-window ${toString cfg.logTimeWindow} \ + ${concatStringsSep " \\\n " cfg.extraFlags} + ''; + }; + }; +} diff --git a/pkgs/by-name/pr/prometheus-xray-exporter/package.nix b/pkgs/by-name/pr/prometheus-xray-exporter/package.nix new file mode 100644 index 000000000000..7bf489f2ef12 --- /dev/null +++ b/pkgs/by-name/pr/prometheus-xray-exporter/package.nix @@ -0,0 +1,28 @@ +{ + buildGoModule, + lib, + fetchFromGitHub, +}: + +buildGoModule (finalAttrs: { + __structuredAttrs = true; + pname = "prometheus-xray-exporter"; + version = "0.2.0"; + + src = fetchFromGitHub { + owner = "compassvpn"; + repo = "xray-exporter"; + tag = "v${finalAttrs.version}"; + hash = "sha256-GP0CFphMgSS8ezSuRoHQJMuehCND4glrXE9fye0PhTE="; + }; + + vendorHash = "sha256-yRxy44SnEFa7yOJyiOgFTk+Z4s5HOJ4cMjcf8VTTfQk="; + + meta = { + description = "Prometheus exporter for Xray-core metrics"; + mainProgram = "xray-exporter"; + homepage = "https://github.com/compassvpn/xray-exporter"; + license = lib.licenses.mit; + maintainers = with lib.maintainers; [ podocarp ]; + }; +})