blob: 755fdebc1b15aaa075397e6b59a22e303fc41a01 [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001/*
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
19// Package binarylog implementation binary logging as defined in
20// https://github.com/grpc/proposal/blob/master/A16-binary-logging.md.
21package binarylog
22
23import (
24 "fmt"
25 "os"
26
27 "google.golang.org/grpc/grpclog"
Abhay Kumara2ae5992025-11-10 14:02:24 +000028 "google.golang.org/grpc/internal/grpcutil"
khenaidooac637102019-01-14 15:44:34 -050029)
30
Abhay Kumara2ae5992025-11-10 14:02:24 +000031var grpclogLogger = grpclog.Component("binarylog")
32
33// Logger specifies MethodLoggers for method names with a Log call that
34// takes a context.
35//
36// This is used in the 1.0 release of gcp/observability, and thus must not be
37// deleted or changed.
khenaidooac637102019-01-14 15:44:34 -050038type Logger interface {
Abhay Kumara2ae5992025-11-10 14:02:24 +000039 GetMethodLogger(methodName string) MethodLogger
khenaidooac637102019-01-14 15:44:34 -050040}
41
42// binLogger is the global binary logger for the binary. One of this should be
Andrea Campanella3614a922021-02-25 12:40:42 +010043// built at init time from the configuration (environment variable or flags).
khenaidooac637102019-01-14 15:44:34 -050044//
Abhay Kumara2ae5992025-11-10 14:02:24 +000045// It is used to get a MethodLogger for each individual method.
khenaidooac637102019-01-14 15:44:34 -050046var binLogger Logger
47
Abhay Kumara2ae5992025-11-10 14:02:24 +000048// SetLogger sets the binary logger.
khenaidooac637102019-01-14 15:44:34 -050049//
50// Only call this at init time.
51func SetLogger(l Logger) {
52 binLogger = l
53}
54
Abhay Kumara2ae5992025-11-10 14:02:24 +000055// GetLogger gets the binary logger.
56//
57// Only call this at init time.
58func GetLogger() Logger {
59 return binLogger
60}
61
62// GetMethodLogger returns the MethodLogger for the given methodName.
khenaidooac637102019-01-14 15:44:34 -050063//
64// methodName should be in the format of "/service/method".
65//
Abhay Kumara2ae5992025-11-10 14:02:24 +000066// Each MethodLogger returned by this method is a new instance. This is to
khenaidooac637102019-01-14 15:44:34 -050067// generate sequence id within the call.
Abhay Kumara2ae5992025-11-10 14:02:24 +000068func GetMethodLogger(methodName string) MethodLogger {
khenaidooac637102019-01-14 15:44:34 -050069 if binLogger == nil {
70 return nil
71 }
Abhay Kumara2ae5992025-11-10 14:02:24 +000072 return binLogger.GetMethodLogger(methodName)
khenaidooac637102019-01-14 15:44:34 -050073}
74
75func init() {
76 const envStr = "GRPC_BINARY_LOG_FILTER"
77 configStr := os.Getenv(envStr)
78 binLogger = NewLoggerFromConfigString(configStr)
79}
80
Abhay Kumara2ae5992025-11-10 14:02:24 +000081// MethodLoggerConfig contains the setting for logging behavior of a method
82// logger. Currently, it contains the max length of header and message.
83type MethodLoggerConfig struct {
khenaidooac637102019-01-14 15:44:34 -050084 // Max length of header and message.
Abhay Kumara2ae5992025-11-10 14:02:24 +000085 Header, Message uint64
86}
87
88// LoggerConfig contains the config for loggers to create method loggers.
89type LoggerConfig struct {
90 All *MethodLoggerConfig
91 Services map[string]*MethodLoggerConfig
92 Methods map[string]*MethodLoggerConfig
93
94 Blacklist map[string]struct{}
khenaidooac637102019-01-14 15:44:34 -050095}
96
97type logger struct {
Abhay Kumara2ae5992025-11-10 14:02:24 +000098 config LoggerConfig
99}
khenaidooac637102019-01-14 15:44:34 -0500100
Abhay Kumara2ae5992025-11-10 14:02:24 +0000101// NewLoggerFromConfig builds a logger with the given LoggerConfig.
102func NewLoggerFromConfig(config LoggerConfig) Logger {
103 return &logger{config: config}
khenaidooac637102019-01-14 15:44:34 -0500104}
105
106// newEmptyLogger creates an empty logger. The map fields need to be filled in
107// using the set* functions.
108func newEmptyLogger() *logger {
109 return &logger{}
110}
111
112// Set method logger for "*".
Abhay Kumara2ae5992025-11-10 14:02:24 +0000113func (l *logger) setDefaultMethodLogger(ml *MethodLoggerConfig) error {
114 if l.config.All != nil {
khenaidooac637102019-01-14 15:44:34 -0500115 return fmt.Errorf("conflicting global rules found")
116 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000117 l.config.All = ml
khenaidooac637102019-01-14 15:44:34 -0500118 return nil
119}
120
121// Set method logger for "service/*".
122//
Abhay Kumara2ae5992025-11-10 14:02:24 +0000123// New MethodLogger with same service overrides the old one.
124func (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)
khenaidooac637102019-01-14 15:44:34 -0500127 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000128 if l.config.Services == nil {
129 l.config.Services = make(map[string]*MethodLoggerConfig)
khenaidooac637102019-01-14 15:44:34 -0500130 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000131 l.config.Services[service] = ml
khenaidooac637102019-01-14 15:44:34 -0500132 return nil
133}
134
135// Set method logger for "service/method".
136//
Abhay Kumara2ae5992025-11-10 14:02:24 +0000137// New MethodLogger with same method overrides the old one.
138func (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)
khenaidooac637102019-01-14 15:44:34 -0500141 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000142 if _, ok := l.config.Methods[method]; ok {
143 return fmt.Errorf("conflicting method rules for method %v found", method)
khenaidooac637102019-01-14 15:44:34 -0500144 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000145 if l.config.Methods == nil {
146 l.config.Methods = make(map[string]*MethodLoggerConfig)
khenaidooac637102019-01-14 15:44:34 -0500147 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000148 l.config.Methods[method] = ml
khenaidooac637102019-01-14 15:44:34 -0500149 return nil
150}
151
152// Set blacklist method for "-service/method".
153func (l *logger) setBlacklist(method string) error {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000154 if _, ok := l.config.Blacklist[method]; ok {
155 return fmt.Errorf("conflicting blacklist rules for method %v found", method)
khenaidooac637102019-01-14 15:44:34 -0500156 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000157 if _, ok := l.config.Methods[method]; ok {
158 return fmt.Errorf("conflicting method rules for method %v found", method)
khenaidooac637102019-01-14 15:44:34 -0500159 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000160 if l.config.Blacklist == nil {
161 l.config.Blacklist = make(map[string]struct{})
khenaidooac637102019-01-14 15:44:34 -0500162 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000163 l.config.Blacklist[method] = struct{}{}
khenaidooac637102019-01-14 15:44:34 -0500164 return nil
165}
166
Abhay Kumara2ae5992025-11-10 14:02:24 +0000167// getMethodLogger returns the MethodLogger for the given methodName.
khenaidooac637102019-01-14 15:44:34 -0500168//
169// methodName should be in the format of "/service/method".
170//
Abhay Kumara2ae5992025-11-10 14:02:24 +0000171// Each MethodLogger returned by this method is a new instance. This is to
khenaidooac637102019-01-14 15:44:34 -0500172// generate sequence id within the call.
Abhay Kumara2ae5992025-11-10 14:02:24 +0000173func (l *logger) GetMethodLogger(methodName string) MethodLogger {
174 s, m, err := grpcutil.ParseMethod(methodName)
khenaidooac637102019-01-14 15:44:34 -0500175 if err != nil {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000176 grpclogLogger.Infof("binarylogging: failed to parse %q: %v", methodName, err)
khenaidooac637102019-01-14 15:44:34 -0500177 return nil
178 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000179 if ml, ok := l.config.Methods[s+"/"+m]; ok {
180 return NewTruncatingMethodLogger(ml.Header, ml.Message)
khenaidooac637102019-01-14 15:44:34 -0500181 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000182 if _, ok := l.config.Blacklist[s+"/"+m]; ok {
khenaidooac637102019-01-14 15:44:34 -0500183 return nil
184 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000185 if ml, ok := l.config.Services[s]; ok {
186 return NewTruncatingMethodLogger(ml.Header, ml.Message)
khenaidooac637102019-01-14 15:44:34 -0500187 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000188 if l.config.All == nil {
khenaidooac637102019-01-14 15:44:34 -0500189 return nil
190 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000191 return NewTruncatingMethodLogger(l.config.All.Header, l.config.All.Message)
khenaidooac637102019-01-14 15:44:34 -0500192}