Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract zap logger from context for logging server interceptors #476

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions logging/zap/server_interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ var (
ServerField = zap.String("span.kind", "server")
)

type extractLogger = func(ctx context.Context) *zap.Logger

// UnaryServerInterceptor returns a new unary server interceptors that adds zap.Logger to the context.
func UnaryServerInterceptor(logger *zap.Logger, opts ...Option) grpc.UnaryServerInterceptor {
return LoggingUnaryServerInterceptor(
func(_ context.Context) *zap.Logger {
return logger
}, opts...)
}

// LoggingUnaryServerInterceptor returns a new unary server interceptors that extracts zap.Logger from the context.
func LoggingUnaryServerInterceptor(extractLogger extractLogger, opts ...Option) grpc.UnaryServerInterceptor {
o := evaluateServerOpt(opts)
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
startTime := time.Now()

newCtx := newLoggerForCall(ctx, logger, info.FullMethod, startTime, o.timestampFormat)
newCtx := newLoggerForCall(ctx, extractLogger(ctx), info.FullMethod, startTime, o.timestampFormat)

resp, err := handler(newCtx, req)
if !o.shouldLog(info.FullMethod, err) {
Expand All @@ -43,10 +53,20 @@ func UnaryServerInterceptor(logger *zap.Logger, opts ...Option) grpc.UnaryServer

// StreamServerInterceptor returns a new streaming server interceptor that adds zap.Logger to the context.
func StreamServerInterceptor(logger *zap.Logger, opts ...Option) grpc.StreamServerInterceptor {
return LoggingStreamServerInterceptor(
func(_ context.Context) *zap.Logger {
return logger
}, opts...)
}

// LoggingStreamServerInterceptor returns a new streaming server interceptor that extracts zap.Logger from the context.
func LoggingStreamServerInterceptor(extractLogger extractLogger, opts ...Option) grpc.StreamServerInterceptor {
o := evaluateServerOpt(opts)
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx := stream.Context()

startTime := time.Now()
newCtx := newLoggerForCall(stream.Context(), logger, info.FullMethod, startTime, o.timestampFormat)
newCtx := newLoggerForCall(ctx, extractLogger(ctx), info.FullMethod, startTime, o.timestampFormat)
wrapped := grpc_middleware.WrapServerStream(stream)
wrapped.WrappedContext = newCtx

Expand Down