Skip to content

Commit

Permalink
With go-grpc-middleware, add grpc_zap to logging grpc request and res…
Browse files Browse the repository at this point in the history
…ponse content in grpc-proxy mode In our test environment, it may be very useful to debug who delete etcd's key with grpc-proxy mode

inspired by https://github.com/grpc-ecosystem/go-grpc-middleware

Signed-off-by: stefanbo <[email protected]>

Update flag name

1. add changelog
2. update flag name to `experimental-enable-grpc-debug`

Signed-off-by: stefan bo <[email protected]>
  • Loading branch information
stefanmonkey committed Jul 29, 2022
1 parent 6d27a42 commit d261b93
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG/CHANGELOG-3.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ See [code changes](https://github.com/etcd-io/etcd/compare/v3.5.0...v3.6.0).
- Add [v3 discovery](https://github.com/etcd-io/etcd/pull/13635) to bootstrap a new etcd cluster.
- Add [field `storage`](https://github.com/etcd-io/etcd/pull/13772) into the response body of endpoint `/version`.
- Add [`etcd --max-concurrent-streams`](https://github.com/etcd-io/etcd/pull/14169) flag to configure the max concurrent streams each client can open at a time, and defaults to math.MaxUint32.
- Add [`etcd grpc-proxy --experimental-enable-grpc-debug`](https://github.com/etcd-io/etcd/pull/14266) flag to logging all grpc request and response
- Fix [non mutating requests pass through quotaKVServer when NOSPACE](https://github.com/etcd-io/etcd/pull/13435)
- Fix [exclude the same alarm type activated by multiple peers](https://github.com/etcd-io/etcd/pull/13467).
- Fix [Provide a better liveness probe for when etcd runs as a Kubernetes pod](https://github.com/etcd-io/etcd/pull/13399)
Expand Down
32 changes: 30 additions & 2 deletions server/etcdmain/grpc_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ import (
"go.etcd.io/etcd/server/v3/proxy/grpcproxy"
"go.uber.org/zap/zapgrpc"

grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_zap "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap"
grpc_ctxtags "github.com/grpc-ecosystem/go-grpc-middleware/tags"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/soheilhy/cmux"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -89,6 +92,7 @@ var (

grpcProxyEnablePprof bool
grpcProxyEnableOrdering bool
grpcProxyEnableLogging bool

grpcProxyDebug bool

Expand Down Expand Up @@ -159,6 +163,7 @@ func newGRPCProxyStartCommand() *cobra.Command {
// experimental flags
cmd.Flags().BoolVar(&grpcProxyEnableOrdering, "experimental-serializable-ordering", false, "Ensure serializable reads have monotonically increasing store revisions across endpoints.")
cmd.Flags().StringVar(&grpcProxyLeasing, "experimental-leasing-prefix", "", "leasing metadata prefix for disconnected linearized reads.")
cmd.Flags().BoolVar(&grpcProxyEnableLogging, "experimental-enable-grpc-debug", false, "logging all grpc request and response")

cmd.Flags().BoolVar(&grpcProxyDebug, "debug", false, "Enable debug-level logging for grpc-proxy.")

Expand Down Expand Up @@ -430,9 +435,32 @@ func newGRPCProxyServer(lg *zap.Logger, client *clientv3.Client) *grpc.Server {
electionp := grpcproxy.NewElectionProxy(client)
lockp := grpcproxy.NewLockProxy(client)

alwaysLoggingDeciderServer := func(ctx context.Context, fullMethodName string, servingObject interface{}) bool { return true }

grpcChainStreamList := []grpc.StreamServerInterceptor{
grpc_prometheus.StreamServerInterceptor,
}
grpcChainUnaryList := []grpc.UnaryServerInterceptor{
grpc_prometheus.UnaryServerInterceptor,
}
if grpcProxyEnableLogging {
grpcChainStreamList = append(grpcChainStreamList,
grpc_ctxtags.StreamServerInterceptor(),
grpc_zap.PayloadStreamServerInterceptor(lg, alwaysLoggingDeciderServer),
)
grpcChainUnaryList = append(grpcChainUnaryList,
grpc_ctxtags.UnaryServerInterceptor(),
grpc_zap.PayloadUnaryServerInterceptor(lg, alwaysLoggingDeciderServer),
)
}

gopts := []grpc.ServerOption{
grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),
grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
grpcChainStreamList...,
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpcChainUnaryList...,
)),
grpc.MaxConcurrentStreams(math.MaxUint32),
}
if grpcKeepAliveMinTime > time.Duration(0) {
Expand Down

0 comments on commit d261b93

Please sign in to comment.