blob: 07eb73d62692b4dc5cc3829cc5e6826703faf458 [file] [log] [blame]
ncteisenc3c6e062018-05-09 11:10:21 -07001/*
2 *
3 * Copyright 2018 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#ifndef GRPC_CORE_LIB_CHANNEL_CHANNELZ_H
20#define GRPC_CORE_LIB_CHANNEL_CHANNELZ_H
21
22#include <grpc/impl/codegen/port_platform.h>
23
24#include <grpc/grpc.h>
25
ncteisen23c50fd2018-05-18 16:38:29 -070026#include "src/core/lib/channel/channel_trace.h"
27#include "src/core/lib/gprpp/manual_constructor.h"
ncteisenc3c6e062018-05-09 11:10:21 -070028#include "src/core/lib/gprpp/ref_counted.h"
29#include "src/core/lib/gprpp/ref_counted_ptr.h"
30#include "src/core/lib/iomgr/error.h"
ncteisenc845ba62018-06-07 10:14:55 -070031#include "src/core/lib/iomgr/exec_ctx.h"
ncteisenc3c6e062018-05-09 11:10:21 -070032#include "src/core/lib/json/json.h"
33
ncteisenafb98292018-06-28 18:04:24 -070034// Channel arg key for client channel factory.
35#define GRPC_ARG_CHANNELZ_CHANNEL_NODE_CREATION_FUNC \
36 "grpc.channelz_channel_node_creation_func"
37
ncteisen97066fd2018-07-13 14:05:27 -070038// Channel arg key to signal that the channel is an internal channel.
39#define GRPC_ARG_CHANNELZ_CHANNEL_IS_INTERNAL_CHANNEL \
40 "grpc.channelz_channel_is_internal_channel"
41
ncteisenc3c6e062018-05-09 11:10:21 -070042namespace grpc_core {
43namespace channelz {
44
ncteisenc845ba62018-06-07 10:14:55 -070045namespace testing {
ncteisen6c987cb2018-06-08 09:30:12 -070046class ChannelNodePeer;
ncteisenc845ba62018-06-07 10:14:55 -070047}
48
ncteisen6c987cb2018-06-08 09:30:12 -070049class ChannelNode : public RefCounted<ChannelNode> {
ncteisenc3c6e062018-05-09 11:10:21 -070050 public:
ncteisencaa85b22018-07-03 11:25:41 -070051 static RefCountedPtr<ChannelNode> MakeChannelNode(
ncteisen5d373c42018-07-17 11:57:31 -070052 grpc_channel* channel, size_t channel_tracer_max_nodes,
53 bool is_top_level_channel);
ncteisenc3c6e062018-05-09 11:10:21 -070054
ncteisenc845ba62018-06-07 10:14:55 -070055 void RecordCallStarted();
56 void RecordCallFailed() {
ncteisena8717042018-06-05 08:26:34 -070057 gpr_atm_no_barrier_fetch_add(&calls_failed_, (gpr_atm(1)));
58 }
ncteisenc845ba62018-06-07 10:14:55 -070059 void RecordCallSucceeded() {
ncteisena8717042018-06-05 08:26:34 -070060 gpr_atm_no_barrier_fetch_add(&calls_succeeded_, (gpr_atm(1)));
61 }
ncteisenc3c6e062018-05-09 11:10:21 -070062
ncteisen97066fd2018-07-13 14:05:27 -070063 grpc_json* RenderJson();
64 char* RenderJsonString();
ncteisenc3c6e062018-05-09 11:10:21 -070065
ncteisen23003512018-06-29 12:00:15 -070066 // helper for getting and populating connectivity state. It is virtual
67 // because it allows the client_channel specific code to live in ext/
68 // instead of lib/
69 virtual void PopulateConnectivityState(grpc_json* json);
ncteisenafb98292018-06-28 18:04:24 -070070
ncteisen018498a2018-06-29 14:48:05 -070071 virtual void PopulateChildRefs(grpc_json* json);
72
ncteisenc845ba62018-06-07 10:14:55 -070073 ChannelTrace* trace() { return trace_.get(); }
ncteisen23c50fd2018-05-18 16:38:29 -070074
ncteisenc9c1fef2018-06-29 12:32:45 -070075 void MarkChannelDestroyed() {
ncteisend23739e2018-06-07 12:55:15 -070076 GPR_ASSERT(channel_ != nullptr);
77 channel_ = nullptr;
ncteisenc3c6e062018-05-09 11:10:21 -070078 }
79
ncteisenc9c1fef2018-06-29 12:32:45 -070080 bool ChannelIsDestroyed() { return channel_ == nullptr; }
ncteisen23c50fd2018-05-18 16:38:29 -070081
ncteisenc9c1fef2018-06-29 12:32:45 -070082 intptr_t channel_uuid() { return channel_uuid_; }
ncteisen97066fd2018-07-13 14:05:27 -070083 bool is_top_level_channel() { return is_top_level_channel_; }
ncteisenafb98292018-06-28 18:04:24 -070084
ncteisencaa85b22018-07-03 11:25:41 -070085 protected:
86 GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
87 GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW
ncteisen5d373c42018-07-17 11:57:31 -070088 ChannelNode(grpc_channel* channel, size_t channel_tracer_max_nodes,
89 bool is_top_level_channel);
ncteisencaa85b22018-07-03 11:25:41 -070090 virtual ~ChannelNode();
91
ncteisenc3c6e062018-05-09 11:10:21 -070092 private:
ncteisenc845ba62018-06-07 10:14:55 -070093 // testing peer friend.
ncteisen6c987cb2018-06-08 09:30:12 -070094 friend class testing::ChannelNodePeer;
ncteisena8717042018-06-05 08:26:34 -070095
ncteisenc7166ae2018-06-15 11:04:37 -040096 grpc_channel* channel_ = nullptr;
ncteisend23739e2018-06-07 12:55:15 -070097 UniquePtr<char> target_;
ncteisena8717042018-06-05 08:26:34 -070098 gpr_atm calls_started_ = 0;
99 gpr_atm calls_succeeded_ = 0;
100 gpr_atm calls_failed_ = 0;
ncteisenc7166ae2018-06-15 11:04:37 -0400101 gpr_atm last_call_started_millis_ = 0;
102 intptr_t channel_uuid_;
ncteisen97066fd2018-07-13 14:05:27 -0700103 bool is_top_level_channel_ = true;
ncteisen23c50fd2018-05-18 16:38:29 -0700104 ManualConstructor<ChannelTrace> trace_;
ncteisenc3c6e062018-05-09 11:10:21 -0700105};
106
ncteisen1e6c0b42018-07-13 11:22:08 -0700107// Placeholds channelz class for subchannels. All this can do now is track its
ncteisen0f6e4dd2018-07-17 11:26:55 -0700108// uuid (this information is needed by the parent channelz class).
109// TODO(ncteisen): build this out to support the GetSubchannel channelz request.
ncteisen1e6c0b42018-07-13 11:22:08 -0700110class SubchannelNode : public RefCounted<SubchannelNode> {
111 public:
112 SubchannelNode();
113 virtual ~SubchannelNode();
114
115 intptr_t subchannel_uuid() { return subchannel_uuid_; }
116
117 protected:
118 GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
119 GPRC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW
120
121 private:
122 intptr_t subchannel_uuid_;
123};
124
ncteisenafb98292018-06-28 18:04:24 -0700125// Creation functions
126
127typedef RefCountedPtr<ChannelNode> (*ChannelNodeCreationFunc)(grpc_channel*,
ncteisen5d373c42018-07-17 11:57:31 -0700128 size_t, bool);
ncteisenafb98292018-06-28 18:04:24 -0700129
ncteisenc3c6e062018-05-09 11:10:21 -0700130} // namespace channelz
131} // namespace grpc_core
132
133#endif /* GRPC_CORE_LIB_CHANNEL_CHANNELZ_H */