1#pragma once
2
3#include "IServer.h"
4#include "PrometheusMetricsWriter.h"
5
6#include <Poco/Net/HTTPServerRequest.h>
7#include <Poco/Net/HTTPServerResponse.h>
8#include <Poco/Net/HTTPRequestHandler.h>
9#include <Poco/Net/HTTPRequestHandlerFactory.h>
10
11namespace DB
12{
13
14class PrometheusRequestHandler : public Poco::Net::HTTPRequestHandler
15{
16private:
17 IServer & server;
18 const PrometheusMetricsWriter & metrics_writer;
19
20public:
21 explicit PrometheusRequestHandler(IServer & server_, PrometheusMetricsWriter & metrics_writer_)
22 : server(server_)
23 , metrics_writer(metrics_writer_)
24 {
25 }
26
27 void handleRequest(
28 Poco::Net::HTTPServerRequest & request,
29 Poco::Net::HTTPServerResponse & response) override;
30};
31
32
33template <typename HandlerType>
34class PrometeusRequestHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory
35{
36private:
37 IServer & server;
38 std::string endpoint_path;
39 PrometheusMetricsWriter metrics_writer;
40
41public:
42 PrometeusRequestHandlerFactory(IServer & server_, const AsynchronousMetrics & async_metrics_)
43 : server(server_)
44 , endpoint_path(server_.config().getString("prometheus.endpoint", "/metrics"))
45 , metrics_writer(server_.config(), "prometheus", async_metrics_)
46 {
47 }
48
49 Poco::Net::HTTPRequestHandler * createRequestHandler(const Poco::Net::HTTPServerRequest & request) override
50 {
51 if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_GET
52 && request.getURI() == endpoint_path)
53 return new HandlerType(server, metrics_writer);
54
55 return nullptr;
56 }
57};
58
59using PrometeusHandlerFactory = PrometeusRequestHandlerFactory<PrometheusRequestHandler>;
60
61}
62