blob: 755fdebc1b15aaa075397e6b59a22e303fc41a01 [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -04001/*
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"
28 "google.golang.org/grpc/internal/grpcutil"
29)
30
Akash Kankanala761955c2024-02-21 19:32:20 +053031var 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.
khenaidoo5fc5cea2021-08-11 17:39:16 -040038type Logger interface {
Akash Kankanala761955c2024-02-21 19:32:20 +053039 GetMethodLogger(methodName string) MethodLogger
khenaidoo5fc5cea2021-08-11 17:39:16 -040040}
41
42// binLogger is the global binary logger for the binary. One of this should be
43// built at init time from the configuration (environment variable or flags).
44//
Akash Kankanala761955c2024-02-21 19:32:20 +053045// It is used to get a MethodLogger for each individual method.
khenaidoo5fc5cea2021-08-11 17:39:16 -040046var binLogger Logger
47
Akash Kankanala761955c2024-02-21 19:32:20 +053048// SetLogger sets the binary logger.
khenaidoo5fc5cea2021-08-11 17:39:16 -040049//
50// Only call this at init time.
51func SetLogger(l Logger) {
52 binLogger = l
53}
54
Akash Kankanala761955c2024-02-21 19:32:20 +053055// 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.
khenaidoo5fc5cea2021-08-11 17:39:16 -040063//
64// methodName should be in the format of "/service/method".
65//
Akash Kankanala761955c2024-02-21 19:32:20 +053066// Each MethodLogger returned by this method is a new instance. This is to
khenaidoo5fc5cea2021-08-11 17:39:16 -040067// generate sequence id within the call.
Akash Kankanala761955c2024-02-21 19:32:20 +053068func GetMethodLogger(methodName string) MethodLogger {
khenaidoo5fc5cea2021-08-11 17:39:16 -040069 if binLogger == nil {
70 return nil
71 }
Akash Kankanala761955c2024-02-21 19:32:20 +053072 return binLogger.GetMethodLogger(methodName)
khenaidoo5fc5cea2021-08-11 17:39:16 -040073}
74
75func init() {
76 const envStr = "GRPC_BINARY_LOG_FILTER"
77 configStr := os.Getenv(envStr)
78 binLogger = NewLoggerFromConfigString(configStr)
79}
80
Akash Kankanala761955c2024-02-21 19:32:20 +053081// 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 {
khenaidoo5fc5cea2021-08-11 17:39:16 -040084 // Max length of header and message.
Akash Kankanala761955c2024-02-21 19:32:20 +053085 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{}
khenaidoo5fc5cea2021-08-11 17:39:16 -040095}
96
97type logger struct {
Akash Kankanala761955c2024-02-21 19:32:20 +053098 config LoggerConfig
99}
khenaidoo5fc5cea2021-08-11 17:39:16 -0400100
Akash Kankanala761955c2024-02-21 19:32:20 +0530101// NewLoggerFromConfig builds a logger with the given LoggerConfig.
102func NewLoggerFromConfig(config LoggerConfig) Logger {
103 return &logger{config: config}
khenaidoo5fc5cea2021-08-11 17:39:16 -0400104}
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 "*".
Akash Kankanala761955c2024-02-21 19:32:20 +0530113func (l *logger) setDefaultMethodLogger(ml *MethodLoggerConfig) error {
114 if l.config.All != nil {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400115 return fmt.Errorf("conflicting global rules found")
116 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530117 l.config.All = ml
khenaidoo5fc5cea2021-08-11 17:39:16 -0400118 return nil
119}
120
121// Set method logger for "service/*".
122//
Akash Kankanala761955c2024-02-21 19:32:20 +0530123// 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 {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400126 return fmt.Errorf("conflicting service rules for service %v found", service)
127 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530128 if l.config.Services == nil {
129 l.config.Services = make(map[string]*MethodLoggerConfig)
khenaidoo5fc5cea2021-08-11 17:39:16 -0400130 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530131 l.config.Services[service] = ml
khenaidoo5fc5cea2021-08-11 17:39:16 -0400132 return nil
133}
134
135// Set method logger for "service/method".
136//
Akash Kankanala761955c2024-02-21 19:32:20 +0530137// 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 {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400140 return fmt.Errorf("conflicting blacklist rules for method %v found", method)
141 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530142 if _, ok := l.config.Methods[method]; ok {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400143 return fmt.Errorf("conflicting method rules for method %v found", method)
144 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530145 if l.config.Methods == nil {
146 l.config.Methods = make(map[string]*MethodLoggerConfig)
khenaidoo5fc5cea2021-08-11 17:39:16 -0400147 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530148 l.config.Methods[method] = ml
khenaidoo5fc5cea2021-08-11 17:39:16 -0400149 return nil
150}
151
152// Set blacklist method for "-service/method".
153func (l *logger) setBlacklist(method string) error {
Akash Kankanala761955c2024-02-21 19:32:20 +0530154 if _, ok := l.config.Blacklist[method]; ok {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400155 return fmt.Errorf("conflicting blacklist rules for method %v found", method)
156 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530157 if _, ok := l.config.Methods[method]; ok {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400158 return fmt.Errorf("conflicting method rules for method %v found", method)
159 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530160 if l.config.Blacklist == nil {
161 l.config.Blacklist = make(map[string]struct{})
khenaidoo5fc5cea2021-08-11 17:39:16 -0400162 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530163 l.config.Blacklist[method] = struct{}{}
khenaidoo5fc5cea2021-08-11 17:39:16 -0400164 return nil
165}
166
Akash Kankanala761955c2024-02-21 19:32:20 +0530167// getMethodLogger returns the MethodLogger for the given methodName.
khenaidoo5fc5cea2021-08-11 17:39:16 -0400168//
169// methodName should be in the format of "/service/method".
170//
Akash Kankanala761955c2024-02-21 19:32:20 +0530171// Each MethodLogger returned by this method is a new instance. This is to
khenaidoo5fc5cea2021-08-11 17:39:16 -0400172// generate sequence id within the call.
Akash Kankanala761955c2024-02-21 19:32:20 +0530173func (l *logger) GetMethodLogger(methodName string) MethodLogger {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400174 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 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530179 if ml, ok := l.config.Methods[s+"/"+m]; ok {
180 return NewTruncatingMethodLogger(ml.Header, ml.Message)
khenaidoo5fc5cea2021-08-11 17:39:16 -0400181 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530182 if _, ok := l.config.Blacklist[s+"/"+m]; ok {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400183 return nil
184 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530185 if ml, ok := l.config.Services[s]; ok {
186 return NewTruncatingMethodLogger(ml.Header, ml.Message)
khenaidoo5fc5cea2021-08-11 17:39:16 -0400187 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530188 if l.config.All == nil {
khenaidoo5fc5cea2021-08-11 17:39:16 -0400189 return nil
190 }
Akash Kankanala761955c2024-02-21 19:32:20 +0530191 return NewTruncatingMethodLogger(l.config.All.Header, l.config.All.Message)
khenaidoo5fc5cea2021-08-11 17:39:16 -0400192}