3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/ext/filters/client_channel/client_channel.h"
22 #include "src/core/ext/filters/client_channel/client_channel_channelz.h"
23 #include "src/core/lib/channel/channelz_registry.h"
24 #include "src/core/lib/gpr/useful.h"
25 #include "src/core/lib/surface/channel.h"
26 #include "src/core/lib/transport/connectivity_state.h"
28 #include <grpc/support/string_util.h>
33 SubchannelNode::SubchannelNode(const char* target_address,
34 size_t channel_tracer_max_nodes)
35 : BaseNode(EntityType::kSubchannel,
36 UniquePtr<char>(gpr_strdup(target_address))),
37 target_(UniquePtr<char>(gpr_strdup(target_address))),
38 trace_(channel_tracer_max_nodes) {}
40 SubchannelNode::~SubchannelNode() {}
42 void SubchannelNode::UpdateConnectivityState(grpc_connectivity_state state) {
43 connectivity_state_.Store(state, MemoryOrder::RELAXED);
46 void SubchannelNode::SetChildSocket(RefCountedPtr<SocketNode> socket) {
47 MutexLock lock(&socket_mu_);
48 child_socket_ = std::move(socket);
51 void SubchannelNode::PopulateConnectivityState(grpc_json* json) {
52 grpc_connectivity_state state =
53 connectivity_state_.Load(MemoryOrder::RELAXED);
54 json = grpc_json_create_child(nullptr, json, "state", nullptr,
55 GRPC_JSON_OBJECT, false);
56 grpc_json_create_child(nullptr, json, "state",
57 grpc_connectivity_state_name(state), GRPC_JSON_STRING,
61 grpc_json* SubchannelNode::RenderJson() {
62 grpc_json* top_level_json = grpc_json_create(GRPC_JSON_OBJECT);
63 grpc_json* json = top_level_json;
64 grpc_json* json_iterator = nullptr;
65 json_iterator = grpc_json_create_child(json_iterator, json, "ref", nullptr,
66 GRPC_JSON_OBJECT, false);
68 json_iterator = nullptr;
69 json_iterator = grpc_json_add_number_string_child(json, json_iterator,
70 "subchannelId", uuid());
71 // reset json iterators to top level object
72 json = top_level_json;
73 json_iterator = nullptr;
74 // create and fill the data child.
75 grpc_json* data = grpc_json_create_child(json_iterator, json, "data", nullptr,
76 GRPC_JSON_OBJECT, false);
78 json_iterator = nullptr;
79 PopulateConnectivityState(json);
80 GPR_ASSERT(target_.get() != nullptr);
81 grpc_json_create_child(nullptr, json, "target", target_.get(),
82 GRPC_JSON_STRING, false);
83 // fill in the channel trace if applicable
84 grpc_json* trace_json = trace_.RenderJson();
85 if (trace_json != nullptr) {
86 trace_json->key = "trace"; // this object is named trace in channelz.proto
87 grpc_json_link_child(json, trace_json, nullptr);
89 // ask CallCountingHelper to populate trace and call count data.
90 call_counter_.PopulateCallCounts(json);
91 json = top_level_json;
92 // populate the child socket.
93 RefCountedPtr<SocketNode> child_socket;
95 MutexLock lock(&socket_mu_);
96 child_socket = child_socket_;
98 if (child_socket != nullptr && child_socket->uuid() != 0) {
99 grpc_json* array_parent = grpc_json_create_child(
100 nullptr, json, "socketRef", nullptr, GRPC_JSON_ARRAY, false);
101 json_iterator = grpc_json_create_child(json_iterator, array_parent, nullptr,
102 nullptr, GRPC_JSON_OBJECT, false);
103 grpc_json* sibling_iterator = grpc_json_add_number_string_child(
104 json_iterator, nullptr, "socketId", child_socket->uuid());
105 grpc_json_create_child(sibling_iterator, json_iterator, "name",
106 child_socket->name(), GRPC_JSON_STRING, false);
108 return top_level_json;
111 } // namespace channelz
112 } // namespace grpc_core