UnaryServerInterceptor returns an interceptor that logs gRPC requests
(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler)
| 50 | |
| 51 | // UnaryServerInterceptor returns an interceptor that logs gRPC requests |
| 52 | func (s GRPCServerLog) UnaryServerInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { |
| 53 | begin := time.Now() |
| 54 | resp, err := handler(ctx, req) |
| 55 | if err == nil && s.DisableRequestSuccessLog { |
| 56 | return resp, nil |
| 57 | } |
| 58 | |
| 59 | // Honor sampled error logging. |
| 60 | keep, reason := shouldLog(ctx, err) |
| 61 | if reason != "" { |
| 62 | err = fmt.Errorf("%w (%s)", err, reason) |
| 63 | } |
| 64 | if !keep { |
| 65 | return resp, err |
| 66 | } |
| 67 | |
| 68 | entry := log.With(user.LogWith(ctx, s.Log), "method", info.FullMethod, "duration", time.Since(begin)) |
| 69 | if err != nil { |
| 70 | if s.WithRequest { |
| 71 | entry = log.With(entry, "request", req) |
| 72 | } |
| 73 | if grpcUtils.IsCanceled(err) { |
| 74 | level.Debug(entry).Log("msg", gRPC, "err", err) |
| 75 | } else { |
| 76 | level.Warn(entry).Log("msg", gRPC, "err", err) |
| 77 | } |
| 78 | } else { |
| 79 | level.Debug(entry).Log("msg", dskit_log.LazySprintf("%s (success)", gRPC)) |
| 80 | } |
| 81 | return resp, err |
| 82 | } |
| 83 | |
| 84 | // StreamServerInterceptor returns an interceptor that logs gRPC requests |
| 85 | func (s GRPCServerLog) StreamServerInterceptor(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { |