github.com/banmanh482/nomad@v0.11.8/helper/grpc-middleware/logging/options.go (about)

     1  package logging
     2  
     3  import (
     4  	"github.com/hashicorp/go-hclog"
     5  	"google.golang.org/grpc/codes"
     6  )
     7  
     8  type options struct {
     9  	levelFunc CodeToLevel
    10  }
    11  
    12  var defaultOptions = &options{}
    13  
    14  type Option func(*options)
    15  
    16  func evaluateClientOpt(opts []Option) *options {
    17  	optCopy := &options{}
    18  	*optCopy = *defaultOptions
    19  	optCopy.levelFunc = DefaultCodeToLevel
    20  	for _, o := range opts {
    21  		o(optCopy)
    22  	}
    23  	return optCopy
    24  }
    25  
    26  func WithStatusCodeToLevelFunc(fn CodeToLevel) Option {
    27  	return func(opts *options) {
    28  		opts.levelFunc = fn
    29  	}
    30  }
    31  
    32  // CodeToLevel function defines the mapping between gRPC return codes and hclog level.
    33  type CodeToLevel func(code codes.Code) hclog.Level
    34  
    35  func DefaultCodeToLevel(code codes.Code) hclog.Level {
    36  	switch code {
    37  	// Trace Logs -- Useful for Nomad developers but not necessarily always wanted
    38  	case codes.OK:
    39  		return hclog.Trace
    40  
    41  	// Debug logs
    42  	case codes.Canceled:
    43  		return hclog.Debug
    44  	case codes.InvalidArgument:
    45  		return hclog.Debug
    46  	case codes.ResourceExhausted:
    47  		return hclog.Debug
    48  	case codes.FailedPrecondition:
    49  		return hclog.Debug
    50  	case codes.Aborted:
    51  		return hclog.Debug
    52  	case codes.OutOfRange:
    53  		return hclog.Debug
    54  	case codes.NotFound:
    55  		return hclog.Debug
    56  	case codes.AlreadyExists:
    57  		return hclog.Debug
    58  
    59  	// Info Logs - More curious/interesting than debug, but not necessarily critical
    60  	case codes.Unknown:
    61  		return hclog.Info
    62  	case codes.DeadlineExceeded:
    63  		return hclog.Info
    64  	case codes.PermissionDenied:
    65  		return hclog.Info
    66  	case codes.Unauthenticated:
    67  		// unauthenticated requests are probably usually fine?
    68  		return hclog.Info
    69  	case codes.Unavailable:
    70  		// unavailable errors indicate the upstream is not currently available. Info
    71  		// because I would guess these are usually transient and will be handled by
    72  		// retry mechanisms before being served as a higher level warning.
    73  		return hclog.Info
    74  
    75  	// Warn Logs - These are almost definitely bad in most cases - usually because
    76  	//             the upstream is broken.
    77  	case codes.Unimplemented:
    78  		return hclog.Warn
    79  	case codes.Internal:
    80  		return hclog.Warn
    81  	case codes.DataLoss:
    82  		return hclog.Warn
    83  
    84  	default:
    85  		// Codes that aren't implemented as part of a CodeToLevel case are probably
    86  		// unknown and should be surfaced.
    87  		return hclog.Info
    88  	}
    89  }