2021-02-19 12:51:26 +00:00
|
|
|
#include <Server/PrometheusRequestHandler.h>
|
2019-11-24 12:06:10 +00:00
|
|
|
|
|
|
|
#include <IO/HTTPCommon.h>
|
2021-02-19 12:51:26 +00:00
|
|
|
#include <Server/HTTP/WriteBufferFromHTTPServerResponse.h>
|
|
|
|
#include <Server/HTTPHandlerFactory.h>
|
|
|
|
#include <Server/IServer.h>
|
|
|
|
#include <Common/CurrentMetrics.h>
|
2019-11-24 12:06:10 +00:00
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/ProfileEvents.h>
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
#include <Poco/Util/LayeredConfiguration.h>
|
2019-11-24 12:06:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2021-02-19 12:51:26 +00:00
|
|
|
void PrometheusRequestHandler::handleRequest(HTTPServerRequest & request, HTTPServerResponse & response)
|
2019-11-24 12:06:10 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto & config = server.config();
|
|
|
|
unsigned keep_alive_timeout = config.getUInt("keep_alive_timeout", 10);
|
|
|
|
|
|
|
|
setResponseDefaultHeaders(response, keep_alive_timeout);
|
|
|
|
|
|
|
|
response.setContentType("text/plain; version=0.0.4; charset=UTF-8");
|
|
|
|
|
2021-02-20 05:28:47 +00:00
|
|
|
WriteBufferFromHTTPServerResponse wb(response, request.getMethod() == Poco::Net::HTTPRequest::HTTP_HEAD, keep_alive_timeout);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
metrics_writer.write(wb);
|
|
|
|
wb.finalize();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
wb.finalize();
|
|
|
|
}
|
2019-11-24 12:06:10 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException("PrometheusRequestHandler");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 12:51:26 +00:00
|
|
|
HTTPRequestHandlerFactoryPtr
|
|
|
|
createPrometheusHandlerFactory(IServer & server, AsynchronousMetrics & async_metrics, const std::string & config_prefix)
|
2020-06-12 10:04:42 +00:00
|
|
|
{
|
2021-02-19 12:51:26 +00:00
|
|
|
auto factory = std::make_shared<HandlingRuleHTTPHandlerFactory<PrometheusRequestHandler>>(
|
|
|
|
server, PrometheusMetricsWriter(server.config(), config_prefix + ".handler", async_metrics));
|
|
|
|
factory->addFiltersFromConfig(server.config(), config_prefix);
|
|
|
|
return factory;
|
2020-06-12 10:04:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 12:06:10 +00:00
|
|
|
}
|