From edf3c348f2156827a43639c8acac7051b87dec98 Mon Sep 17 00:00:00 2001 From: Cole Helbling Date: Wed, 6 Apr 2022 10:58:57 -0700 Subject: [PATCH] hydra-queue-runner: make entire address configurable --- src/hydra-queue-runner/hydra-queue-runner.cc | 31 +++++++------------- src/hydra-queue-runner/state.hh | 4 +-- t/Hydra/Config/include.t | 2 +- t/lib/HydraTestContext.pm | 2 +- 4 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/hydra-queue-runner/hydra-queue-runner.cc b/src/hydra-queue-runner/hydra-queue-runner.cc index 6a84749f..5ad1a9d9 100644 --- a/src/hydra-queue-runner/hydra-queue-runner.cc +++ b/src/hydra-queue-runner/hydra-queue-runner.cc @@ -39,7 +39,7 @@ std::string getEnvOrDie(const std::string & key) } -State::State(std::optional metricsPortOpt) +State::State(std::optional metricsAddrOpt) : config(std::make_unique()) , maxUnsupportedTime(config->getIntOption("max_unsupported_time", 0)) , dbPool(config->getIntOption("max_db_connections", 128)) @@ -47,15 +47,15 @@ State::State(std::optional metricsPortOpt) , maxLogSize(config->getIntOption("max_log_size", 64ULL << 20)) , uploadLogsToBinaryCache(config->getBoolOption("upload_logs_to_binary_cache", false)) , rootsDir(config->getStrOption("gc_roots_dir", fmt("%s/gcroots/per-user/%s/hydra-roots", settings.nixStateDir, getEnvOrDie("LOGNAME")))) - , metricsPort(config->getIntOption("queue_runner_metrics_port", 9198)) + , metricsAddr(config->getStrOption("queue_runner_metrics_address", std::string{"127.0.0.1:9198"})) , registry(std::make_shared()) { hydraData = getEnvOrDie("HYDRA_DATA"); logDir = canonPath(hydraData + "/build-logs"); - if (metricsPortOpt.has_value()) { - metricsPort = metricsPortOpt.value(); + if (metricsAddrOpt.has_value()) { + metricsAddr = metricsAddrOpt.value(); } /* handle deprecated store specification */ @@ -762,16 +762,15 @@ void State::run(BuildID buildOne) if (!lock) throw Error("hydra-queue-runner is already running"); - std::cout << "Starting the Prometheus exporter on port " << metricsPort << std::endl; + std::cout << "Starting the Prometheus exporter on " << metricsAddr << std::endl; /* Set up simple exporter, to show that we're still alive. */ - std::string metricsAddress{"127.0.0.1"}; // FIXME: configurable - prometheus::Exposer promExposer{metricsAddress + ":" + std::to_string(metricsPort)}; + prometheus::Exposer promExposer{metricsAddr}; auto exposerPort = promExposer.GetListeningPorts().front(); promExposer.RegisterCollectable(registry); std::cout << "Started the Prometheus exporter, listening on " - << "http://" << metricsAddress << ":" << exposerPort << "/metrics" + << metricsAddr << "/metrics (port " << exposerPort << ")" << std::endl; Store::Params localParams; @@ -884,7 +883,7 @@ int main(int argc, char * * argv) bool unlock = false; bool status = false; BuildID buildOne = 0; - std::optional metricsPortOpt = std::nullopt; + std::optional metricsAddrOpt = std::nullopt; parseCmdLine(argc, argv, [&](Strings::iterator & arg, const Strings::iterator & end) { if (*arg == "--unlock") @@ -896,16 +895,8 @@ int main(int argc, char * * argv) buildOne = *b; else throw Error("‘--build-one’ requires a build ID"); - } else if (*arg == "--port") { - if (auto p = string2Int(getArg(*arg, arg, end))) { - if (*p > std::numeric_limits::max()) { - throw Error("'--port' has a maximum of 65535"); - } else { - metricsPortOpt = *p; - } - } else { - throw Error("'--port' requires a numeric port (0 for a random, usable port; max 65535)"); - } + } else if (*arg == "--prometheus-address") { + metricsAddrOpt = getArg(*arg, arg, end); } else return false; return true; @@ -914,7 +905,7 @@ int main(int argc, char * * argv) settings.verboseBuild = true; settings.lockCPU = false; - State state{metricsPortOpt}; + State state{metricsAddrOpt}; if (status) state.showStatus(); else if (unlock) diff --git a/src/hydra-queue-runner/state.hh b/src/hydra-queue-runner/state.hh index fb533559..a37548a3 100644 --- a/src/hydra-queue-runner/state.hh +++ b/src/hydra-queue-runner/state.hh @@ -434,12 +434,12 @@ private: via gc_roots_dir. */ nix::Path rootsDir; - uint16_t metricsPort; + std::string metricsAddr;; std::shared_ptr registry; public: - State(std::optional metricsPortOpt); + State(std::optional metricsAddrOpt); private: diff --git a/t/Hydra/Config/include.t b/t/Hydra/Config/include.t index 63186f87..14f657ff 100644 --- a/t/Hydra/Config/include.t +++ b/t/Hydra/Config/include.t @@ -20,7 +20,7 @@ write_file($ctx{'tmpdir'} . "/bar.conf", q| |); is(getHydraConfig(), { - queue_runner_metrics_port => 0, + queue_runner_metrics_address => "127.0.0.1:0", foo => { bar => "baz" } }, "Nested includes work."); diff --git a/t/lib/HydraTestContext.pm b/t/lib/HydraTestContext.pm index ce933c09..2bb1478c 100644 --- a/t/lib/HydraTestContext.pm +++ b/t/lib/HydraTestContext.pm @@ -51,7 +51,7 @@ sub new { $ENV{'HYDRA_CONFIG'} = "$dir/hydra.conf"; my $hydra_config = $opts{'hydra_config'} || ""; - $hydra_config = "queue_runner_metrics_port = 0\n" . $hydra_config; + $hydra_config = "queue_runner_metrics_address = 127.0.0.1:0\n" . $hydra_config; if ($opts{'use_external_destination_store'} // 1) { $hydra_config = "store_uri = file:$dir/nix/dest-store\n" . $hydra_config; }