ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright 2017 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 | |
| 19 | #include <grpc/impl/codegen/port_platform.h> |
| 20 | |
| 21 | #include "src/core/lib/channel/channelz.h" |
| 22 | |
| 23 | #include <grpc/grpc.h> |
| 24 | #include <grpc/support/alloc.h> |
| 25 | #include <grpc/support/log.h> |
| 26 | #include <grpc/support/string_util.h> |
| 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include "src/core/lib/channel/channelz_registry.h" |
| 32 | #include "src/core/lib/channel/status_util.h" |
| 33 | #include "src/core/lib/gpr/string.h" |
| 34 | #include "src/core/lib/gpr/useful.h" |
| 35 | #include "src/core/lib/gprpp/memory.h" |
| 36 | #include "src/core/lib/iomgr/error.h" |
| 37 | #include "src/core/lib/slice/slice_internal.h" |
| 38 | #include "src/core/lib/surface/channel.h" |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 39 | #include "src/core/lib/transport/error_utils.h" |
| 40 | |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 41 | namespace grpc_core { |
| 42 | namespace channelz { |
| 43 | |
ncteisen | 5d373c4 | 2018-07-17 11:57:31 -0700 | [diff] [blame] | 44 | ChannelNode::ChannelNode(grpc_channel* channel, size_t channel_tracer_max_nodes, |
| 45 | bool is_top_level_channel) |
| 46 | : channel_(channel), |
| 47 | target_(nullptr), |
| 48 | channel_uuid_(-1), |
| 49 | is_top_level_channel_(is_top_level_channel) { |
ncteisen | 23c50fd | 2018-05-18 16:38:29 -0700 | [diff] [blame] | 50 | trace_.Init(channel_tracer_max_nodes); |
ncteisen | c7166ae | 2018-06-15 11:04:37 -0400 | [diff] [blame] | 51 | target_ = UniquePtr<char>(grpc_channel_get_target(channel_)); |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 52 | channel_uuid_ = ChannelzRegistry::RegisterChannelNode(this); |
Noah Eisen | ea424b8 | 2018-06-07 13:02:48 -0700 | [diff] [blame] | 53 | gpr_atm_no_barrier_store(&last_call_started_millis_, |
| 54 | (gpr_atm)ExecCtx::Get()->Now()); |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 55 | } |
| 56 | |
ncteisen | 6c987cb | 2018-06-08 09:30:12 -0700 | [diff] [blame] | 57 | ChannelNode::~ChannelNode() { |
ncteisen | 9a1bb05 | 2018-05-30 16:17:06 -0700 | [diff] [blame] | 58 | trace_.Destroy(); |
ncteisen | 9a6c722 | 2018-07-13 12:09:55 -0700 | [diff] [blame] | 59 | ChannelzRegistry::UnregisterChannelNode(channel_uuid_); |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 60 | } |
| 61 | |
ncteisen | 6c987cb | 2018-06-08 09:30:12 -0700 | [diff] [blame] | 62 | void ChannelNode::RecordCallStarted() { |
ncteisen | a871704 | 2018-06-05 08:26:34 -0700 | [diff] [blame] | 63 | gpr_atm_no_barrier_fetch_add(&calls_started_, (gpr_atm)1); |
Noah Eisen | ea424b8 | 2018-06-07 13:02:48 -0700 | [diff] [blame] | 64 | gpr_atm_no_barrier_store(&last_call_started_millis_, |
| 65 | (gpr_atm)ExecCtx::Get()->Now()); |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 66 | } |
| 67 | |
ncteisen | 2300351 | 2018-06-29 12:00:15 -0700 | [diff] [blame] | 68 | void ChannelNode::PopulateConnectivityState(grpc_json* json) {} |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 69 | |
ncteisen | 018498a | 2018-06-29 14:48:05 -0700 | [diff] [blame] | 70 | void ChannelNode::PopulateChildRefs(grpc_json* json) {} |
| 71 | |
ncteisen | 97066fd | 2018-07-13 14:05:27 -0700 | [diff] [blame] | 72 | grpc_json* ChannelNode::RenderJson() { |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 73 | // We need to track these three json objects to build our object |
| 74 | grpc_json* top_level_json = grpc_json_create(GRPC_JSON_OBJECT); |
| 75 | grpc_json* json = top_level_json; |
| 76 | grpc_json* json_iterator = nullptr; |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 77 | // create and fill the ref child |
| 78 | json_iterator = grpc_json_create_child(json_iterator, json, "ref", nullptr, |
ncteisen | 9a1bb05 | 2018-05-30 16:17:06 -0700 | [diff] [blame] | 79 | GRPC_JSON_OBJECT, false); |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 80 | json = json_iterator; |
| 81 | json_iterator = nullptr; |
ncteisen | 018498a | 2018-06-29 14:48:05 -0700 | [diff] [blame] | 82 | json_iterator = grpc_json_add_number_string_child(json, json_iterator, |
| 83 | "channelId", channel_uuid_); |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 84 | // reset json iterators to top level object |
| 85 | json = top_level_json; |
| 86 | json_iterator = nullptr; |
ncteisen | 9a1bb05 | 2018-05-30 16:17:06 -0700 | [diff] [blame] | 87 | // create and fill the data child. |
| 88 | grpc_json* data = grpc_json_create_child(json_iterator, json, "data", nullptr, |
| 89 | GRPC_JSON_OBJECT, false); |
ncteisen | 9a1bb05 | 2018-05-30 16:17:06 -0700 | [diff] [blame] | 90 | json = data; |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 91 | json_iterator = nullptr; |
ncteisen | 2300351 | 2018-06-29 12:00:15 -0700 | [diff] [blame] | 92 | PopulateConnectivityState(json); |
ncteisen | 97066fd | 2018-07-13 14:05:27 -0700 | [diff] [blame] | 93 | GPR_ASSERT(target_.get() != nullptr); |
Noah Eisen | ea424b8 | 2018-06-07 13:02:48 -0700 | [diff] [blame] | 94 | json_iterator = grpc_json_create_child( |
| 95 | json_iterator, json, "target", target_.get(), GRPC_JSON_STRING, false); |
ncteisen | 9a1bb05 | 2018-05-30 16:17:06 -0700 | [diff] [blame] | 96 | // fill in the channel trace if applicable |
ncteisen | 97066fd | 2018-07-13 14:05:27 -0700 | [diff] [blame] | 97 | grpc_json* trace = trace_->RenderJson(); |
ncteisen | 9a1bb05 | 2018-05-30 16:17:06 -0700 | [diff] [blame] | 98 | if (trace != nullptr) { |
ncteisen | 5d373c4 | 2018-07-17 11:57:31 -0700 | [diff] [blame] | 99 | // we manually link up and fill the child since it was created for us in |
ncteisen | 97066fd | 2018-07-13 14:05:27 -0700 | [diff] [blame] | 100 | // ChannelTrace::RenderJson |
ncteisen | 9a1bb05 | 2018-05-30 16:17:06 -0700 | [diff] [blame] | 101 | json_iterator = grpc_json_link_child(json, trace, json_iterator); |
| 102 | trace->parent = json; |
| 103 | trace->value = nullptr; |
| 104 | trace->key = "trace"; |
| 105 | trace->owns_value = false; |
| 106 | } |
ncteisen | 9a1bb05 | 2018-05-30 16:17:06 -0700 | [diff] [blame] | 107 | // reset the parent to be the data object. |
| 108 | json = data; |
| 109 | json_iterator = nullptr; |
ncteisen | 97066fd | 2018-07-13 14:05:27 -0700 | [diff] [blame] | 110 | if (calls_started_ != 0) { |
ncteisen | f87b955 | 2018-07-18 07:38:15 -0700 | [diff] [blame^] | 111 | json_iterator = grpc_json_add_number_string_child( |
| 112 | json, json_iterator, "callsStarted", calls_started_); |
ncteisen | 97066fd | 2018-07-13 14:05:27 -0700 | [diff] [blame] | 113 | } |
| 114 | if (calls_succeeded_ != 0) { |
ncteisen | f87b955 | 2018-07-18 07:38:15 -0700 | [diff] [blame^] | 115 | json_iterator = grpc_json_add_number_string_child( |
| 116 | json, json_iterator, "callsSucceeded", calls_succeeded_); |
ncteisen | 97066fd | 2018-07-13 14:05:27 -0700 | [diff] [blame] | 117 | } |
| 118 | if (calls_failed_) { |
ncteisen | f87b955 | 2018-07-18 07:38:15 -0700 | [diff] [blame^] | 119 | json_iterator = grpc_json_add_number_string_child( |
| 120 | json, json_iterator, "callsFailed", calls_failed_); |
ncteisen | 97066fd | 2018-07-13 14:05:27 -0700 | [diff] [blame] | 121 | } |
Noah Eisen | ea424b8 | 2018-06-07 13:02:48 -0700 | [diff] [blame] | 122 | gpr_timespec ts = |
| 123 | grpc_millis_to_timespec(last_call_started_millis_, GPR_CLOCK_REALTIME); |
| 124 | json_iterator = |
| 125 | grpc_json_create_child(json_iterator, json, "lastCallStartedTimestamp", |
ncteisen | 018498a | 2018-06-29 14:48:05 -0700 | [diff] [blame] | 126 | gpr_format_timespec(ts), GRPC_JSON_STRING, true); |
ncteisen | 018498a | 2018-06-29 14:48:05 -0700 | [diff] [blame] | 127 | json = top_level_json; |
| 128 | json_iterator = nullptr; |
| 129 | PopulateChildRefs(json); |
ncteisen | 97066fd | 2018-07-13 14:05:27 -0700 | [diff] [blame] | 130 | return top_level_json; |
| 131 | } |
| 132 | |
| 133 | char* ChannelNode::RenderJsonString() { |
| 134 | grpc_json* json = RenderJson(); |
| 135 | char* json_str = grpc_json_dump_to_string(json, 0); |
| 136 | grpc_json_destroy(json); |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 137 | return json_str; |
| 138 | } |
| 139 | |
ncteisen | caa85b2 | 2018-07-03 11:25:41 -0700 | [diff] [blame] | 140 | RefCountedPtr<ChannelNode> ChannelNode::MakeChannelNode( |
ncteisen | 5d373c4 | 2018-07-17 11:57:31 -0700 | [diff] [blame] | 141 | grpc_channel* channel, size_t channel_tracer_max_nodes, |
| 142 | bool is_top_level_channel) { |
ncteisen | afb9829 | 2018-06-28 18:04:24 -0700 | [diff] [blame] | 143 | return MakeRefCounted<grpc_core::channelz::ChannelNode>( |
ncteisen | 5d373c4 | 2018-07-17 11:57:31 -0700 | [diff] [blame] | 144 | channel, channel_tracer_max_nodes, is_top_level_channel); |
ncteisen | afb9829 | 2018-06-28 18:04:24 -0700 | [diff] [blame] | 145 | } |
| 146 | |
ncteisen | 0f6e4dd | 2018-07-17 11:26:55 -0700 | [diff] [blame] | 147 | SubchannelNode::SubchannelNode() { |
ncteisen | f87b955 | 2018-07-18 07:38:15 -0700 | [diff] [blame^] | 148 | subchannel_uuid_ = ChannelzRegistry::RegisterSubchannelNode(this); |
ncteisen | 1e6c0b4 | 2018-07-13 11:22:08 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | SubchannelNode::~SubchannelNode() { |
ncteisen | f87b955 | 2018-07-18 07:38:15 -0700 | [diff] [blame^] | 152 | ChannelzRegistry::UnregisterSubchannelNode(subchannel_uuid_); |
ncteisen | 1e6c0b4 | 2018-07-13 11:22:08 -0700 | [diff] [blame] | 153 | } |
| 154 | |
ncteisen | c3c6e06 | 2018-05-09 11:10:21 -0700 | [diff] [blame] | 155 | } // namespace channelz |
| 156 | } // namespace grpc_core |