Skip to content

Commit 7d2cefc

Browse files
committed
enable channelz config
Signed-off-by: yeya24 <benye@amazon.com>
1 parent 96019ed commit 7d2cefc

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ require (
2323
github.com/pmezard/go-difflib v1.0.0
2424
github.com/prometheus/client_golang v1.15.1
2525
github.com/prometheus/exporter-toolkit v0.8.2
26+
github.com/rantav/go-grpc-channelz v0.0.4
27+
github.com/sercand/kuberesolver/v5 v5.1.1
2628
github.com/sirupsen/logrus v1.6.0
2729
github.com/soheilhy/cmux v0.1.5
2830
github.com/stretchr/testify v1.8.1
@@ -41,6 +43,7 @@ require (
4143
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect
4244
github.com/coreos/go-systemd/v22 v22.4.0 // indirect
4345
github.com/fsnotify/fsnotify v1.6.0 // indirect
46+
github.com/go-chi/chi/v5 v5.0.7 // indirect
4447
github.com/go-logfmt/logfmt v0.5.1 // indirect
4548
github.com/jpillora/backoff v1.0.0 // indirect
4649
github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect
@@ -53,7 +56,6 @@ require (
5356
github.com/prometheus/common v0.42.0 // indirect
5457
github.com/prometheus/procfs v0.9.0 // indirect
5558
github.com/rogpeppe/go-internal v1.13.1 // indirect
56-
github.com/sercand/kuberesolver/v5 v5.1.1 // indirect
5759
go.uber.org/atomic v1.5.1 // indirect
5860
golang.org/x/crypto v0.29.0 // indirect
5961
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBd
1717
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
1818
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
1919
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
20+
github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8=
21+
github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8=
2022
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
2123
github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0=
2224
github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA=
@@ -79,6 +81,8 @@ github.com/prometheus/exporter-toolkit v0.8.2 h1:sbJAfBXQFkG6sUkbwBun8MNdzW9+wd5
7981
github.com/prometheus/exporter-toolkit v0.8.2/go.mod h1:00shzmJL7KxcsabLWcONwpyNEuWhREOnFqZW7vadFS0=
8082
github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI=
8183
github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY=
84+
github.com/rantav/go-grpc-channelz v0.0.4 h1:8GvqhA6siQVBsZYzal3yHhyJ9YiHEJx7RtSH2Jvm9Co=
85+
github.com/rantav/go-grpc-channelz v0.0.4/go.mod h1:HodrRmnnH1zXcEEfK7EJrI23YMPMT7uvyAYkq2JUIcI=
8286
github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII=
8387
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
8488
github.com/sercand/kuberesolver/v5 v5.1.1 h1:CYH+d67G0sGBj7q5wLK61yzqJJ8gLLC8aeprPTHb6yY=

server/server.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import (
1717
"github.com/prometheus/client_golang/prometheus"
1818
"github.com/prometheus/client_golang/prometheus/promhttp"
1919
"github.com/prometheus/exporter-toolkit/web"
20+
channelz "github.com/rantav/go-grpc-channelz"
2021
"github.com/soheilhy/cmux"
2122
"golang.org/x/net/context"
2223
"golang.org/x/net/netutil"
2324
"google.golang.org/grpc"
25+
channelzservice "google.golang.org/grpc/channelz/service"
2426
"google.golang.org/grpc/credentials"
2527
"google.golang.org/grpc/keepalive"
2628

@@ -110,6 +112,8 @@ type Config struct {
110112
GRPCServerMinTimeBetweenPings time.Duration `yaml:"grpc_server_min_time_between_pings"`
111113
GRPCServerPingWithoutStreamAllowed bool `yaml:"grpc_server_ping_without_stream_allowed"`
112114

115+
EnableChannelz bool `yaml:"enable_channelz"`
116+
113117
LogFormat logging.Format `yaml:"log_format"`
114118
LogLevel logging.Level `yaml:"log_level"`
115119
Log logging.Interface `yaml:"-"`
@@ -168,6 +172,7 @@ func (cfg *Config) RegisterFlags(f *flag.FlagSet) {
168172
f.DurationVar(&cfg.GRPCServerTimeout, "server.grpc.keepalive.timeout", time.Second*20, "After having pinged for keepalive check, the duration after which an idle connection should be closed, Default: 20s")
169173
f.DurationVar(&cfg.GRPCServerMinTimeBetweenPings, "server.grpc.keepalive.min-time-between-pings", 5*time.Minute, "Minimum amount of time a client should wait before sending a keepalive ping. If client sends keepalive ping more often, server will send GOAWAY and close the connection.")
170174
f.BoolVar(&cfg.GRPCServerPingWithoutStreamAllowed, "server.grpc.keepalive.ping-without-stream-allowed", false, "If true, server allows keepalive pings even when there are no active streams(RPCs). If false, and client sends ping when there are no active streams, server will send GOAWAY and close the connection.")
175+
f.BoolVar(&cfg.EnableChannelz, "server.enable-channelz", false, "Enable Channelz for gRPC server. A web UI will be also exposed on the HTTP server at /channelz")
171176
f.StringVar(&cfg.PathPrefix, "server.path-prefix", "", "Base path to serve all API routes from (e.g. /v1/)")
172177
cfg.LogFormat.RegisterFlags(f)
173178
cfg.LogLevel.RegisterFlags(f)
@@ -376,6 +381,12 @@ func newServer(cfg Config, metrics *Metrics) (*Server, error) {
376381
grpcServer := grpc.NewServer(grpcOptions...)
377382
grpcOnHttpServer := grpc.NewServer(grpcOptions...)
378383

384+
// Register channelz service if enabled
385+
if cfg.EnableChannelz {
386+
channelzservice.RegisterChannelzServiceToServer(grpcServer)
387+
channelzservice.RegisterChannelzServiceToServer(grpcOnHttpServer)
388+
}
389+
379390
// Setup HTTP server
380391
var router *mux.Router
381392
if cfg.Router != nil {
@@ -392,6 +403,13 @@ func newServer(cfg Config, metrics *Metrics) (*Server, error) {
392403
RegisterInstrumentationWithGatherer(router, gatherer)
393404
}
394405

406+
// Register channelz web UI if enabled
407+
if cfg.EnableChannelz {
408+
// Mount channelz handler at /channelz
409+
grpcAddr := fmt.Sprintf("%s:%d", cfg.GRPCListenAddress, cfg.GRPCListenPort)
410+
router.PathPrefix("/channelz").Handler(channelz.CreateHandler("/", grpcAddr))
411+
}
412+
395413
var sourceIPs *middleware.SourceIPExtractor
396414
if cfg.LogSourceIPs {
397415
sourceIPs, err = middleware.NewSourceIPs(cfg.LogSourceIPsHeader, cfg.LogSourceIPsRegex)

0 commit comments

Comments
 (0)