Commit c4ae795c authored by zhujiashun's avatar zhujiashun

customize brpc metrics path

parent 29de815d
......@@ -32,6 +32,9 @@ DECLARE_int32(bvar_latency_p3);
namespace brpc {
DEFINE_string(prometheus_metrics_path, "/metrics", "The HTTP resource "
"path from which prometheus fetch metrics.");
// This is a class that convert bvar result to prometheus output.
// Currently the output only includes gauge and summary for two
// reasons:
......@@ -174,10 +177,10 @@ bool PrometheusMetricsDumper::DumpLatencyRecorderSuffix(
return true;
}
void PrometheusMetricsService::default_method(::google::protobuf::RpcController* cntl_base,
const ::brpc::MetricsRequest*,
::brpc::MetricsResponse*,
::google::protobuf::Closure* done) {
void PrometheusMetricsService::metrics(::google::protobuf::RpcController* cntl_base,
const ::brpc::MetricsRequest*,
::brpc::MetricsResponse*,
::google::protobuf::Closure* done) {
ClosureGuard done_guard(done);
Controller *cntl = static_cast<Controller*>(cntl_base);
cntl->http_response().set_content_type("text/plain");
......
......@@ -22,15 +22,15 @@
namespace brpc {
class PrometheusMetricsService : public brpc_vars_metrics {
class PrometheusMetricsService : public bvars {
public:
PrometheusMetricsService(Server* server)
: _server(server) {}
void default_method(::google::protobuf::RpcController* cntl_base,
const ::brpc::MetricsRequest* request,
::brpc::MetricsResponse* response,
::google::protobuf::Closure* done) override;
void metrics(::google::protobuf::RpcController* cntl_base,
const ::brpc::MetricsRequest* request,
::brpc::MetricsResponse* response,
::google::protobuf::Closure* done) override;
private:
Server* _server;
};
......
......@@ -97,8 +97,8 @@ service sockets {
rpc default_method(SocketsRequest) returns (SocketsResponse);
}
service brpc_vars_metrics {
rpc default_method(MetricsRequest) returns (MetricsResponse);
service bvars {
rpc metrics(MetricsRequest) returns (MetricsResponse);
}
service badmethod {
......
......@@ -110,6 +110,7 @@ DEFINE_bool(enable_threads_service, false, "Enable /threads");
DECLARE_int32(usercode_backup_threads);
DECLARE_bool(usercode_in_pthread);
DECLARE_string(prometheus_metrics_path);
const int INITIAL_SERVICE_CAP = 64;
const int INITIAL_CERT_MAP = 64;
......@@ -484,7 +485,10 @@ int Server::AddBuiltinServices() {
LOG(ERROR) << "Fail to add ListService";
return -1;
}
if (AddBuiltinService(new (std::nothrow) PrometheusMetricsService(this))) {
ServiceOptions options;
options.ownership = SERVER_OWNS_SERVICE;
options.restful_mappings = FLAGS_prometheus_metrics_path + " => metrics";
if (AddBuiltinService(new (std::nothrow) PrometheusMetricsService(this), options)) {
LOG(ERROR) << "Fail to add MetricsService";
return -1;
}
......@@ -1411,6 +1415,11 @@ int Server::AddService(google::protobuf::Service* service,
int Server::AddBuiltinService(google::protobuf::Service* service) {
ServiceOptions options;
options.ownership = SERVER_OWNS_SERVICE;
return AddBuiltinService(service, options);
}
int Server::AddBuiltinService(google::protobuf::Service* service,
const ServiceOptions& options) {
return AddServiceInternal(service, true, options);
}
......
......@@ -530,6 +530,8 @@ friend class Controller;
const ServiceOptions& options);
int AddBuiltinService(google::protobuf::Service* service);
int AddBuiltinService(google::protobuf::Service* service,
const ServiceOptions& options);
// Remove all methods of `service' from internal structures.
void RemoveMethodsOf(google::protobuf::Service* service);
......
......@@ -10,6 +10,10 @@
#include "butil/strings/string_piece.h"
#include "echo.pb.h"
namespace brpc {
DECLARE_string(prometheus_metrics_path);
} // brpc
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
......@@ -45,7 +49,7 @@ TEST(PrometheusMetrics, sanity) {
channel_opts.protocol = "http";
ASSERT_EQ(0, channel.Init("127.0.0.1:8614", &channel_opts));
brpc::Controller cntl;
cntl.http_request().uri() = "/brpc_vars_metrics";
cntl.http_request().uri() = brpc::FLAGS_prometheus_metrics_path;
channel.CallMethod(NULL, &cntl, NULL, NULL, NULL);
ASSERT_FALSE(cntl.Failed());
std::string res = cntl.response_attachment().to_string();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment