Skip to content

Commit

Permalink
Added logging server interceptor
Browse files Browse the repository at this point in the history
  • Loading branch information
0xKrishna committed Jul 19, 2022
1 parent bc47b07 commit 502e41e
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion server/gRPC/gRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gRPC
import (
"context"
"net"
"time"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/maticnetwork/heimdall/server/gRPC/proto"
Expand All @@ -26,7 +27,7 @@ type HeimdallGRPCServer struct {

func SetupGRPCServer(shutDownCtx context.Context, cdc *codec.Codec, addr string, lggr tmLog.Logger) error {
logger = lggr
grpcServer := grpc.NewServer()
grpcServer := grpc.NewServer(withLoggingUnaryInterceptor())
proto.RegisterHeimdallServer(grpcServer,
&HeimdallGRPCServer{
cdc: cdc,
Expand All @@ -52,3 +53,16 @@ func SetupGRPCServer(shutDownCtx context.Context, cdc *codec.Codec, addr string,

return nil
}

func withLoggingUnaryInterceptor() grpc.ServerOption {
return grpc.UnaryInterceptor(loggingServerInterceptor)
}

func loggingServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
start := time.Now()
h, err := handler(ctx, req)

logger.Info("Request", "method", info.FullMethod, "duration", time.Since(start), "error", err)

return h, err
}

0 comments on commit 502e41e

Please sign in to comment.