Skip to content

Commit

Permalink
cmd/server: log dispatching at debug level
Browse files Browse the repository at this point in the history
Prior to this change, dispatch logs can become
VERY noisy and most use cases don't necessitate
them.
  • Loading branch information
jzelinskie committed Apr 11, 2024
1 parent 095ffd3 commit f30c5cb
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions pkg/cmd/server/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,26 @@ func MetricsHandler(telemetryRegistry *prometheus.Registry, c *Config) http.Hand
return mux
}

// the server has a deadline set, so we consider it a normal condition
// this makes sure we don't log them as errors
var defaultCodeToLevel = grpclog.WithLevels(func(code codes.Code) grpclog.Level {
if code == codes.DeadlineExceeded {
// The server has a deadline set, so we consider it a normal condition.
// This ensures that we don't log them as errors.
return grpclog.LevelInfo
}
return grpclog.DefaultServerCodeToLevel(code)
})

var dispatchDefaultCodeToLevel = grpclog.WithLevels(func(code codes.Code) grpclog.Level {
switch code {
case codes.OK, codes.Canceled:
return grpclog.LevelDebug
case codes.NotFound, codes.AlreadyExists, codes.InvalidArgument, codes.Unauthenticated:
return grpclog.LevelWarn
default:
return grpclog.DefaultServerCodeToLevel(code)
}
})

var durationFieldOption = grpclog.WithDurationField(func(duration time.Duration) grpclog.Fields {
return grpclog.Fields{"grpc.time_ms", duration.Milliseconds()}
})
Expand Down Expand Up @@ -376,15 +387,15 @@ func DefaultDispatchMiddleware(logger zerolog.Logger, authFunc grpcauth.AuthFunc
return []grpc.UnaryServerInterceptor{
requestid.UnaryServerInterceptor(requestid.GenerateIfMissing(true)),
logmw.UnaryServerInterceptor(logmw.ExtractMetadataField(string(requestmeta.RequestIDKey), "requestID")),
grpclog.UnaryServerInterceptor(InterceptorLogger(logger), defaultCodeToLevel, durationFieldOption, traceIDFieldOption),
grpclog.UnaryServerInterceptor(InterceptorLogger(logger), dispatchDefaultCodeToLevel, durationFieldOption, traceIDFieldOption),
grpcMetricsUnaryInterceptor,
grpcauth.UnaryServerInterceptor(authFunc),
datastoremw.UnaryServerInterceptor(ds),
servicespecific.UnaryServerInterceptor,
}, []grpc.StreamServerInterceptor{
requestid.StreamServerInterceptor(requestid.GenerateIfMissing(true)),
logmw.StreamServerInterceptor(logmw.ExtractMetadataField(string(requestmeta.RequestIDKey), "requestID")),
grpclog.StreamServerInterceptor(InterceptorLogger(logger), defaultCodeToLevel, durationFieldOption, traceIDFieldOption),
grpclog.StreamServerInterceptor(InterceptorLogger(logger), dispatchDefaultCodeToLevel, durationFieldOption, traceIDFieldOption),
grpcMetricsStreamingInterceptor,
grpcauth.StreamServerInterceptor(authFunc),
datastoremw.StreamServerInterceptor(ds),
Expand Down

0 comments on commit f30c5cb

Please sign in to comment.