Add async metrics to Prometheus output

This commit is contained in:
VDimir 2019-11-26 23:27:24 +03:00
parent eadc58bbd6
commit 3c4220017c
6 changed files with 43 additions and 16 deletions

View File

@ -34,10 +34,10 @@ public:
Poco::Net::HTTPRequestHandler * createRequestHandler(const Poco::Net::HTTPServerRequest & request) override;
template <typename T>
TThis * addHandler()
template <typename T, typename... TArgs>
TThis * addHandler(TArgs &&... args)
{
child_handler_factories.emplace_back(std::make_unique<T>(server));
child_handler_factories.emplace_back(std::make_unique<T>(server, std::forward<TArgs>(args)...));
return this;
}
};

View File

@ -27,9 +27,12 @@ namespace DB
{
PrometheusMetricsWriter::PrometheusMetricsWriter(
const Poco::Util::AbstractConfiguration & config, const std::string & config_name)
: send_events(config.getBool(config_name + ".events", true))
const Poco::Util::AbstractConfiguration & config, const std::string & config_name,
const AsynchronousMetrics & async_metrics_)
: async_metrics(async_metrics_)
, send_events(config.getBool(config_name + ".events", true))
, send_metrics(config.getBool(config_name + ".metrics", true))
, send_asynchronous_metrics(config.getBool(config_name + ".asynchronous_metrics", true))
{
}
@ -68,6 +71,20 @@ void PrometheusMetricsWriter::write(WriteBuffer & wb) const
writeOutLine(wb, key, value);
}
}
if (send_asynchronous_metrics)
{
auto async_metrics_values = async_metrics.getValues();
for (const auto & name_value : async_metrics_values)
{
std::string key{asynchronous_metrics_prefix + name_value.first};
auto value = name_value.second;
// TODO: add HELP section? asynchronous_metrics contains only key and value
writeOutLine(wb, "# TYPE", key, "gauge");
writeOutLine(wb, key, value);
}
}
}
}

View File

@ -2,7 +2,10 @@
#include <string>
#include <Interpreters/AsynchronousMetrics.h>
#include <IO/WriteBuffer.h>
#include <Poco/Util/AbstractConfiguration.h>
namespace DB
@ -13,16 +16,21 @@ class PrometheusMetricsWriter
{
public:
PrometheusMetricsWriter(
const Poco::Util::AbstractConfiguration & config, const std::string & config_name);
const Poco::Util::AbstractConfiguration & config, const std::string & config_name,
const AsynchronousMetrics & async_metrics_);
void write(WriteBuffer & wb) const;
private:
const AsynchronousMetrics & async_metrics;
const bool send_events;
const bool send_metrics;
const bool send_asynchronous_metrics;
static inline constexpr auto profile_events_prefix = "ClickHouse_ProfileEvents_";
static inline constexpr auto current_metrics_prefix = "ClickHouse_Metrics_";
static inline constexpr auto profile_events_prefix = "clickhouse_profile_events_";
static inline constexpr auto current_metrics_prefix = "clickhouse_metrics_";
static inline constexpr auto asynchronous_metrics_prefix = "clickhouse_async_metrics_";
};
}

View File

@ -39,10 +39,10 @@ private:
PrometheusMetricsWriter metrics_writer;
public:
PrometeusRequestHandlerFactory(IServer & server_)
PrometeusRequestHandlerFactory(IServer & server_, const AsynchronousMetrics & async_metrics_)
: server(server_)
, endpoint_path(server_.config().getString("prometheus.endpoint", "/metrics"))
, metrics_writer(server_.config(), "prometheus")
, metrics_writer(server_.config(), "prometheus", async_metrics_)
{
}

View File

@ -668,6 +668,10 @@ int Server::main(const std::vector<std::string> & /*args*/)
return address;
};
/// This object will periodically calculate some metrics.
AsynchronousMetrics async_metrics(*global_context);
attachSystemTablesAsync(*global_context->getDatabase("system"), async_metrics);
for (const auto & listen_host : listen_hosts)
{
auto create_server = [&](const char * port_name, auto && func)
@ -709,7 +713,7 @@ int Server::main(const std::vector<std::string> & /*args*/)
socket.setSendTimeout(settings.http_send_timeout);
auto handlerFactory = createDefaultHandlerFatory<HTTPHandler>(*this, "HTTPHandler-factory");
if (config().has("prometheus") && config().getInt("prometheus.port", 0) == 0)
handlerFactory->addHandler<PrometeusHandlerFactory>();
handlerFactory->addHandler<PrometeusHandlerFactory>(async_metrics);
servers.emplace_back(std::make_unique<Poco::Net::HTTPServer>(
handlerFactory,
@ -840,7 +844,7 @@ int Server::main(const std::vector<std::string> & /*args*/)
socket.setReceiveTimeout(settings.http_receive_timeout);
socket.setSendTimeout(settings.http_send_timeout);
auto handlerFactory = new HTTPRequestHandlerFactoryMain(*this, "PrometheusHandler-factory");
handlerFactory->addHandler<PrometeusHandlerFactory>();
handlerFactory->addHandler<PrometeusHandlerFactory>(async_metrics);
servers.emplace_back(std::make_unique<Poco::Net::HTTPServer>(
handlerFactory,
server_pool,
@ -951,10 +955,6 @@ int Server::main(const std::vector<std::string> & /*args*/)
throw;
}
/// This object will periodically calculate some metrics.
AsynchronousMetrics async_metrics(*global_context);
attachSystemTablesAsync(*global_context->getDatabase("system"), async_metrics);
std::vector<std::unique_ptr<MetricsTransmitter>> metrics_transmitters;
for (const auto & graphite_key : DB::getMultipleKeysFromConfig(config(), "", "graphite"))
{

View File

@ -295,6 +295,7 @@
port - port to setup server. If not defined or 0 than http_port used
metrics - send data from table system.metrics
events - send data from table system.events
asynchronous_metrics - send data from table system.asynchronous_metrics
-->
<prometheus>
<endpoint>/metrics</endpoint>
@ -302,6 +303,7 @@
<metrics>true</metrics>
<events>true</events>
<asynchronous_metrics>true</asynchronous_metrics>
</prometheus>
<!-- Query log. Used only for queries with setting log_queries = 1. -->