From 7d2cefc49c562b0df8a7ec2e71dfabda57eed383 Mon Sep 17 00:00:00 2001 From: yeya24 Date: Mon, 4 Aug 2025 14:09:49 -0700 Subject: [PATCH] enable channelz config Signed-off-by: yeya24 --- go.mod | 4 +++- go.sum | 4 ++++ server/server.go | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 6780aee9..6640ea17 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,8 @@ require ( github.com/pmezard/go-difflib v1.0.0 github.com/prometheus/client_golang v1.15.1 github.com/prometheus/exporter-toolkit v0.8.2 + github.com/rantav/go-grpc-channelz v0.0.4 + github.com/sercand/kuberesolver/v5 v5.1.1 github.com/sirupsen/logrus v1.6.0 github.com/soheilhy/cmux v0.1.5 github.com/stretchr/testify v1.8.1 @@ -41,6 +43,7 @@ require ( github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd // indirect github.com/coreos/go-systemd/v22 v22.4.0 // indirect github.com/fsnotify/fsnotify v1.6.0 // indirect + github.com/go-chi/chi/v5 v5.0.7 // indirect github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/jpillora/backoff v1.0.0 // indirect github.com/konsorten/go-windows-terminal-sequences v1.0.3 // indirect @@ -53,7 +56,6 @@ require ( github.com/prometheus/common v0.42.0 // indirect github.com/prometheus/procfs v0.9.0 // indirect github.com/rogpeppe/go-internal v1.13.1 // indirect - github.com/sercand/kuberesolver/v5 v5.1.1 // indirect go.uber.org/atomic v1.5.1 // indirect golang.org/x/crypto v0.29.0 // indirect golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect diff --git a/go.sum b/go.sum index 8bff6c9e..d6bcb497 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,8 @@ github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBd github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw= +github.com/go-chi/chi/v5 v5.0.7 h1:rDTPXLDHGATaeHvVlLcR4Qe0zftYethFucbjVQ1PxU8= +github.com/go-chi/chi/v5 v5.0.7/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU= github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0= 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 github.com/prometheus/exporter-toolkit v0.8.2/go.mod h1:00shzmJL7KxcsabLWcONwpyNEuWhREOnFqZW7vadFS0= github.com/prometheus/procfs v0.9.0 h1:wzCHvIvM5SxWqYvwgVL7yJY8Lz3PKn49KQtpgMYJfhI= github.com/prometheus/procfs v0.9.0/go.mod h1:+pB4zwohETzFnmlpe6yd2lSc+0/46IYZRB/chUwxUZY= +github.com/rantav/go-grpc-channelz v0.0.4 h1:8GvqhA6siQVBsZYzal3yHhyJ9YiHEJx7RtSH2Jvm9Co= +github.com/rantav/go-grpc-channelz v0.0.4/go.mod h1:HodrRmnnH1zXcEEfK7EJrI23YMPMT7uvyAYkq2JUIcI= github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= github.com/sercand/kuberesolver/v5 v5.1.1 h1:CYH+d67G0sGBj7q5wLK61yzqJJ8gLLC8aeprPTHb6yY= diff --git a/server/server.go b/server/server.go index 5d4776a6..0971c8f3 100644 --- a/server/server.go +++ b/server/server.go @@ -17,10 +17,12 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/exporter-toolkit/web" + channelz "github.com/rantav/go-grpc-channelz" "github.com/soheilhy/cmux" "golang.org/x/net/context" "golang.org/x/net/netutil" "google.golang.org/grpc" + channelzservice "google.golang.org/grpc/channelz/service" "google.golang.org/grpc/credentials" "google.golang.org/grpc/keepalive" @@ -110,6 +112,8 @@ type Config struct { GRPCServerMinTimeBetweenPings time.Duration `yaml:"grpc_server_min_time_between_pings"` GRPCServerPingWithoutStreamAllowed bool `yaml:"grpc_server_ping_without_stream_allowed"` + EnableChannelz bool `yaml:"enable_channelz"` + LogFormat logging.Format `yaml:"log_format"` LogLevel logging.Level `yaml:"log_level"` Log logging.Interface `yaml:"-"` @@ -168,6 +172,7 @@ func (cfg *Config) RegisterFlags(f *flag.FlagSet) { 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") 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.") 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.") + 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") f.StringVar(&cfg.PathPrefix, "server.path-prefix", "", "Base path to serve all API routes from (e.g. /v1/)") cfg.LogFormat.RegisterFlags(f) cfg.LogLevel.RegisterFlags(f) @@ -376,6 +381,12 @@ func newServer(cfg Config, metrics *Metrics) (*Server, error) { grpcServer := grpc.NewServer(grpcOptions...) grpcOnHttpServer := grpc.NewServer(grpcOptions...) + // Register channelz service if enabled + if cfg.EnableChannelz { + channelzservice.RegisterChannelzServiceToServer(grpcServer) + channelzservice.RegisterChannelzServiceToServer(grpcOnHttpServer) + } + // Setup HTTP server var router *mux.Router if cfg.Router != nil { @@ -392,6 +403,13 @@ func newServer(cfg Config, metrics *Metrics) (*Server, error) { RegisterInstrumentationWithGatherer(router, gatherer) } + // Register channelz web UI if enabled + if cfg.EnableChannelz { + // Mount channelz handler at /channelz + grpcAddr := fmt.Sprintf("%s:%d", cfg.GRPCListenAddress, cfg.GRPCListenPort) + router.PathPrefix("/channelz").Handler(channelz.CreateHandler("/", grpcAddr)) + } + var sourceIPs *middleware.SourceIPExtractor if cfg.LogSourceIPs { sourceIPs, err = middleware.NewSourceIPs(cfg.LogSourceIPsHeader, cfg.LogSourceIPsRegex)