queue metrics: refactor the metrics into a struct

This commit is contained in:
Graham Christensen 2022-04-06 19:59:53 -04:00
parent 46f52b4c4e
commit 5de08d412e
3 changed files with 22 additions and 11 deletions

View file

@ -38,6 +38,18 @@ std::string getEnvOrDie(const std::string & key)
return *value; return *value;
} }
State::PromMetrics::PromMetrics()
: registry(std::make_shared<prometheus::Registry>())
, queue_checks_started(
prometheus::BuildCounter()
.Name("hydraqueuerunner_queue_checks_started_total")
.Help("Number of times State::getQueuedBuilds() was started")
.Register(*registry)
.Add({})
)
{
}
State::State(std::optional<std::string> metricsAddrOpt) State::State(std::optional<std::string> metricsAddrOpt)
: config(std::make_unique<HydraConfig>()) : config(std::make_unique<HydraConfig>())
@ -48,12 +60,6 @@ State::State(std::optional<std::string> metricsAddrOpt)
, uploadLogsToBinaryCache(config->getBoolOption("upload_logs_to_binary_cache", false)) , 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")))) , rootsDir(config->getStrOption("gc_roots_dir", fmt("%s/gcroots/per-user/%s/hydra-roots", settings.nixStateDir, getEnvOrDie("LOGNAME"))))
, metricsAddr(config->getStrOption("queue_runner_metrics_address", std::string{"127.0.0.1:9198"})) , metricsAddr(config->getStrOption("queue_runner_metrics_address", std::string{"127.0.0.1:9198"}))
, registry(std::make_shared<prometheus::Registry>())
, call_ctr(prometheus::BuildCounter()
.Name("queue_queued_builds_calls_total")
.Help("Number of times State::getQueuedBuilds() was called")
.Register(*registry))
, queue_queued_builds_calls(call_ctr.Add({})) // FIXME: add the proper arguments
{ {
hydraData = getEnvOrDie("HYDRA_DATA"); hydraData = getEnvOrDie("HYDRA_DATA");
@ -774,7 +780,7 @@ void State::run(BuildID buildOne)
prometheus::Exposer promExposer{metricsAddr}; prometheus::Exposer promExposer{metricsAddr};
auto exposerPort = promExposer.GetListeningPorts().front(); auto exposerPort = promExposer.GetListeningPorts().front();
promExposer.RegisterCollectable(registry); promExposer.RegisterCollectable(prom.registry);
std::cout << "Started the Prometheus exporter, listening on " std::cout << "Started the Prometheus exporter, listening on "
<< metricsAddr << "/metrics (port " << exposerPort << ")" << metricsAddr << "/metrics (port " << exposerPort << ")"

View file

@ -82,7 +82,7 @@ struct PreviousFailure : public std::exception {
bool State::getQueuedBuilds(Connection & conn, bool State::getQueuedBuilds(Connection & conn,
ref<Store> destStore, unsigned int & lastBuildId) ref<Store> destStore, unsigned int & lastBuildId)
{ {
queue_queued_builds_calls.Increment(); prom.queue_checks_started.Increment();
printInfo("checking the queue for builds > %d...", lastBuildId); printInfo("checking the queue for builds > %d...", lastBuildId);

View file

@ -437,10 +437,15 @@ private:
std::string metricsAddr; std::string metricsAddr;
std::shared_ptr<prometheus::Registry> registry; struct PromMetrics
{
std::shared_ptr<prometheus::Registry> registry;
prometheus::Family<prometheus::Counter>& call_ctr; prometheus::Counter& queue_checks_started;
prometheus::Counter& queue_queued_builds_calls;
PromMetrics();
};
PromMetrics prom;
public: public:
State(std::optional<std::string> metricsAddrOpt); State(std::optional<std::string> metricsAddrOpt);