binarylog.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  16. *
  17. */
  18. // Package binarylog implementation binary logging as defined in
  19. // https://github.com/grpc/proposal/blob/master/A16-binary-logging.md.
  20. package binarylog
  21. import (
  22. "fmt"
  23. "os"
  24. "google.golang.org/grpc/grpclog"
  25. "google.golang.org/grpc/internal/grpcutil"
  26. )
  27. // Logger is the global binary logger. It can be used to get binary logger for
  28. // each method.
  29. type Logger interface {
  30. GetMethodLogger(methodName string) MethodLogger
  31. }
  32. // binLogger is the global binary logger for the binary. One of this should be
  33. // built at init time from the configuration (environment variable or flags).
  34. //
  35. // It is used to get a methodLogger for each individual method.
  36. var binLogger Logger
  37. var grpclogLogger = grpclog.Component("binarylog")
  38. // SetLogger sets the binarg logger.
  39. //
  40. // Only call this at init time.
  41. func SetLogger(l Logger) {
  42. binLogger = l
  43. }
  44. // GetLogger gets the binarg logger.
  45. //
  46. // Only call this at init time.
  47. func GetLogger() Logger {
  48. return binLogger
  49. }
  50. // GetMethodLogger returns the methodLogger for the given methodName.
  51. //
  52. // methodName should be in the format of "/service/method".
  53. //
  54. // Each methodLogger returned by this method is a new instance. This is to
  55. // generate sequence id within the call.
  56. func GetMethodLogger(methodName string) MethodLogger {
  57. if binLogger == nil {
  58. return nil
  59. }
  60. return binLogger.GetMethodLogger(methodName)
  61. }
  62. func init() {
  63. const envStr = "GRPC_BINARY_LOG_FILTER"
  64. configStr := os.Getenv(envStr)
  65. binLogger = NewLoggerFromConfigString(configStr)
  66. }
  67. // MethodLoggerConfig contains the setting for logging behavior of a method
  68. // logger. Currently, it contains the max length of header and message.
  69. type MethodLoggerConfig struct {
  70. // Max length of header and message.
  71. Header, Message uint64
  72. }
  73. // LoggerConfig contains the config for loggers to create method loggers.
  74. type LoggerConfig struct {
  75. All *MethodLoggerConfig
  76. Services map[string]*MethodLoggerConfig
  77. Methods map[string]*MethodLoggerConfig
  78. Blacklist map[string]struct{}
  79. }
  80. type logger struct {
  81. config LoggerConfig
  82. }
  83. // NewLoggerFromConfig builds a logger with the given LoggerConfig.
  84. func NewLoggerFromConfig(config LoggerConfig) Logger {
  85. return &logger{config: config}
  86. }
  87. // newEmptyLogger creates an empty logger. The map fields need to be filled in
  88. // using the set* functions.
  89. func newEmptyLogger() *logger {
  90. return &logger{}
  91. }
  92. // Set method logger for "*".
  93. func (l *logger) setDefaultMethodLogger(ml *MethodLoggerConfig) error {
  94. if l.config.All != nil {
  95. return fmt.Errorf("conflicting global rules found")
  96. }
  97. l.config.All = ml
  98. return nil
  99. }
  100. // Set method logger for "service/*".
  101. //
  102. // New methodLogger with same service overrides the old one.
  103. func (l *logger) setServiceMethodLogger(service string, ml *MethodLoggerConfig) error {
  104. if _, ok := l.config.Services[service]; ok {
  105. return fmt.Errorf("conflicting service rules for service %v found", service)
  106. }
  107. if l.config.Services == nil {
  108. l.config.Services = make(map[string]*MethodLoggerConfig)
  109. }
  110. l.config.Services[service] = ml
  111. return nil
  112. }
  113. // Set method logger for "service/method".
  114. //
  115. // New methodLogger with same method overrides the old one.
  116. func (l *logger) setMethodMethodLogger(method string, ml *MethodLoggerConfig) error {
  117. if _, ok := l.config.Blacklist[method]; ok {
  118. return fmt.Errorf("conflicting blacklist rules for method %v found", method)
  119. }
  120. if _, ok := l.config.Methods[method]; ok {
  121. return fmt.Errorf("conflicting method rules for method %v found", method)
  122. }
  123. if l.config.Methods == nil {
  124. l.config.Methods = make(map[string]*MethodLoggerConfig)
  125. }
  126. l.config.Methods[method] = ml
  127. return nil
  128. }
  129. // Set blacklist method for "-service/method".
  130. func (l *logger) setBlacklist(method string) error {
  131. if _, ok := l.config.Blacklist[method]; ok {
  132. return fmt.Errorf("conflicting blacklist rules for method %v found", method)
  133. }
  134. if _, ok := l.config.Methods[method]; ok {
  135. return fmt.Errorf("conflicting method rules for method %v found", method)
  136. }
  137. if l.config.Blacklist == nil {
  138. l.config.Blacklist = make(map[string]struct{})
  139. }
  140. l.config.Blacklist[method] = struct{}{}
  141. return nil
  142. }
  143. // getMethodLogger returns the methodLogger for the given methodName.
  144. //
  145. // methodName should be in the format of "/service/method".
  146. //
  147. // Each methodLogger returned by this method is a new instance. This is to
  148. // generate sequence id within the call.
  149. func (l *logger) GetMethodLogger(methodName string) MethodLogger {
  150. s, m, err := grpcutil.ParseMethod(methodName)
  151. if err != nil {
  152. grpclogLogger.Infof("binarylogging: failed to parse %q: %v", methodName, err)
  153. return nil
  154. }
  155. if ml, ok := l.config.Methods[s+"/"+m]; ok {
  156. return newMethodLogger(ml.Header, ml.Message)
  157. }
  158. if _, ok := l.config.Blacklist[s+"/"+m]; ok {
  159. return nil
  160. }
  161. if ml, ok := l.config.Services[s]; ok {
  162. return newMethodLogger(ml.Header, ml.Message)
  163. }
  164. if l.config.All == nil {
  165. return nil
  166. }
  167. return newMethodLogger(l.config.All.Header, l.config.All.Message)
  168. }