From 0ea1a1dafb14f90e9d1ab7b5dcd7896c7a5b0abf Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sat, 13 Jan 2024 21:39:38 -0700 Subject: [PATCH 1/3] config option to log gRPC queries --- baseapp/grpcserver.go | 7 ++++++- server/config/config.go | 4 ++++ server/config/toml.go | 4 ++++ server/grpc/server.go | 2 +- server/types/app.go | 2 +- 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/baseapp/grpcserver.go b/baseapp/grpcserver.go index d04f71d1abce..0b77d7c471e4 100644 --- a/baseapp/grpcserver.go +++ b/baseapp/grpcserver.go @@ -2,6 +2,7 @@ package baseapp import ( "context" + "fmt" "strconv" gogogrpc "github.com/cosmos/gogoproto/grpc" @@ -21,7 +22,7 @@ import ( func (app *BaseApp) GRPCQueryRouter() *GRPCQueryRouter { return app.grpcQueryRouter } // RegisterGRPCServer registers gRPC services directly with the gRPC server. -func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server) { +func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server, logQueries bool) { // Define an interceptor for all gRPC queries: this interceptor will create // a new sdk.Context, and pass it into the query handler. interceptor := func(grpcCtx context.Context, req interface{}, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { @@ -65,6 +66,10 @@ func (app *BaseApp) RegisterGRPCServer(server gogogrpc.Server) { app.logger.Error("failed to set gRPC header", "err", err) } + if logQueries { + app.logger.Info("gRPC query received of type: " + fmt.Sprintf("%#v", req)) + } + return handler(grpcCtx, req) } diff --git a/server/config/config.go b/server/config/config.go index 90dad0c943cb..8d2c212cfd74 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -177,6 +177,9 @@ type GRPCConfig struct { // MaxSendMsgSize defines the max message size in bytes the server can send. // The default value is math.MaxInt32. MaxSendMsgSize int `mapstructure:"max-send-msg-size"` + + // LogQueries logs every gRPC query to the console as an info log. + LogQueries bool `mapstructure:"log-queries"` } // GRPCWebConfig defines configuration for the gRPC-web server. @@ -319,6 +322,7 @@ func DefaultConfig() *Config { Address: DefaultGRPCAddress, MaxRecvMsgSize: DefaultGRPCMaxRecvMsgSize, MaxSendMsgSize: DefaultGRPCMaxSendMsgSize, + LogQueries: false, }, Rosetta: RosettaConfig{ Enable: false, diff --git a/server/config/toml.go b/server/config/toml.go index 1ec7ce6a2e2d..70e6d472a54d 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -207,6 +207,10 @@ max-recv-msg-size = "{{ .GRPC.MaxRecvMsgSize }}" # The default value is math.MaxInt32. max-send-msg-size = "{{ .GRPC.MaxSendMsgSize }}" +# LogQueries if enabled will print an info log stating what query type +# was submitted to this node on every submission. +log-queries = "{{ .GRPC.LogQueries }}" + ############################################################################### ### gRPC Web Configuration ### ############################################################################### diff --git a/server/grpc/server.go b/server/grpc/server.go index 79a9be3dca24..b21d9afa629c 100644 --- a/server/grpc/server.go +++ b/server/grpc/server.go @@ -35,7 +35,7 @@ func StartGRPCServer(clientCtx client.Context, app types.Application, cfg config grpc.MaxRecvMsgSize(maxRecvMsgSize), ) - app.RegisterGRPCServer(grpcSrv) + app.RegisterGRPCServer(grpcSrv, cfg.LogQueries) // Reflection allows consumers to build dynamic clients that can write to any // Cosmos SDK application without relying on application packages at compile diff --git a/server/types/app.go b/server/types/app.go index 727f767fc35e..eb8d4e78c83e 100644 --- a/server/types/app.go +++ b/server/types/app.go @@ -46,7 +46,7 @@ type ( // RegisterGRPCServer registers gRPC services directly with the gRPC // server. - RegisterGRPCServer(grpc.Server) + RegisterGRPCServer(grpc.Server, bool) // RegisterTxService registers the gRPC Query service for tx (such as tx // simulation, fetching txs by hash...). From ad306d8682b34775f6393631a237e7136404bb25 Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 14 Jan 2024 13:50:21 -0700 Subject: [PATCH 2/3] clarify gRPC log comment --- server/config/toml.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/config/toml.go b/server/config/toml.go index 70e6d472a54d..5a560ccff51b 100644 --- a/server/config/toml.go +++ b/server/config/toml.go @@ -207,8 +207,9 @@ max-recv-msg-size = "{{ .GRPC.MaxRecvMsgSize }}" # The default value is math.MaxInt32. max-send-msg-size = "{{ .GRPC.MaxSendMsgSize }}" -# LogQueries if enabled will print an info log stating what query type -# was submitted to this node on every submission. +# LogQueries if enabled will print an info log containing the query request +# that was submitted to this node on every submission. +# This is useful strictly for debugging purposes and should be disabled otherwise. log-queries = "{{ .GRPC.LogQueries }}" ############################################################################### From 19aacbd5490b1a9178da0b7a2ccd9042a316797a Mon Sep 17 00:00:00 2001 From: Adam Tucker Date: Sun, 14 Jan 2024 13:51:54 -0700 Subject: [PATCH 3/3] create var --- server/config/config.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server/config/config.go b/server/config/config.go index 8d2c212cfd74..cd9d814abe81 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -34,6 +34,10 @@ const ( // bytes the server can send. DefaultGRPCMaxSendMsgSize = math.MaxInt32 + // DefaultLogQueries defines the default value for the log_queries parameter. + // Should be set to false unless debugging. + DefaultLogQueries = false + // FileStreamer defines the store streaming type for file streaming. FileStreamer = "file" ) @@ -322,7 +326,7 @@ func DefaultConfig() *Config { Address: DefaultGRPCAddress, MaxRecvMsgSize: DefaultGRPCMaxRecvMsgSize, MaxSendMsgSize: DefaultGRPCMaxSendMsgSize, - LogQueries: false, + LogQueries: DefaultLogQueries, }, Rosetta: RosettaConfig{ Enable: false,