3 * Copyright 2018 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
28 "google.golang.org/grpc/grpclog"
31 // NewLoggerFromConfigString reads the string and build a logger. It can be used
32 // to build a new logger and assign it to binarylog.Logger.
34 // Example filter config strings:
35 // - "" Nothing will be logged
36 // - "*" All headers and messages will be fully logged.
37 // - "*{h}" Only headers will be logged.
38 // - "*{m:256}" Only the first 256 bytes of each message will be logged.
39 // - "Foo/*" Logs every method in service Foo
40 // - "Foo/*,-Foo/Bar" Logs every method in service Foo except method /Foo/Bar
41 // - "Foo/*,Foo/Bar{m:256}" Logs the first 256 bytes of each message in method
42 // /Foo/Bar, logs all headers and messages in every other method in service
45 // If two configs exist for one certain method or service, the one specified
46 // later overrides the privous config.
47 func NewLoggerFromConfigString(s string) Logger {
52 methods := strings.Split(s, ",")
53 for _, method := range methods {
54 if err := l.fillMethodLoggerWithConfigString(method); err != nil {
55 grpclog.Warningf("failed to parse binary log config: %v", err)
62 // fillMethodLoggerWithConfigString parses config, creates methodLogger and adds
63 // it to the right map in the logger.
64 func (l *logger) fillMethodLoggerWithConfigString(config string) error {
67 return errors.New("empty string is not a valid method binary logging config")
70 // "-service/method", blacklist, no * or {} allowed.
72 s, m, suffix, err := parseMethodConfigAndSuffix(config[1:])
74 return fmt.Errorf("invalid config: %q, %v", config, err)
77 return fmt.Errorf("invalid config: %q, %v", config, "* not allowd in blacklist config")
80 return fmt.Errorf("invalid config: %q, %v", config, "header/message limit not allowed in blacklist config")
82 if err := l.setBlacklist(s + "/" + m); err != nil {
83 return fmt.Errorf("invalid config: %v", err)
90 hdr, msg, err := parseHeaderMessageLengthConfig(config[1:])
92 return fmt.Errorf("invalid config: %q, %v", config, err)
94 if err := l.setDefaultMethodLogger(&methodLoggerConfig{hdr: hdr, msg: msg}); err != nil {
95 return fmt.Errorf("invalid config: %v", err)
100 s, m, suffix, err := parseMethodConfigAndSuffix(config)
102 return fmt.Errorf("invalid config: %q, %v", config, err)
104 hdr, msg, err := parseHeaderMessageLengthConfig(suffix)
106 return fmt.Errorf("invalid header/message length config: %q, %v", suffix, err)
109 if err := l.setServiceMethodLogger(s, &methodLoggerConfig{hdr: hdr, msg: msg}); err != nil {
110 return fmt.Errorf("invalid config: %v", err)
113 if err := l.setMethodMethodLogger(s+"/"+m, &methodLoggerConfig{hdr: hdr, msg: msg}); err != nil {
114 return fmt.Errorf("invalid config: %v", err)
121 // TODO: this const is only used by env_config now. But could be useful for
122 // other config. Move to binarylog.go if necessary.
125 // For "p.s/m" plus any suffix. Suffix will be parsed again. See test for
127 longMethodConfigRegexpStr = `^([\w./]+)/((?:\w+)|[*])(.+)?$`
129 // For suffix from above, "{h:123,m:123}". See test for expected output.
130 optionalLengthRegexpStr = `(?::(\d+))?` // Optional ":123".
131 headerConfigRegexpStr = `^{h` + optionalLengthRegexpStr + `}$`
132 messageConfigRegexpStr = `^{m` + optionalLengthRegexpStr + `}$`
133 headerMessageConfigRegexpStr = `^{h` + optionalLengthRegexpStr + `;m` + optionalLengthRegexpStr + `}$`
137 longMethodConfigRegexp = regexp.MustCompile(longMethodConfigRegexpStr)
138 headerConfigRegexp = regexp.MustCompile(headerConfigRegexpStr)
139 messageConfigRegexp = regexp.MustCompile(messageConfigRegexpStr)
140 headerMessageConfigRegexp = regexp.MustCompile(headerMessageConfigRegexpStr)
143 // Turn "service/method{h;m}" into "service", "method", "{h;m}".
144 func parseMethodConfigAndSuffix(c string) (service, method, suffix string, _ error) {
147 // in: "p.s/m{h:123,m:123}",
148 // out: []string{"p.s/m{h:123,m:123}", "p.s", "m", "{h:123,m:123}"},
149 match := longMethodConfigRegexp.FindStringSubmatch(c)
151 return "", "", "", fmt.Errorf("%q contains invalid substring", c)
159 // Turn "{h:123;m:345}" into 123, 345.
161 // Return maxUInt if length is unspecified.
162 func parseHeaderMessageLengthConfig(c string) (hdrLenStr, msgLenStr uint64, err error) {
164 return maxUInt, maxUInt, nil
166 // Header config only.
167 if match := headerConfigRegexp.FindStringSubmatch(c); match != nil {
168 if s := match[1]; s != "" {
169 hdrLenStr, err = strconv.ParseUint(s, 10, 64)
171 return 0, 0, fmt.Errorf("failed to convert %q to uint", s)
173 return hdrLenStr, 0, nil
175 return maxUInt, 0, nil
178 // Message config only.
179 if match := messageConfigRegexp.FindStringSubmatch(c); match != nil {
180 if s := match[1]; s != "" {
181 msgLenStr, err = strconv.ParseUint(s, 10, 64)
183 return 0, 0, fmt.Errorf("Failed to convert %q to uint", s)
185 return 0, msgLenStr, nil
187 return 0, maxUInt, nil
190 // Header and message config both.
191 if match := headerMessageConfigRegexp.FindStringSubmatch(c); match != nil {
192 // Both hdr and msg are specified, but one or two of them might be empty.
195 if s := match[1]; s != "" {
196 hdrLenStr, err = strconv.ParseUint(s, 10, 64)
198 return 0, 0, fmt.Errorf("Failed to convert %q to uint", s)
201 if s := match[2]; s != "" {
202 msgLenStr, err = strconv.ParseUint(s, 10, 64)
204 return 0, 0, fmt.Errorf("Failed to convert %q to uint", s)
207 return hdrLenStr, msgLenStr, nil
209 return 0, 0, fmt.Errorf("%q contains invalid substring", c)