Skip to content

Commit 3018f14

Browse files
ctillercopybara-github
authored andcommitted
[channelz-v2] Introduce start of zviz
I'm building out a new framework for consuming channelz-v2 data and producing visualizations in various formats. This commit adds some string utilities that'll be needed in a few places. PiperOrigin-RevId: 775678939
1 parent e7b3051 commit 3018f14

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

src/core/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10193,6 +10193,17 @@ grpc_cc_library(
1019310193
],
1019410194
)
1019510195

10196+
grpc_cc_library(
10197+
name = "zviz_strings",
10198+
srcs = [
10199+
"channelz/zviz/strings.cc",
10200+
],
10201+
hdrs = ["channelz/zviz/strings.h"],
10202+
external_deps = [
10203+
"absl/strings",
10204+
],
10205+
)
10206+
1019610207
### UPB Targets
1019710208

1019810209
grpc_upb_proto_library(

src/core/channelz/zviz/strings.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 The gRPC Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "src/core/channelz/zviz/strings.h"
16+
17+
#include "absl/strings/str_cat.h"
18+
19+
namespace grpc_zviz {
20+
21+
std::string DisplayKind(absl::string_view kind) {
22+
if (kind.empty()) return "Entity";
23+
if (kind == "channel") return "Channel";
24+
if (kind == "subchannel") return "Subchannel";
25+
if (kind == "socket") return "Socket";
26+
return absl::StrCat("Entity kind '", kind, "'");
27+
}
28+
29+
} // namespace grpc_zviz

src/core/channelz/zviz/strings.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2025 The gRPC Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef GRPC_SRC_CORE_CHANNELZ_ZVIZ_STRINGS_H
16+
#define GRPC_SRC_CORE_CHANNELZ_ZVIZ_STRINGS_H
17+
18+
#include <string>
19+
20+
#include "absl/strings/string_view.h"
21+
22+
namespace grpc_zviz {
23+
24+
std::string DisplayKind(absl::string_view kind);
25+
26+
} // namespace grpc_zviz
27+
28+
#endif // GRPC_SRC_CORE_CHANNELZ_ZVIZ_STRINGS_H

test/core/channelz/zviz/BUILD

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Copyright 2016 gRPC authors.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("//bazel:grpc_build_system.bzl", "grpc_package")
16+
load("//test/core/test_util:grpc_fuzzer.bzl", "grpc_fuzz_test")
17+
18+
grpc_package(name = "test/core/channelz/zviz")
19+
20+
licenses(["notice"])
21+
22+
grpc_fuzz_test(
23+
name = "strings_test",
24+
srcs = ["strings_test.cc"],
25+
external_deps = [
26+
"absl/log:check",
27+
"fuzztest",
28+
"fuzztest_main",
29+
],
30+
deps = [
31+
"//src/core:zviz_strings",
32+
],
33+
)
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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 "src/core/channelz/zviz/strings.h"
20+
21+
#include "fuzztest/fuzztest.h"
22+
#include "gtest/gtest.h"
23+
24+
namespace grpc_zviz {
25+
26+
TEST(StringsTest, DisplayKind) {
27+
EXPECT_EQ(DisplayKind("channel"), "Channel");
28+
EXPECT_EQ(DisplayKind("subchannel"), "Subchannel");
29+
EXPECT_EQ(DisplayKind("socket"), "Socket");
30+
EXPECT_EQ(DisplayKind("foo"), "Entity kind 'foo'");
31+
EXPECT_EQ(DisplayKind(""), "Entity");
32+
}
33+
34+
void DisplayKindNeverEmpty(absl::string_view kind) {
35+
EXPECT_NE(DisplayKind(kind), "");
36+
}
37+
FUZZ_TEST(StringsTest, DisplayKindNeverEmpty);
38+
39+
} // namespace grpc_zviz

0 commit comments

Comments
 (0)