Skip to content

Commit

Permalink
enhancement: Log client address for gRPC calls (#31)
Browse files Browse the repository at this point in the history
Signed-off-by: jannfis <[email protected]>
  • Loading branch information
jannfis authored Mar 1, 2024
1 parent 5be3d2a commit 3297ec3
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions principal/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/jannfis/argocd-agent/internal/grpcutil"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
Expand Down Expand Up @@ -36,14 +37,20 @@ func InterceptorLogger(l logrus.FieldLogger) logging.Logger {

func unaryRequestLogger() grpc.UnaryServerInterceptor {
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
log().WithField("method", info.FullMethod).Debug("Processing unary gRPC request")
log().WithFields(logrus.Fields{
"method": info.FullMethod,
"client_addr": grpcutil.AddressFromContext(ctx),
}).Debug("Processing unary gRPC request")
return handler(ctx, req)
}
}

func streamRequestLogger() grpc.StreamServerInterceptor {
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
log().WithField("method", info.FullMethod).Debug("Processing streaming gRPC request")
log().WithFields(logrus.Fields{
"method": info.FullMethod,
"client_addr": grpcutil.AddressFromContext(ss.Context()),
}).Debug("Processing unary gRPC request")
return handler(srv, ss)
}
}

0 comments on commit 3297ec3

Please sign in to comment.