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
19 #include "src/core/channelz/channelz.h"
20
21 #include <grpc/impl/channel_arg_names.h>
22 #include <grpc/status.h>
23
24 #include <string>
25
26 #include "gmock/gmock.h"
27 #include "gtest/gtest.h"
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/surface/channel.h"
30 #include "src/core/server/server.h"
31 #include "src/core/util/time.h"
32 #include "test/core/end2end/end2end_tests.h"
33
34 using testing::HasSubstr;
35 using testing::Not;
36
37 namespace grpc_core {
38 namespace {
39
RunOneRequest(CoreEnd2endTest & test,bool request_is_success)40 void RunOneRequest(CoreEnd2endTest& test, bool request_is_success) {
41 auto c = test.NewClientCall("/foo").Timeout(Duration::Seconds(5)).Create();
42 IncomingMetadata server_initial_metadata;
43 IncomingStatusOnClient server_status;
44 c.NewBatch(1)
45 .SendInitialMetadata({})
46 .SendCloseFromClient()
47 .RecvInitialMetadata(server_initial_metadata)
48 .RecvStatusOnClient(server_status);
49 auto s = test.RequestCall(101);
50 test.Expect(101, true);
51 test.Step();
52 IncomingCloseOnServer client_close;
53 s.NewBatch(102)
54 .SendInitialMetadata({})
55 .SendStatusFromServer(
56 request_is_success ? GRPC_STATUS_OK : GRPC_STATUS_UNIMPLEMENTED,
57 "xyz", {})
58 .RecvCloseOnServer(client_close);
59 test.Expect(102, true);
60 test.Expect(1, true);
61 test.Step();
62 EXPECT_EQ(server_status.message(), "xyz");
63 EXPECT_EQ(s.method(), "/foo");
64 }
65
CORE_END2END_TEST(CoreEnd2endTest,Channelz)66 CORE_END2END_TEST(CoreEnd2endTest, Channelz) {
67 SKIP_IF_V3();
68 auto args = ChannelArgs()
69 .Set(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE, 0)
70 .Set(GRPC_ARG_ENABLE_CHANNELZ, true);
71 InitServer(args);
72 InitClient(args);
73
74 channelz::ChannelNode* channelz_channel =
75 grpc_channel_get_channelz_node(client());
76 ASSERT_NE(channelz_channel, nullptr);
77
78 channelz::ServerNode* channelz_server =
79 Server::FromC(server())->channelz_node();
80 ASSERT_NE(channelz_server, nullptr);
81
82 std::string json = channelz_channel->RenderJsonString();
83 // nothing is present yet
84 EXPECT_THAT(json, Not(HasSubstr("\"callsStarted\"")));
85 EXPECT_THAT(json, Not(HasSubstr("\"callsFailed\"")));
86 EXPECT_THAT(json, Not(HasSubstr("\"callsSucceeded\"")));
87
88 // one successful request
89 RunOneRequest(*this, true);
90
91 json = channelz_channel->RenderJsonString();
92 EXPECT_THAT(json, HasSubstr("\"callsStarted\":\"1\""));
93 EXPECT_THAT(json, HasSubstr("\"callsSucceeded\":\"1\""));
94
95 // one failed request
96 RunOneRequest(*this, false);
97
98 json = channelz_channel->RenderJsonString();
99 EXPECT_THAT(json, HasSubstr("\"callsStarted\":\"2\""));
100 EXPECT_THAT(json, HasSubstr("\"callsFailed\":\"1\""));
101 EXPECT_THAT(json, HasSubstr("\"callsSucceeded\":\"1\""));
102 // channel tracing is not enabled, so these should not be preset.
103 EXPECT_THAT(json, Not(HasSubstr("\"trace\"")));
104 EXPECT_THAT(json, Not(HasSubstr("\"description\":\"Channel created\"")));
105 EXPECT_THAT(json, Not(HasSubstr("\"severity\":\"CT_INFO\"")));
106
107 json = channelz_server->RenderJsonString();
108 EXPECT_THAT(json, HasSubstr("\"callsStarted\":\"2\""));
109 EXPECT_THAT(json, HasSubstr("\"callsFailed\":\"1\""));
110 EXPECT_THAT(json, HasSubstr("\"callsSucceeded\":\"1\""));
111 // channel tracing is not enabled, so these should not be preset.
112 EXPECT_THAT(json, Not(HasSubstr("\"trace\"")));
113 EXPECT_THAT(json, Not(HasSubstr("\"description\":\"Channel created\"")));
114 EXPECT_THAT(json, Not(HasSubstr("\"severity\":\"CT_INFO\"")));
115
116 json = channelz_server->RenderServerSockets(0, 100);
117 EXPECT_THAT(json, HasSubstr("\"end\":true"));
118 }
119
CORE_END2END_TEST(CoreEnd2endTest,ChannelzWithChannelTrace)120 CORE_END2END_TEST(CoreEnd2endTest, ChannelzWithChannelTrace) {
121 SKIP_IF_V3();
122 auto args =
123 ChannelArgs()
124 .Set(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE, 1024 * 1024)
125 .Set(GRPC_ARG_ENABLE_CHANNELZ, true);
126 InitServer(args);
127 InitClient(args);
128
129 channelz::ChannelNode* channelz_channel =
130 grpc_channel_get_channelz_node(client());
131 ASSERT_NE(channelz_channel, nullptr);
132
133 channelz::ServerNode* channelz_server =
134 Server::FromC(server())->channelz_node();
135 ASSERT_NE(channelz_server, nullptr);
136
137 RunOneRequest(*this, true);
138
139 std::string json = channelz_channel->RenderJsonString();
140 EXPECT_THAT(json, HasSubstr("\"trace\""));
141 EXPECT_THAT(json, HasSubstr("\"description\":\"Channel created\""));
142 EXPECT_THAT(json, HasSubstr("\"severity\":\"CT_INFO\""));
143
144 json = channelz_server->RenderJsonString();
145 EXPECT_THAT(json, HasSubstr("\"trace\""));
146 EXPECT_THAT(json, HasSubstr("\"description\":\"Server created\""));
147 EXPECT_THAT(json, HasSubstr("\"severity\":\"CT_INFO\""));
148 }
149
CORE_END2END_TEST(CoreEnd2endTest,ChannelzDisabled)150 CORE_END2END_TEST(CoreEnd2endTest, ChannelzDisabled) {
151 SKIP_IF_V3();
152 auto args = ChannelArgs()
153 .Set(GRPC_ARG_MAX_CHANNEL_TRACE_EVENT_MEMORY_PER_NODE, 0)
154 .Set(GRPC_ARG_ENABLE_CHANNELZ, false);
155 InitServer(args);
156 InitClient(args);
157 channelz::ChannelNode* channelz_channel =
158 grpc_channel_get_channelz_node(client());
159 EXPECT_EQ(channelz_channel, nullptr);
160 RunOneRequest(*this, true);
161 }
162
163 } // namespace
164 } // namespace grpc_core
165