-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.go
105 lines (89 loc) · 2.81 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// The interceptor package stay here for testing purpose
// Second step was to move this package in more appropriate code base
//
package logger_grpc
import (
"context"
"path"
"time"
"github.com/gol4ng/logger"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type Options struct {
LoggerContextProvider LoggerContextProvider
LevelFunc CodeToLevel
CodeFunc func(error) codes.Code
}
// LoggerContextProvider function defines the default logger context values
type LoggerContextProvider func(fullMethodName string) *logger.Context
// CodeToLevel function defines the mapping between gRPC return codes and interceptor log level.
type CodeToLevel func(code codes.Code) logger.Level
func DefaultCodeToLevel(code codes.Code) logger.Level {
switch code {
case codes.OK, codes.Canceled, codes.NotFound, codes.AlreadyExists:
return logger.InfoLevel
case codes.InvalidArgument, codes.PermissionDenied, codes.Unauthenticated:
return logger.NoticeLevel
case codes.DeadlineExceeded, codes.ResourceExhausted, codes.FailedPrecondition, codes.Aborted, codes.OutOfRange, codes.Unavailable:
return logger.WarningLevel
//case codes.Unknown, codes.Unimplemented, codes.Internal, codes.DataLoss:
}
return logger.ErrorLevel
}
func newDefaultOptions() *Options {
return &Options{
LoggerContextProvider: func(fullMethodName string) *logger.Context {
return nil
},
LevelFunc: DefaultCodeToLevel,
CodeFunc: status.Code,
}
}
func EvaluateServerOpt(opts []Option) *Options {
optCopy := newDefaultOptions()
for _, o := range opts {
o(optCopy)
}
return optCopy
}
func EvaluateClientOpt(opts []Option) *Options {
optCopy := newDefaultOptions()
for _, o := range opts {
o(optCopy)
}
return optCopy
}
type Option func(*Options)
// WithDecider customizes the function for deciding if the gRPC interceptor logs should log.
func WithLoggerContext(f LoggerContextProvider) Option {
return func(o *Options) {
o.LoggerContextProvider = f
}
}
// WithLevels customizes the function for mapping gRPC return codes and interceptor log level statements.
func WithLevels(f CodeToLevel) Option {
return func(o *Options) {
o.LevelFunc = f
}
}
// WithCodes customizes the function for mapping errors to error codes.
func WithCodes(f func(error) codes.Code) Option {
return func(o *Options) {
o.CodeFunc = f
}
}
func FeedContext(loggerContext *logger.Context, ctx context.Context, fullMethod string, startTime time.Time) *logger.Context {
if loggerContext == nil {
loggerContext = logger.NewContext()
}
loggerContext.
Add("grpc_service", path.Dir(fullMethod)[1:]).
Add("grpc_method", path.Base(fullMethod)).
Add("grpc_start_time", startTime.Format(time.RFC3339))
if d, ok := ctx.Deadline(); ok {
loggerContext.Add("grpc_request_deadline", d.Format(time.RFC3339))
}
return loggerContext
}