2019-11-24 12:06:10 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "IServer.h"
|
2019-11-24 15:43:53 +00:00
|
|
|
#include "PrometheusMetricsWriter.h"
|
2019-11-24 12:06:10 +00:00
|
|
|
|
2019-11-24 15:43:53 +00:00
|
|
|
#include <Poco/Net/HTTPServerRequest.h>
|
|
|
|
#include <Poco/Net/HTTPServerResponse.h>
|
2019-11-24 12:06:10 +00:00
|
|
|
#include <Poco/Net/HTTPRequestHandler.h>
|
2019-11-24 15:43:53 +00:00
|
|
|
#include <Poco/Net/HTTPRequestHandlerFactory.h>
|
2019-11-24 12:06:10 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
class PrometheusRequestHandler : public Poco::Net::HTTPRequestHandler
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
IServer & server;
|
2019-11-24 15:43:53 +00:00
|
|
|
const PrometheusMetricsWriter & metrics_writer;
|
2019-11-24 12:06:10 +00:00
|
|
|
|
|
|
|
public:
|
2019-11-24 15:43:53 +00:00
|
|
|
explicit PrometheusRequestHandler(IServer & server_, PrometheusMetricsWriter & metrics_writer_)
|
|
|
|
: server(server_)
|
|
|
|
, metrics_writer(metrics_writer_)
|
2019-11-24 12:06:10 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleRequest(
|
|
|
|
Poco::Net::HTTPServerRequest & request,
|
|
|
|
Poco::Net::HTTPServerResponse & response) override;
|
|
|
|
};
|
|
|
|
|
2019-11-24 15:43:53 +00:00
|
|
|
|
|
|
|
template <typename HandlerType>
|
|
|
|
class PrometeusRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
IServer & server;
|
|
|
|
std::string endpoint_path;
|
|
|
|
PrometheusMetricsWriter metrics_writer;
|
|
|
|
|
|
|
|
public:
|
2019-11-26 20:27:24 +00:00
|
|
|
PrometeusRequestHandlerFactory(IServer & server_, const AsynchronousMetrics & async_metrics_)
|
2019-11-24 15:43:53 +00:00
|
|
|
: server(server_)
|
|
|
|
, endpoint_path(server_.config().getString("prometheus.endpoint", "/metrics"))
|
2019-11-26 20:27:24 +00:00
|
|
|
, metrics_writer(server_.config(), "prometheus", async_metrics_)
|
2019-11-24 15:43:53 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Poco::Net::HTTPRequestHandler * createRequestHandler(const Poco::Net::HTTPServerRequest & request) override
|
|
|
|
{
|
|
|
|
if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_GET
|
|
|
|
&& request.getURI() == endpoint_path)
|
|
|
|
return new HandlerType(server, metrics_writer);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
using PrometeusHandlerFactory = PrometeusRequestHandlerFactory<PrometheusRequestHandler>;
|
|
|
|
|
2019-11-24 12:06:10 +00:00
|
|
|
}
|