...
1
18
19
20
21 package binarylog
22
23 import (
24 "fmt"
25 "os"
26
27 "google.golang.org/grpc/grpclog"
28 "google.golang.org/grpc/internal/grpcutil"
29 )
30
31 var grpclogLogger = grpclog.Component("binarylog")
32
33
34
35
36
37
38 type Logger interface {
39 GetMethodLogger(methodName string) MethodLogger
40 }
41
42
43
44
45
46 var binLogger Logger
47
48
49
50
51 func SetLogger(l Logger) {
52 binLogger = l
53 }
54
55
56
57
58 func GetLogger() Logger {
59 return binLogger
60 }
61
62
63
64
65
66
67
68 func GetMethodLogger(methodName string) MethodLogger {
69 if binLogger == nil {
70 return nil
71 }
72 return binLogger.GetMethodLogger(methodName)
73 }
74
75 func init() {
76 const envStr = "GRPC_BINARY_LOG_FILTER"
77 configStr := os.Getenv(envStr)
78 binLogger = NewLoggerFromConfigString(configStr)
79 }
80
81
82
83 type MethodLoggerConfig struct {
84
85 Header, Message uint64
86 }
87
88
89 type LoggerConfig struct {
90 All *MethodLoggerConfig
91 Services map[string]*MethodLoggerConfig
92 Methods map[string]*MethodLoggerConfig
93
94 Blacklist map[string]struct{}
95 }
96
97 type logger struct {
98 config LoggerConfig
99 }
100
101
102 func NewLoggerFromConfig(config LoggerConfig) Logger {
103 return &logger{config: config}
104 }
105
106
107
108 func newEmptyLogger() *logger {
109 return &logger{}
110 }
111
112
113 func (l *logger) setDefaultMethodLogger(ml *MethodLoggerConfig) error {
114 if l.config.All != nil {
115 return fmt.Errorf("conflicting global rules found")
116 }
117 l.config.All = ml
118 return nil
119 }
120
121
122
123
124 func (l *logger) setServiceMethodLogger(service string, ml *MethodLoggerConfig) error {
125 if _, ok := l.config.Services[service]; ok {
126 return fmt.Errorf("conflicting service rules for service %v found", service)
127 }
128 if l.config.Services == nil {
129 l.config.Services = make(map[string]*MethodLoggerConfig)
130 }
131 l.config.Services[service] = ml
132 return nil
133 }
134
135
136
137
138 func (l *logger) setMethodMethodLogger(method string, ml *MethodLoggerConfig) error {
139 if _, ok := l.config.Blacklist[method]; ok {
140 return fmt.Errorf("conflicting blacklist rules for method %v found", method)
141 }
142 if _, ok := l.config.Methods[method]; ok {
143 return fmt.Errorf("conflicting method rules for method %v found", method)
144 }
145 if l.config.Methods == nil {
146 l.config.Methods = make(map[string]*MethodLoggerConfig)
147 }
148 l.config.Methods[method] = ml
149 return nil
150 }
151
152
153 func (l *logger) setBlacklist(method string) error {
154 if _, ok := l.config.Blacklist[method]; ok {
155 return fmt.Errorf("conflicting blacklist rules for method %v found", method)
156 }
157 if _, ok := l.config.Methods[method]; ok {
158 return fmt.Errorf("conflicting method rules for method %v found", method)
159 }
160 if l.config.Blacklist == nil {
161 l.config.Blacklist = make(map[string]struct{})
162 }
163 l.config.Blacklist[method] = struct{}{}
164 return nil
165 }
166
167
168
169
170
171
172
173 func (l *logger) GetMethodLogger(methodName string) MethodLogger {
174 s, m, err := grpcutil.ParseMethod(methodName)
175 if err != nil {
176 grpclogLogger.Infof("binarylogging: failed to parse %q: %v", methodName, err)
177 return nil
178 }
179 if ml, ok := l.config.Methods[s+"/"+m]; ok {
180 return NewTruncatingMethodLogger(ml.Header, ml.Message)
181 }
182 if _, ok := l.config.Blacklist[s+"/"+m]; ok {
183 return nil
184 }
185 if ml, ok := l.config.Services[s]; ok {
186 return NewTruncatingMethodLogger(ml.Header, ml.Message)
187 }
188 if l.config.All == nil {
189 return nil
190 }
191 return NewTruncatingMethodLogger(l.config.All.Header, l.config.All.Message)
192 }
193
View as plain text