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