channelz_sampler.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <grpc/support/port_platform.h>
  19. #include <unistd.h>
  20. #include <cstdlib>
  21. #include <fstream>
  22. #include <iostream>
  23. #include <memory>
  24. #include <ostream>
  25. #include <queue>
  26. #include <string>
  27. #include "absl/flags/flag.h"
  28. #include "absl/strings/str_format.h"
  29. #include "absl/strings/str_join.h"
  30. #include "google/protobuf/text_format.h"
  31. #include <grpc/grpc.h>
  32. #include <grpcpp/channel.h>
  33. #include <grpcpp/client_context.h>
  34. #include <grpcpp/create_channel.h>
  35. #include <grpcpp/ext/channelz_service_plugin.h>
  36. #include <grpcpp/grpcpp.h>
  37. #include <grpcpp/security/credentials.h>
  38. #include <grpcpp/security/server_credentials.h>
  39. #include <grpcpp/server.h>
  40. #include <grpcpp/server_builder.h>
  41. #include <grpcpp/server_context.h>
  42. #include "src/core/lib/json/json.h"
  43. #include "src/cpp/server/channelz/channelz_service.h"
  44. #include "src/proto/grpc/channelz/channelz.pb.h"
  45. #include "test/core/util/test_config.h"
  46. #include "test/cpp/util/test_config.h"
  47. #include "test/cpp/util/test_credentials_provider.h"
  48. ABSL_FLAG(std::string, server_address, "", "channelz server address");
  49. ABSL_FLAG(std::string, custom_credentials_type, "", "custom credentials type");
  50. ABSL_FLAG(int64_t, sampling_times, 1, "number of sampling");
  51. // TODO(Capstan): Consider using absl::Duration
  52. ABSL_FLAG(int64_t, sampling_interval_seconds, 0,
  53. "sampling interval in seconds");
  54. ABSL_FLAG(std::string, output_json, "", "output filename in json format");
  55. namespace {
  56. using grpc::ClientContext;
  57. using grpc::Status;
  58. using grpc::StatusCode;
  59. using grpc::channelz::v1::GetChannelRequest;
  60. using grpc::channelz::v1::GetChannelResponse;
  61. using grpc::channelz::v1::GetServersRequest;
  62. using grpc::channelz::v1::GetServersResponse;
  63. using grpc::channelz::v1::GetSocketRequest;
  64. using grpc::channelz::v1::GetSocketResponse;
  65. using grpc::channelz::v1::GetSubchannelRequest;
  66. using grpc::channelz::v1::GetSubchannelResponse;
  67. using grpc::channelz::v1::GetTopChannelsRequest;
  68. using grpc::channelz::v1::GetTopChannelsResponse;
  69. } // namespace
  70. class ChannelzSampler final {
  71. public:
  72. // Get server_id of a server
  73. int64_t GetServerID(const grpc::channelz::v1::Server& server) {
  74. return server.ref().server_id();
  75. }
  76. // Get channel_id of a channel
  77. inline int64_t GetChannelID(const grpc::channelz::v1::Channel& channel) {
  78. return channel.ref().channel_id();
  79. }
  80. // Get subchannel_id of a subchannel
  81. inline int64_t GetSubchannelID(
  82. const grpc::channelz::v1::Subchannel& subchannel) {
  83. return subchannel.ref().subchannel_id();
  84. }
  85. // Get socket_id of a socket
  86. inline int64_t GetSocketID(const grpc::channelz::v1::Socket& socket) {
  87. return socket.ref().socket_id();
  88. }
  89. // Get name of a server
  90. inline std::string GetServerName(const grpc::channelz::v1::Server& server) {
  91. return server.ref().name();
  92. }
  93. // Get name of a channel
  94. inline std::string GetChannelName(
  95. const grpc::channelz::v1::Channel& channel) {
  96. return channel.ref().name();
  97. }
  98. // Get name of a subchannel
  99. inline std::string GetSubchannelName(
  100. const grpc::channelz::v1::Subchannel& subchannel) {
  101. return subchannel.ref().name();
  102. }
  103. // Get name of a socket
  104. inline std::string GetSocketName(const grpc::channelz::v1::Socket& socket) {
  105. return socket.ref().name();
  106. }
  107. // Get a channel based on channel_id
  108. grpc::channelz::v1::Channel GetChannelRPC(int64_t channel_id) {
  109. GetChannelRequest get_channel_request;
  110. get_channel_request.set_channel_id(channel_id);
  111. GetChannelResponse get_channel_response;
  112. ClientContext get_channel_context;
  113. get_channel_context.set_deadline(
  114. grpc_timeout_seconds_to_deadline(rpc_timeout_seconds_));
  115. Status status = channelz_stub_->GetChannel(
  116. &get_channel_context, get_channel_request, &get_channel_response);
  117. if (!status.ok()) {
  118. gpr_log(GPR_ERROR, "GetChannelRPC failed: %s",
  119. get_channel_context.debug_error_string().c_str());
  120. GPR_ASSERT(0);
  121. }
  122. return get_channel_response.channel();
  123. }
  124. // Get a subchannel based on subchannel_id
  125. grpc::channelz::v1::Subchannel GetSubchannelRPC(int64_t subchannel_id) {
  126. GetSubchannelRequest get_subchannel_request;
  127. get_subchannel_request.set_subchannel_id(subchannel_id);
  128. GetSubchannelResponse get_subchannel_response;
  129. ClientContext get_subchannel_context;
  130. get_subchannel_context.set_deadline(
  131. grpc_timeout_seconds_to_deadline(rpc_timeout_seconds_));
  132. Status status = channelz_stub_->GetSubchannel(&get_subchannel_context,
  133. get_subchannel_request,
  134. &get_subchannel_response);
  135. if (!status.ok()) {
  136. gpr_log(GPR_ERROR, "GetSubchannelRPC failed: %s",
  137. get_subchannel_context.debug_error_string().c_str());
  138. GPR_ASSERT(0);
  139. }
  140. return get_subchannel_response.subchannel();
  141. }
  142. // get a socket based on socket_id
  143. grpc::channelz::v1::Socket GetSocketRPC(int64_t socket_id) {
  144. GetSocketRequest get_socket_request;
  145. get_socket_request.set_socket_id(socket_id);
  146. GetSocketResponse get_socket_response;
  147. ClientContext get_socket_context;
  148. get_socket_context.set_deadline(
  149. grpc_timeout_seconds_to_deadline(rpc_timeout_seconds_));
  150. Status status = channelz_stub_->GetSocket(
  151. &get_socket_context, get_socket_request, &get_socket_response);
  152. if (!status.ok()) {
  153. gpr_log(GPR_ERROR, "GetSocketRPC failed: %s",
  154. get_socket_context.debug_error_string().c_str());
  155. GPR_ASSERT(0);
  156. }
  157. return get_socket_response.socket();
  158. }
  159. // get the descedent channels/subchannels/sockets of a channel
  160. // push descedent channels/subchannels to queue for layer traverse
  161. // store descedent channels/subchannels/sockets for dumping data
  162. void GetChannelDescedence(
  163. const grpc::channelz::v1::Channel& channel,
  164. std::queue<grpc::channelz::v1::Channel>& channel_queue,
  165. std::queue<grpc::channelz::v1::Subchannel>& subchannel_queue) {
  166. std::cout << " Channel ID" << GetChannelID(channel) << "_"
  167. << GetChannelName(channel) << " descendence - ";
  168. if (channel.channel_ref_size() > 0 || channel.subchannel_ref_size() > 0) {
  169. if (channel.channel_ref_size() > 0) {
  170. std::cout << "channel: ";
  171. for (const auto& _channelref : channel.channel_ref()) {
  172. int64_t ch_id = _channelref.channel_id();
  173. std::cout << "ID" << ch_id << "_" << _channelref.name() << " ";
  174. grpc::channelz::v1::Channel ch = GetChannelRPC(ch_id);
  175. channel_queue.push(ch);
  176. if (CheckID(ch_id)) {
  177. all_channels_.push_back(ch);
  178. StoreChannelInJson(ch);
  179. }
  180. }
  181. if (channel.subchannel_ref_size() > 0) {
  182. std::cout << ", ";
  183. }
  184. }
  185. if (channel.subchannel_ref_size() > 0) {
  186. std::cout << "subchannel: ";
  187. for (const auto& _subchannelref : channel.subchannel_ref()) {
  188. int64_t subch_id = _subchannelref.subchannel_id();
  189. std::cout << "ID" << subch_id << "_" << _subchannelref.name() << " ";
  190. grpc::channelz::v1::Subchannel subch = GetSubchannelRPC(subch_id);
  191. subchannel_queue.push(subch);
  192. if (CheckID(subch_id)) {
  193. all_subchannels_.push_back(subch);
  194. StoreSubchannelInJson(subch);
  195. }
  196. }
  197. }
  198. } else if (channel.socket_ref_size() > 0) {
  199. std::cout << "socket: ";
  200. for (const auto& _socketref : channel.socket_ref()) {
  201. int64_t so_id = _socketref.socket_id();
  202. std::cout << "ID" << so_id << "_" << _socketref.name() << " ";
  203. grpc::channelz::v1::Socket so = GetSocketRPC(so_id);
  204. if (CheckID(so_id)) {
  205. all_sockets_.push_back(so);
  206. StoreSocketInJson(so);
  207. }
  208. }
  209. }
  210. std::cout << std::endl;
  211. }
  212. // get the descedent channels/subchannels/sockets of a subchannel
  213. // push descedent channels/subchannels to queue for layer traverse
  214. // store descedent channels/subchannels/sockets for dumping data
  215. void GetSubchannelDescedence(
  216. grpc::channelz::v1::Subchannel& subchannel,
  217. std::queue<grpc::channelz::v1::Channel>& channel_queue,
  218. std::queue<grpc::channelz::v1::Subchannel>& subchannel_queue) {
  219. std::cout << " Subchannel ID" << GetSubchannelID(subchannel) << "_"
  220. << GetSubchannelName(subchannel) << " descendence - ";
  221. if (subchannel.channel_ref_size() > 0 ||
  222. subchannel.subchannel_ref_size() > 0) {
  223. if (subchannel.channel_ref_size() > 0) {
  224. std::cout << "channel: ";
  225. for (const auto& _channelref : subchannel.channel_ref()) {
  226. int64_t ch_id = _channelref.channel_id();
  227. std::cout << "ID" << ch_id << "_" << _channelref.name() << " ";
  228. grpc::channelz::v1::Channel ch = GetChannelRPC(ch_id);
  229. channel_queue.push(ch);
  230. if (CheckID(ch_id)) {
  231. all_channels_.push_back(ch);
  232. StoreChannelInJson(ch);
  233. }
  234. }
  235. if (subchannel.subchannel_ref_size() > 0) {
  236. std::cout << ", ";
  237. }
  238. }
  239. if (subchannel.subchannel_ref_size() > 0) {
  240. std::cout << "subchannel: ";
  241. for (const auto& _subchannelref : subchannel.subchannel_ref()) {
  242. int64_t subch_id = _subchannelref.subchannel_id();
  243. std::cout << "ID" << subch_id << "_" << _subchannelref.name() << " ";
  244. grpc::channelz::v1::Subchannel subch = GetSubchannelRPC(subch_id);
  245. subchannel_queue.push(subch);
  246. if (CheckID(subch_id)) {
  247. all_subchannels_.push_back(subch);
  248. StoreSubchannelInJson(subch);
  249. }
  250. }
  251. }
  252. } else if (subchannel.socket_ref_size() > 0) {
  253. std::cout << "socket: ";
  254. for (const auto& _socketref : subchannel.socket_ref()) {
  255. int64_t so_id = _socketref.socket_id();
  256. std::cout << "ID" << so_id << "_" << _socketref.name() << " ";
  257. grpc::channelz::v1::Socket so = GetSocketRPC(so_id);
  258. if (CheckID(so_id)) {
  259. all_sockets_.push_back(so);
  260. StoreSocketInJson(so);
  261. }
  262. }
  263. }
  264. std::cout << std::endl;
  265. }
  266. // Set up the channelz sampler client
  267. // Initialize json as an array
  268. void Setup(const std::string& custom_credentials_type,
  269. const std::string& server_address) {
  270. json_ = grpc_core::Json::Array();
  271. rpc_timeout_seconds_ = 20;
  272. grpc::ChannelArguments channel_args;
  273. std::shared_ptr<grpc::ChannelCredentials> channel_creds =
  274. grpc::testing::GetCredentialsProvider()->GetChannelCredentials(
  275. custom_credentials_type, &channel_args);
  276. if (!channel_creds) {
  277. gpr_log(GPR_ERROR,
  278. "Wrong user credential type: %s. Allowed credential types: "
  279. "INSECURE_CREDENTIALS, ssl, alts, google_default_credentials.",
  280. custom_credentials_type.c_str());
  281. GPR_ASSERT(0);
  282. }
  283. std::shared_ptr<grpc::Channel> channel =
  284. CreateChannel(server_address, channel_creds);
  285. channelz_stub_ = grpc::channelz::v1::Channelz::NewStub(channel);
  286. }
  287. // Get all servers, keep querying until getting all
  288. // Store servers for dumping data
  289. // Need to check id repeating for servers
  290. void GetServersRPC() {
  291. int64_t server_start_id = 0;
  292. while (true) {
  293. GetServersRequest get_servers_request;
  294. GetServersResponse get_servers_response;
  295. ClientContext get_servers_context;
  296. get_servers_context.set_deadline(
  297. grpc_timeout_seconds_to_deadline(rpc_timeout_seconds_));
  298. get_servers_request.set_start_server_id(server_start_id);
  299. Status status = channelz_stub_->GetServers(
  300. &get_servers_context, get_servers_request, &get_servers_response);
  301. if (!status.ok()) {
  302. if (status.error_code() == StatusCode::UNIMPLEMENTED) {
  303. gpr_log(GPR_ERROR,
  304. "Error status UNIMPLEMENTED. Please check and make sure "
  305. "channelz has been registered on the server being queried.");
  306. } else {
  307. gpr_log(GPR_ERROR,
  308. "GetServers RPC with GetServersRequest.server_start_id=%d, "
  309. "failed: %s",
  310. int(server_start_id),
  311. get_servers_context.debug_error_string().c_str());
  312. }
  313. GPR_ASSERT(0);
  314. }
  315. for (const auto& _server : get_servers_response.server()) {
  316. all_servers_.push_back(_server);
  317. StoreServerInJson(_server);
  318. }
  319. if (!get_servers_response.end()) {
  320. server_start_id = GetServerID(all_servers_.back()) + 1;
  321. } else {
  322. break;
  323. }
  324. }
  325. std::cout << "Number of servers = " << all_servers_.size() << std::endl;
  326. }
  327. // Get sockets that belongs to servers
  328. // Store sockets for dumping data
  329. void GetSocketsOfServers() {
  330. for (const auto& _server : all_servers_) {
  331. std::cout << "Server ID" << GetServerID(_server) << "_"
  332. << GetServerName(_server) << " listen_socket - ";
  333. for (const auto& _socket : _server.listen_socket()) {
  334. int64_t so_id = _socket.socket_id();
  335. std::cout << "ID" << so_id << "_" << _socket.name() << " ";
  336. if (CheckID(so_id)) {
  337. grpc::channelz::v1::Socket so = GetSocketRPC(so_id);
  338. all_sockets_.push_back(so);
  339. StoreSocketInJson(so);
  340. }
  341. }
  342. std::cout << std::endl;
  343. }
  344. }
  345. // Get all top channels, keep querying until getting all
  346. // Store channels for dumping data
  347. // No need to check id repeating for top channels
  348. void GetTopChannelsRPC() {
  349. int64_t channel_start_id = 0;
  350. while (true) {
  351. GetTopChannelsRequest get_top_channels_request;
  352. GetTopChannelsResponse get_top_channels_response;
  353. ClientContext get_top_channels_context;
  354. get_top_channels_context.set_deadline(
  355. grpc_timeout_seconds_to_deadline(rpc_timeout_seconds_));
  356. get_top_channels_request.set_start_channel_id(channel_start_id);
  357. Status status = channelz_stub_->GetTopChannels(
  358. &get_top_channels_context, get_top_channels_request,
  359. &get_top_channels_response);
  360. if (!status.ok()) {
  361. gpr_log(GPR_ERROR,
  362. "GetTopChannels RPC with "
  363. "GetTopChannelsRequest.channel_start_id=%d failed: %s",
  364. int(channel_start_id),
  365. get_top_channels_context.debug_error_string().c_str());
  366. GPR_ASSERT(0);
  367. }
  368. for (const auto& _topchannel : get_top_channels_response.channel()) {
  369. top_channels_.push_back(_topchannel);
  370. all_channels_.push_back(_topchannel);
  371. StoreChannelInJson(_topchannel);
  372. }
  373. if (!get_top_channels_response.end()) {
  374. channel_start_id = GetChannelID(top_channels_.back()) + 1;
  375. } else {
  376. break;
  377. }
  378. }
  379. std::cout << std::endl
  380. << "Number of top channels = " << top_channels_.size()
  381. << std::endl;
  382. }
  383. // layer traverse for each top channel
  384. void TraverseTopChannels() {
  385. for (const auto& _topchannel : top_channels_) {
  386. int tree_depth = 0;
  387. std::queue<grpc::channelz::v1::Channel> channel_queue;
  388. std::queue<grpc::channelz::v1::Subchannel> subchannel_queue;
  389. std::cout << "Tree depth = " << tree_depth << std::endl;
  390. GetChannelDescedence(_topchannel, channel_queue, subchannel_queue);
  391. while (!channel_queue.empty() || !subchannel_queue.empty()) {
  392. ++tree_depth;
  393. std::cout << "Tree depth = " << tree_depth << std::endl;
  394. int ch_q_size = channel_queue.size();
  395. int subch_q_size = subchannel_queue.size();
  396. for (int i = 0; i < ch_q_size; ++i) {
  397. grpc::channelz::v1::Channel ch = channel_queue.front();
  398. channel_queue.pop();
  399. GetChannelDescedence(ch, channel_queue, subchannel_queue);
  400. }
  401. for (int i = 0; i < subch_q_size; ++i) {
  402. grpc::channelz::v1::Subchannel subch = subchannel_queue.front();
  403. subchannel_queue.pop();
  404. GetSubchannelDescedence(subch, channel_queue, subchannel_queue);
  405. }
  406. }
  407. std::cout << std::endl;
  408. }
  409. }
  410. // dump data of all entities to stdout
  411. void DumpStdout() {
  412. std::string data_str;
  413. for (const auto& _channel : all_channels_) {
  414. std::cout << "channel ID" << GetChannelID(_channel) << "_"
  415. << GetChannelName(_channel) << " data:" << std::endl;
  416. // TODO(mohanli): TextFormat::PrintToString records time as seconds and
  417. // nanos. Need a more human readable way.
  418. ::google::protobuf::TextFormat::PrintToString(_channel.data(), &data_str);
  419. printf("%s\n", data_str.c_str());
  420. }
  421. for (const auto& _subchannel : all_subchannels_) {
  422. std::cout << "subchannel ID" << GetSubchannelID(_subchannel) << "_"
  423. << GetSubchannelName(_subchannel) << " data:" << std::endl;
  424. ::google::protobuf::TextFormat::PrintToString(_subchannel.data(),
  425. &data_str);
  426. printf("%s\n", data_str.c_str());
  427. }
  428. for (const auto& _server : all_servers_) {
  429. std::cout << "server ID" << GetServerID(_server) << "_"
  430. << GetServerName(_server) << " data:" << std::endl;
  431. ::google::protobuf::TextFormat::PrintToString(_server.data(), &data_str);
  432. printf("%s\n", data_str.c_str());
  433. }
  434. for (const auto& _socket : all_sockets_) {
  435. std::cout << "socket ID" << GetSocketID(_socket) << "_"
  436. << GetSocketName(_socket) << " data:" << std::endl;
  437. ::google::protobuf::TextFormat::PrintToString(_socket.data(), &data_str);
  438. printf("%s\n", data_str.c_str());
  439. }
  440. }
  441. // Store a channel in Json
  442. void StoreChannelInJson(const grpc::channelz::v1::Channel& channel) {
  443. std::string id = grpc::to_string(GetChannelID(channel));
  444. std::string type = "Channel";
  445. std::string description;
  446. ::google::protobuf::TextFormat::PrintToString(channel.data(), &description);
  447. grpc_core::Json description_json = grpc_core::Json(description);
  448. StoreEntityInJson(id, type, description_json);
  449. }
  450. // Store a subchannel in Json
  451. void StoreSubchannelInJson(const grpc::channelz::v1::Subchannel& subchannel) {
  452. std::string id = grpc::to_string(GetSubchannelID(subchannel));
  453. std::string type = "Subchannel";
  454. std::string description;
  455. ::google::protobuf::TextFormat::PrintToString(subchannel.data(),
  456. &description);
  457. grpc_core::Json description_json = grpc_core::Json(description);
  458. StoreEntityInJson(id, type, description_json);
  459. }
  460. // Store a server in Json
  461. void StoreServerInJson(const grpc::channelz::v1::Server& server) {
  462. std::string id = grpc::to_string(GetServerID(server));
  463. std::string type = "Server";
  464. std::string description;
  465. ::google::protobuf::TextFormat::PrintToString(server.data(), &description);
  466. grpc_core::Json description_json = grpc_core::Json(description);
  467. StoreEntityInJson(id, type, description_json);
  468. }
  469. // Store a socket in Json
  470. void StoreSocketInJson(const grpc::channelz::v1::Socket& socket) {
  471. std::string id = grpc::to_string(GetSocketID(socket));
  472. std::string type = "Socket";
  473. std::string description;
  474. ::google::protobuf::TextFormat::PrintToString(socket.data(), &description);
  475. grpc_core::Json description_json = grpc_core::Json(description);
  476. StoreEntityInJson(id, type, description_json);
  477. }
  478. // Store an entity in Json
  479. void StoreEntityInJson(std::string& id, std::string& type,
  480. const grpc_core::Json& description) {
  481. std::string start, finish;
  482. gpr_timespec ago = gpr_time_sub(
  483. now_,
  484. gpr_time_from_seconds(absl::GetFlag(FLAGS_sampling_interval_seconds),
  485. GPR_TIMESPAN));
  486. std::stringstream ss;
  487. const time_t time_now = now_.tv_sec;
  488. ss << std::put_time(std::localtime(&time_now), "%F %T");
  489. finish = ss.str(); // example: "2019-02-01 12:12:18"
  490. ss.str("");
  491. const time_t time_ago = ago.tv_sec;
  492. ss << std::put_time(std::localtime(&time_ago), "%F %T");
  493. start = ss.str();
  494. grpc_core::Json obj =
  495. grpc_core::Json::Object{{"Task", absl::StrFormat("%s_ID%s", type, id)},
  496. {"Start", start},
  497. {"Finish", finish},
  498. {"ID", id},
  499. {"Type", type},
  500. {"Description", description}};
  501. json_.mutable_array()->push_back(obj);
  502. }
  503. // Dump data in json
  504. std::string DumpJson() { return json_.Dump(); }
  505. // Check if one entity has been recorded
  506. bool CheckID(int64_t id) {
  507. if (id_set_.count(id) == 0) {
  508. id_set_.insert(id);
  509. return true;
  510. } else {
  511. return false;
  512. }
  513. }
  514. // Record current time
  515. void RecordNow() { now_ = gpr_now(GPR_CLOCK_REALTIME); }
  516. private:
  517. std::unique_ptr<grpc::channelz::v1::Channelz::Stub> channelz_stub_;
  518. std::vector<grpc::channelz::v1::Channel> top_channels_;
  519. std::vector<grpc::channelz::v1::Server> all_servers_;
  520. std::vector<grpc::channelz::v1::Channel> all_channels_;
  521. std::vector<grpc::channelz::v1::Subchannel> all_subchannels_;
  522. std::vector<grpc::channelz::v1::Socket> all_sockets_;
  523. std::unordered_set<int64_t> id_set_;
  524. grpc_core::Json json_;
  525. int64_t rpc_timeout_seconds_;
  526. gpr_timespec now_;
  527. };
  528. int main(int argc, char** argv) {
  529. grpc::testing::TestEnvironment env(argc, argv);
  530. grpc::testing::InitTest(&argc, &argv, true);
  531. std::ofstream output_file(absl::GetFlag(FLAGS_output_json));
  532. for (int i = 0; i < absl::GetFlag(FLAGS_sampling_times); ++i) {
  533. ChannelzSampler channelz_sampler;
  534. channelz_sampler.Setup(absl::GetFlag(FLAGS_custom_credentials_type),
  535. absl::GetFlag(FLAGS_server_address));
  536. std::cout << "Wait for sampling interval "
  537. << absl::GetFlag(FLAGS_sampling_interval_seconds) << "s..."
  538. << std::endl;
  539. const gpr_timespec kDelay = gpr_time_add(
  540. gpr_now(GPR_CLOCK_MONOTONIC),
  541. gpr_time_from_seconds(absl::GetFlag(FLAGS_sampling_interval_seconds),
  542. GPR_TIMESPAN));
  543. gpr_sleep_until(kDelay);
  544. std::cout << "##### " << i << "th sampling #####" << std::endl;
  545. channelz_sampler.RecordNow();
  546. channelz_sampler.GetServersRPC();
  547. channelz_sampler.GetSocketsOfServers();
  548. channelz_sampler.GetTopChannelsRPC();
  549. channelz_sampler.TraverseTopChannels();
  550. channelz_sampler.DumpStdout();
  551. if (!absl::GetFlag(FLAGS_output_json).empty()) {
  552. output_file << channelz_sampler.DumpJson() << "\n" << std::flush;
  553. }
  554. }
  555. output_file.close();
  556. return 0;
  557. }