Skip to content

Commit

Permalink
ensure new logger, to not extend log context
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Landgraf committed Nov 11, 2021
1 parent caa033a commit acc014b
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions grpc/server.go
Expand Up @@ -68,7 +68,9 @@ func Server(ab AuthBackend) *grpc.Server {
myServer := grpc.NewServer(
grpc.StreamInterceptor(grpc_middleware.ChainStreamServer(
func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
ctx := log.WithContext(stream.Context())
logger := log.Logger().With().Logger()
ctx := logger.WithContext(stream.Context())

wrappedStream := grpc_middleware.WrapServerStream(stream)
wrappedStream.WrappedContext = ctx
var addr string
Expand All @@ -83,7 +85,6 @@ func Server(ab AuthBackend) *grpc.Server {
ctx = security.ContextWithToken(ctx, security.TokenString(bt[0]))
}

logger := zerolog.Ctx(ctx)
logger.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str("req_id", strings.Join(md.Get("req_id"), ""))
})
Expand All @@ -100,7 +101,7 @@ func Server(ab AuthBackend) *grpc.Server {
return err
},
func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) (err error) {
defer errors.HandleWithCtx(stream.Context(), "GRPC"+info.FullMethod)
defer errors.HandleWithCtx(stream.Context(), "GRPC "+info.FullMethod)
err = InternalServerError // default in case of a panic
err = handler(srv, stream)
return err
Expand All @@ -112,21 +113,21 @@ func Server(ab AuthBackend) *grpc.Server {
)),
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
logger := log.Logger().With().Logger()
ctx = logger.WithContext(ctx)

var addr string
if p, ok := peer.FromContext(ctx); ok {
addr = p.Addr.String()
}

ctx = log.WithContext(ctx)

md, _ := metadata.FromIncomingContext(ctx)

bt := md.Get("bearer_token")
if len(bt) > 0 {
ctx = security.ContextWithToken(ctx, security.TokenString(bt[0]))
}

logger := zerolog.Ctx(ctx)
logger.UpdateContext(func(c zerolog.Context) zerolog.Context {
return c.Str("req_id", strings.Join(md.Get("req_id"), ""))
})
Expand All @@ -145,7 +146,7 @@ func Server(ab AuthBackend) *grpc.Server {
return
},
func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
defer errors.HandleWithCtx(ctx, "GRPC"+info.FullMethod)
defer errors.HandleWithCtx(ctx, "GRPC "+info.FullMethod)
err = InternalServerError // default in case of a panic
resp, err = handler(ctx, req)
return
Expand Down

0 comments on commit acc014b

Please sign in to comment.