From b86a74475189e0ed97cdd3ddc48d96e94cec366a Mon Sep 17 00:00:00 2001 From: ncteisen Date: Wed, 11 Jul 2018 17:11:01 -0700 Subject: Expose GetChannel and GetTopChannels Core API --- grpc.def | 2 ++ include/grpc/grpc.h | 23 +++++++++++++++++++++++ src/core/lib/channel/channelz_registry.cc | 21 +++++++++++++++++++++ src/ruby/ext/grpc/rb_grpc_imports.generated.c | 4 ++++ src/ruby/ext/grpc/rb_grpc_imports.generated.h | 6 ++++++ test/core/channel/channelz_test.cc | 10 ++++++++++ test/core/surface/public_headers_must_be_c89.c | 2 ++ test/cpp/util/channel_trace_proto_helper.cc | 5 +++++ test/cpp/util/channel_trace_proto_helper.h | 1 + 9 files changed, 74 insertions(+) diff --git a/grpc.def b/grpc.def index 06db74cad5..5b98792662 100644 --- a/grpc.def +++ b/grpc.def @@ -69,6 +69,8 @@ EXPORTS grpc_resource_quota_unref grpc_resource_quota_resize grpc_resource_quota_arg_vtable + grpc_channelz_get_top_channels + grpc_channelz_get_channel grpc_insecure_channel_create_from_fd grpc_server_add_insecure_channel_from_fd grpc_use_signal diff --git a/include/grpc/grpc.h b/include/grpc/grpc.h index c129a66949..f0eb2c0121 100644 --- a/include/grpc/grpc.h +++ b/include/grpc/grpc.h @@ -454,6 +454,29 @@ GRPCAPI void grpc_resource_quota_resize(grpc_resource_quota* resource_quota, */ GRPCAPI const grpc_arg_pointer_vtable* grpc_resource_quota_arg_vtable(void); +/************* CHANNELZ API *************/ +/** Channelz is under active development. The following APIs will see some + churn as the feature is implemented. This comment will be removed once + channelz is officially supported, and these APIs become stable. For now + you may track the progress by following this github issue: + https://github.com/grpc/grpc/issues/15340 + + the following APIs return allocated JSON strings that match the response + objects from the channelz proto, found here: + https://github.com/grpc/grpc/blob/master/src/proto/grpc/channelz/channelz.proto. + + For easy conversion to protobuf, The JSON is formatted according to: + https://developers.google.com/protocol-buffers/docs/proto3#json. */ + +/* Gets all root channels (i.e. channels the application has directly + created). This does not include subchannels nor non-top level channels. + The returned string is allocated and must be freed by the application. */ +GRPCAPI char* grpc_channelz_get_top_channels(intptr_t start_channel_id); + +/* Returns a single Channel, or else a NOT_FOUND code. The returned string + is allocated and must be freed by the application. */ +GRPCAPI char* grpc_channelz_get_channel(intptr_t channel_id); + #ifdef __cplusplus } #endif diff --git a/src/core/lib/channel/channelz_registry.cc b/src/core/lib/channel/channelz_registry.cc index a16798d524..38496b3d78 100644 --- a/src/core/lib/channel/channelz_registry.cc +++ b/src/core/lib/channel/channelz_registry.cc @@ -121,3 +121,24 @@ char* ChannelzRegistry::InternalGetTopChannels(intptr_t start_channel_id) { } // namespace channelz } // namespace grpc_core + +char* grpc_channelz_get_top_channels(intptr_t start_channel_id) { + return grpc_core::channelz::ChannelzRegistry::GetTopChannels( + start_channel_id); +} + +char* grpc_channelz_get_channel(intptr_t channel_id) { + grpc_core::channelz::ChannelNode* channel_node = + grpc_core::channelz::ChannelzRegistry::GetChannelNode(channel_id); + if (channel_node == nullptr) { + return nullptr; + } + grpc_json* top_level_json = grpc_json_create(GRPC_JSON_OBJECT); + grpc_json* json = top_level_json; + grpc_json* channel_json = channel_node->RenderJson(); + channel_json->key = "channel"; + grpc_json_link_child(json, channel_json, nullptr); + char* json_str = grpc_json_dump_to_string(top_level_json, 0); + grpc_json_destroy(top_level_json); + return json_str; +} diff --git a/src/ruby/ext/grpc/rb_grpc_imports.generated.c b/src/ruby/ext/grpc/rb_grpc_imports.generated.c index 4e235121e2..2443532bb8 100644 --- a/src/ruby/ext/grpc/rb_grpc_imports.generated.c +++ b/src/ruby/ext/grpc/rb_grpc_imports.generated.c @@ -92,6 +92,8 @@ grpc_resource_quota_ref_type grpc_resource_quota_ref_import; grpc_resource_quota_unref_type grpc_resource_quota_unref_import; grpc_resource_quota_resize_type grpc_resource_quota_resize_import; grpc_resource_quota_arg_vtable_type grpc_resource_quota_arg_vtable_import; +grpc_channelz_get_top_channels_type grpc_channelz_get_top_channels_import; +grpc_channelz_get_channel_type grpc_channelz_get_channel_import; grpc_insecure_channel_create_from_fd_type grpc_insecure_channel_create_from_fd_import; grpc_server_add_insecure_channel_from_fd_type grpc_server_add_insecure_channel_from_fd_import; grpc_use_signal_type grpc_use_signal_import; @@ -340,6 +342,8 @@ void grpc_rb_load_imports(HMODULE library) { grpc_resource_quota_unref_import = (grpc_resource_quota_unref_type) GetProcAddress(library, "grpc_resource_quota_unref"); grpc_resource_quota_resize_import = (grpc_resource_quota_resize_type) GetProcAddress(library, "grpc_resource_quota_resize"); grpc_resource_quota_arg_vtable_import = (grpc_resource_quota_arg_vtable_type) GetProcAddress(library, "grpc_resource_quota_arg_vtable"); + grpc_channelz_get_top_channels_import = (grpc_channelz_get_top_channels_type) GetProcAddress(library, "grpc_channelz_get_top_channels"); + grpc_channelz_get_channel_import = (grpc_channelz_get_channel_type) GetProcAddress(library, "grpc_channelz_get_channel"); grpc_insecure_channel_create_from_fd_import = (grpc_insecure_channel_create_from_fd_type) GetProcAddress(library, "grpc_insecure_channel_create_from_fd"); grpc_server_add_insecure_channel_from_fd_import = (grpc_server_add_insecure_channel_from_fd_type) GetProcAddress(library, "grpc_server_add_insecure_channel_from_fd"); grpc_use_signal_import = (grpc_use_signal_type) GetProcAddress(library, "grpc_use_signal"); diff --git a/src/ruby/ext/grpc/rb_grpc_imports.generated.h b/src/ruby/ext/grpc/rb_grpc_imports.generated.h index f01c9c8248..b08a1f94f7 100644 --- a/src/ruby/ext/grpc/rb_grpc_imports.generated.h +++ b/src/ruby/ext/grpc/rb_grpc_imports.generated.h @@ -251,6 +251,12 @@ extern grpc_resource_quota_resize_type grpc_resource_quota_resize_import; typedef const grpc_arg_pointer_vtable*(*grpc_resource_quota_arg_vtable_type)(void); extern grpc_resource_quota_arg_vtable_type grpc_resource_quota_arg_vtable_import; #define grpc_resource_quota_arg_vtable grpc_resource_quota_arg_vtable_import +typedef char*(*grpc_channelz_get_top_channels_type)(intptr_t start_channel_id); +extern grpc_channelz_get_top_channels_type grpc_channelz_get_top_channels_import; +#define grpc_channelz_get_top_channels grpc_channelz_get_top_channels_import +typedef char*(*grpc_channelz_get_channel_type)(intptr_t channel_id); +extern grpc_channelz_get_channel_type grpc_channelz_get_channel_import; +#define grpc_channelz_get_channel grpc_channelz_get_channel_import typedef grpc_channel*(*grpc_insecure_channel_create_from_fd_type)(const char* target, int fd, const grpc_channel_args* args); extern grpc_insecure_channel_create_from_fd_type grpc_insecure_channel_create_from_fd_import; #define grpc_insecure_channel_create_from_fd grpc_insecure_channel_create_from_fd_import diff --git a/test/core/channel/channelz_test.cc b/test/core/channel/channelz_test.cc index d12f529726..ad5f86d934 100644 --- a/test/core/channel/channelz_test.cc +++ b/test/core/channel/channelz_test.cc @@ -95,6 +95,11 @@ void ValidateGetTopChannels(size_t expected_channels) { EXPECT_EQ(end->type, GRPC_JSON_TRUE); grpc_json_destroy(parsed_json); gpr_free(json_str); + // also check that the core API formats this correctly + char* core_api_json_str = grpc_channelz_get_top_channels(0); + grpc::testing::ValidateGetTopChannelsResponseProtoJsonTranslation( + core_api_json_str); + gpr_free(core_api_json_str); } class ChannelFixture { @@ -151,6 +156,11 @@ void ValidateChannel(ChannelNode* channel, validate_channel_data_args args) { grpc::testing::ValidateChannelProtoJsonTranslation(json_str); ValidateCounters(json_str, args); gpr_free(json_str); + // also check that the core API formats this the correct way + char* core_api_json_str = grpc_channelz_get_channel(channel->channel_uuid()); + grpc::testing::ValidateGetChannelResponseProtoJsonTranslation( + core_api_json_str); + gpr_free(core_api_json_str); } grpc_millis GetLastCallStartedMillis(ChannelNode* channel) { diff --git a/test/core/surface/public_headers_must_be_c89.c b/test/core/surface/public_headers_must_be_c89.c index 9a79b468dd..9f4ad2b4d7 100644 --- a/test/core/surface/public_headers_must_be_c89.c +++ b/test/core/surface/public_headers_must_be_c89.c @@ -131,6 +131,8 @@ int main(int argc, char **argv) { printf("%lx", (unsigned long) grpc_resource_quota_unref); printf("%lx", (unsigned long) grpc_resource_quota_resize); printf("%lx", (unsigned long) grpc_resource_quota_arg_vtable); + printf("%lx", (unsigned long) grpc_channelz_get_top_channels); + printf("%lx", (unsigned long) grpc_channelz_get_channel); printf("%lx", (unsigned long) grpc_auth_property_iterator_next); printf("%lx", (unsigned long) grpc_auth_context_property_iterator); printf("%lx", (unsigned long) grpc_auth_context_peer_identity); diff --git a/test/cpp/util/channel_trace_proto_helper.cc b/test/cpp/util/channel_trace_proto_helper.cc index 137f278640..b4704bfe6a 100644 --- a/test/cpp/util/channel_trace_proto_helper.cc +++ b/test/cpp/util/channel_trace_proto_helper.cc @@ -77,5 +77,10 @@ void ValidateGetTopChannelsResponseProtoJsonTranslation(char* json_c_str) { json_c_str); } +void ValidateGetChannelResponseProtoJsonTranslation(char* json_c_str) { + VaidateProtoJsonTranslation( + json_c_str); +} + } // namespace testing } // namespace grpc diff --git a/test/cpp/util/channel_trace_proto_helper.h b/test/cpp/util/channel_trace_proto_helper.h index 74c15f04f1..18e3d54b6b 100644 --- a/test/cpp/util/channel_trace_proto_helper.h +++ b/test/cpp/util/channel_trace_proto_helper.h @@ -25,6 +25,7 @@ namespace testing { void ValidateChannelTraceProtoJsonTranslation(char* json_c_str); void ValidateChannelProtoJsonTranslation(char* json_c_str); void ValidateGetTopChannelsResponseProtoJsonTranslation(char* json_c_str); +void ValidateGetChannelResponseProtoJsonTranslation(char* json_c_str); } // namespace testing } // namespace grpc -- cgit v1.2.3