Commit 81c8e7f2 authored by zhujiashun's avatar zhujiashun

Make PrometheusMetricsService be global

parent 328772dc
......@@ -182,15 +182,15 @@ void PrometheusMetricsService::default_method(::google::protobuf::RpcController*
ClosureGuard done_guard(done);
Controller *cntl = static_cast<Controller*>(cntl_base);
cntl->http_response().set_content_type("text/plain");
if (DumpPrometheusMetricsToIOBuf(_server, &cntl->response_attachment()) != 0) {
if (DumpPrometheusMetricsToIOBuf(&cntl->response_attachment()) != 0) {
cntl->SetFailed("Fail to dump metrics");
return;
}
}
int DumpPrometheusMetricsToIOBuf(const Server* server, butil::IOBuf* output) {
int DumpPrometheusMetricsToIOBuf(butil::IOBuf* output) {
butil::IOBufBuilder os;
PrometheusMetricsDumper dumper(&os, brpc::ServerPrivateAccessor(server).ServerPrefix());
PrometheusMetricsDumper dumper(&os, brpc::ServerPrivateAccessor(NULL).Prefix());
const int ndump = bvar::Variable::dump_exposed(&dumper, NULL);
if (ndump < 0) {
return -1;
......
......@@ -24,19 +24,13 @@ namespace brpc {
class PrometheusMetricsService : public brpc_metrics {
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;
private:
Server* _server;
};
int DumpPrometheusMetricsToIOBuf(const Server* server, butil::IOBuf* output);
int DumpPrometheusMetricsToIOBuf(butil::IOBuf* output);
} // namepace brpc
......
......@@ -30,7 +30,6 @@ namespace brpc {
class ServerPrivateAccessor {
public:
explicit ServerPrivateAccessor(const Server* svr) {
CHECK(svr);
_server = svr;
}
......@@ -98,8 +97,8 @@ public:
RestfulMap* global_restful_map() const
{ return _server->_global_restful_map; }
std::string ServerPrefix() const { return _server->ServerPrefix(); }
std::string Prefix() const { return Server::Prefix(); }
private:
const Server* _server;
};
......
......@@ -265,8 +265,10 @@ static bvar::Vector<unsigned, 2> GetSessionLocalDataCount(void* arg) {
return v;
}
std::string Server::Prefix() { return "rpc_server"; }
std::string Server::ServerPrefix() const {
return butil::string_printf("rpc_server_%d", listen_address().port);
return butil::string_printf("%s_%d", Prefix().c_str(), listen_address().port);
}
void* Server::UpdateDerivedVars(void* arg) {
......@@ -484,7 +486,7 @@ int Server::AddBuiltinServices() {
LOG(ERROR) << "Fail to add ListService";
return -1;
}
if (AddBuiltinService(new (std::nothrow) PrometheusMetricsService(this))) {
if (AddBuiltinService(new (std::nothrow) PrometheusMetricsService)) {
LOG(ERROR) << "Fail to add MetricsService";
return -1;
}
......
......@@ -579,6 +579,7 @@ friend class Controller;
const ServiceProperty*
FindServicePropertyByName(const butil::StringPiece& name) const;
static std::string Prefix();
std::string ServerPrefix() const;
// Mapping from hostname to corresponding SSL_CTX
......
......@@ -40,6 +40,11 @@ TEST(PrometheusMetrics, sanity) {
ASSERT_EQ(0, server.AddService(&echo_svc, brpc::SERVER_DOESNT_OWN_SERVICE));
ASSERT_EQ(0, server.Start("127.0.0.1:8614", NULL));
brpc::Server server2;
DummyEchoServiceImpl echo_svc2;
ASSERT_EQ(0, server2.AddService(&echo_svc2, brpc::SERVER_DOESNT_OWN_SERVICE));
ASSERT_EQ(0, server2.Start("127.0.0.1:8615", NULL));
brpc::Channel channel;
brpc::ChannelOptions channel_opts;
channel_opts.protocol = "http";
......
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