-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathgrpc_log.go
285 lines (250 loc) · 7.53 KB
/
grpc_log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2016 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package grpcutil
import (
"context"
"math"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/log/severity"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"google.golang.org/grpc/grpclog"
)
func init() {
LowerSeverity(severity.ERROR)
}
// LowerSeverity ensures that the severity level below which GRPC
// log messages are suppressed is at most the specified severity.
//
// Prior to the first call to this method, this cutoff is ERROR,
// or whatever is specified via the GRPC_GO_LOG_SEVERITY_LEVEL
// environment variable.
//
// Must be called before GRPC is used.
func LowerSeverity(s log.Severity) {
logger := getGRPCLogger(
s,
os.Getenv("GRPC_GO_LOG_SEVERITY_LEVEL"),
os.Getenv("GRPC_GO_LOG_VERBOSITY_LEVEL"),
)
grpclog.SetLoggerV2(logger)
}
func getGRPCLogger(inputSeverity log.Severity, envSev, envVer string) *grpcLogger {
{
// If the env var specifies a lower severity than the input,
// lower the input accordingly.
var s log.Severity
switch e := strings.ToLower(envSev); e {
case "info":
s = severity.INFO
case "warning":
s = severity.WARNING
case "error":
s = severity.ERROR
case "":
s = inputSeverity
default:
panic("unsupported GRPC_GO_LOG_SEVERITY_LEVEL: " + e)
}
if s < inputSeverity {
inputSeverity = s
}
}
var vl int
if envVer != "" {
vli, err := strconv.ParseInt(envVer, 10, 32)
if err != nil {
panic("invalid GRPC_GO_LOG_VERBOSITY_LEVEL " + envVer + ": " + err.Error())
}
if vl < 0 {
vli = 0
}
if vli > math.MaxInt32 {
vli = math.MaxInt32
}
vl = int(vli)
}
return &grpcLogger{
sev: inputSeverity, grpcVerbosityLevel: vl,
}
}
// NB: This interface is implemented by a pointer because using a value causes
// a synthetic stack frame to be inserted on calls to the interface methods.
// Specifically, we get a stack frame that appears as "<autogenerated>", which
// is not useful in logs.
var _ grpclog.LoggerV2 = (*grpcLogger)(nil)
// We use a depth of 2 throughout this file because all logging
// calls originate from the logging adapter file in grpc, which is
// an additional stack frame away from the actual logging site.
const depth2 = 2
type grpcLogger struct {
sev log.Severity
grpcVerbosityLevel int
}
func (l *grpcLogger) vDepth(i int, depth int) bool {
if i < 0 {
i = 0
}
if i > math.MaxInt32 {
i = math.MaxInt32
}
// If GRPC_GO_LOG_VERBOSITY_LEVEL is less restrictive, it prevails.
if i <= l.grpcVerbosityLevel {
return true
}
// Otherwise, our logger decides.
return log.VDepth(log.Level(i) /* level */, depth+1)
}
func (l *grpcLogger) shouldLog(incomingSeverity log.Severity, depth int) bool {
// If the incoming severity is at or above threshold, log.
if l.sev <= incomingSeverity {
return true
}
// If verbose logging is on at all (either for our
// logger or via the grpc env var), log all severities.
return l.vDepth(1, depth+1)
}
func (l *grpcLogger) Info(args ...interface{}) {
if !l.shouldLog(severity.INFO, depth2) {
return
}
log.InfofDepth(context.TODO(), depth2, "", args...)
}
func (l *grpcLogger) Infoln(args ...interface{}) {
if !l.shouldLog(severity.INFO, depth2) {
return
}
log.InfofDepth(context.TODO(), depth2, "", args...)
}
func (l *grpcLogger) Infof(format string, args ...interface{}) {
if !l.shouldLog(severity.INFO, depth2) {
return
}
log.InfofDepth(context.TODO(), depth2, format, args...)
}
func (l *grpcLogger) Warning(args ...interface{}) {
if !l.shouldLog(severity.WARNING, depth2) {
return
}
if !l.shouldPrintWarning(depth2, args...) {
return
}
log.WarningfDepth(context.TODO(), depth2, "", args...)
}
func (l *grpcLogger) Warningln(args ...interface{}) {
if !l.shouldLog(severity.WARNING, depth2) {
return
}
log.WarningfDepth(context.TODO(), depth2, "", args...)
}
func (l *grpcLogger) Warningf(format string, args ...interface{}) {
if !l.shouldLog(severity.WARNING, depth2) {
return
}
log.WarningfDepth(context.TODO(), depth2, format, args...)
}
func (l *grpcLogger) Error(args ...interface{}) {
if !l.shouldLog(severity.ERROR, depth2) {
return
}
log.ErrorfDepth(context.TODO(), depth2, "", args...)
}
func (l *grpcLogger) Errorln(args ...interface{}) {
if !l.shouldLog(severity.ERROR, depth2) {
return
}
log.ErrorfDepth(context.TODO(), depth2, "", args...)
}
func (l *grpcLogger) Errorf(format string, args ...interface{}) {
if !l.shouldLog(severity.ERROR, depth2) {
return
}
log.ErrorfDepth(context.TODO(), depth2, format, args...)
}
func (l *grpcLogger) Fatal(args ...interface{}) {
log.FatalfDepth(context.TODO(), depth2, "", args...)
}
func (l *grpcLogger) Fatalln(args ...interface{}) {
log.FatalfDepth(context.TODO(), depth2, "", args...)
}
func (l *grpcLogger) Fatalf(format string, args ...interface{}) {
log.FatalfDepth(context.TODO(), depth2, format, args...)
}
func (l *grpcLogger) V(i int) bool {
return l.vDepth(i, depth2)
}
// https://github.com/grpc/grpc-go/blob/v1.29.1/clientconn.go#L1275
const (
outgoingConnSpamReSrc = `^grpc: addrConn\.createTransport failed to connect to.*(` +
// *nix
`connection refused` + `|` +
// Windows
`No connection could be made because the target machine actively refused it` + `|` +
// Host removed from the network and no longer resolvable:
// https://github.com/golang/go/blob/go1.8.3/src/net/net.go#L566
`no such host` + `|` +
// RPC dialer is currently failing but also retrying.
errCannotReuseClientConnMsg +
`)`
// When a TCP probe simply opens and immediately closes the
// connection, gRPC is unhappy that the TLS handshake did not
// complete. We don't care.
incomingConnSpamReSrc = `^grpc: Server\.serve failed to complete security handshake from "[^"]*": EOF`
)
var outgoingConnSpamRe = regexp.MustCompile(outgoingConnSpamReSrc)
var incomingConnSpamRe = regexp.MustCompile(incomingConnSpamReSrc)
var spamMu = struct {
syncutil.Mutex
strs map[string]time.Time
}{
strs: make(map[string]time.Time),
}
const minSpamLogInterval = 30 * time.Second
// shouldPrintWarning returns true iff the gRPC warning message
// identified by args can be logged now. It returns false for outgoing
// connections errors that repeat within minSpamLogInterval of each
// other, and also for certain types of incoming connection errors.
func (l *grpcLogger) shouldPrintWarning(depth int, args ...interface{}) bool {
if l.vDepth(1, depth+1) {
// When verbose logging is on at all, don't
// suppress any warnings.
return true
}
for _, arg := range args {
if argStr, ok := arg.(string); ok {
// Incoming connection errors that match the criteria are blocked
// always.
if incomingConnSpamRe.MatchString(argStr) {
return false
}
// Outgoing connection errors are only reported if performed too
// often.
// TODO(knz): Maybe the string map should be cleared periodically,
// to avoid unbounded memory growth.
if outgoingConnSpamRe.MatchString(argStr) {
now := timeutil.Now()
spamMu.Lock()
t, ok := spamMu.strs[argStr]
doPrint := !(ok && now.Sub(t) < minSpamLogInterval)
if doPrint {
spamMu.strs[argStr] = now
}
spamMu.Unlock()
return doPrint
}
}
}
return true
}