-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathlog.go
190 lines (166 loc) · 5.03 KB
/
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
// 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"
"regexp"
"strings"
"time"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/syncutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"google.golang.org/grpc/grpclog"
)
func init() {
SetSeverity(log.Severity_ERROR)
}
// SetSeverity sets the severity level below which GRPC log messages are
// suppressed.
//
// Must be called before GRPC is used.
func SetSeverity(s log.Severity) {
grpclog.SetLoggerV2((*logger)(&s))
}
// 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.
//
// Also NB: we pass a depth of 2 here because all logging calls originate from
// the logging adapter file in grpc, which is an additional stack frame away
// from the actual logging site.
var _ grpclog.LoggerV2 = (*logger)(nil)
type logger log.Severity
func (severity *logger) Info(args ...interface{}) {
if log.Severity(*severity) > log.Severity_INFO {
return
}
log.InfofDepth(context.TODO(), 2, "", args...)
}
func (severity *logger) Infoln(args ...interface{}) {
if log.Severity(*severity) > log.Severity_INFO {
return
}
log.InfofDepth(context.TODO(), 2, "", args...)
}
func (severity *logger) Infof(format string, args ...interface{}) {
if log.Severity(*severity) > log.Severity_INFO {
return
}
log.InfofDepth(context.TODO(), 2, format, args...)
}
func (severity *logger) Warning(args ...interface{}) {
if log.Severity(*severity) > log.Severity_WARNING {
return
}
if shouldPrint(ConnectivitySpamRe, 30*time.Second, args...) {
log.WarningfDepth(context.TODO(), 2, "", args...)
}
}
func (severity *logger) Warningln(args ...interface{}) {
if log.Severity(*severity) > log.Severity_WARNING {
return
}
log.WarningfDepth(context.TODO(), 2, "", args...)
}
func (severity *logger) Warningf(format string, args ...interface{}) {
if log.Severity(*severity) > log.Severity_WARNING {
return
}
log.WarningfDepth(context.TODO(), 2, format, args...)
}
func (severity *logger) Error(args ...interface{}) {
if log.Severity(*severity) > log.Severity_ERROR {
return
}
log.ErrorfDepth(context.TODO(), 2, "", args...)
}
func (severity *logger) Errorln(args ...interface{}) {
if log.Severity(*severity) > log.Severity_ERROR {
return
}
log.ErrorfDepth(context.TODO(), 2, "", args...)
}
func (severity *logger) Errorf(format string, args ...interface{}) {
if log.Severity(*severity) > log.Severity_ERROR {
return
}
log.ErrorfDepth(context.TODO(), 2, format, args...)
}
func (severity *logger) Fatal(args ...interface{}) {
if log.Severity(*severity) > log.Severity_NONE {
return
}
log.FatalfDepth(context.TODO(), 2, "", args...)
}
func (severity *logger) Fatalln(args ...interface{}) {
if log.Severity(*severity) > log.Severity_NONE {
return
}
log.FatalfDepth(context.TODO(), 2, "", args...)
}
func (severity *logger) Fatalf(format string, args ...interface{}) {
if log.Severity(*severity) > log.Severity_NONE {
return
}
log.FatalfDepth(context.TODO(), 2, format, args...)
}
func (severity *logger) V(i int) bool {
if i < 0 {
i = 0
}
if i > math.MaxInt32 {
i = math.MaxInt32
}
return log.VDepth(log.Level(i) /* level */, 1 /* depth */)
}
// https://github.com/grpc/grpc-go/blob/v1.29.1/clientconn.go#L1275
var (
transportFailedRe = regexp.MustCompile(`^` + regexp.QuoteMeta(`grpc: addrConn.createTransport failed to connect to`))
ConnectionRefusedRe = regexp.MustCompile(
strings.Join([]string{
// *nix
regexp.QuoteMeta("connection refused"),
// Windows
regexp.QuoteMeta("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
regexp.QuoteMeta("no such host"),
}, "|"),
)
clientConnReuseRe = regexp.MustCompile("cannot reuse client connection")
ConnectivitySpamRe = regexp.MustCompile(transportFailedRe.String() + `.*` +
"(" + ConnectionRefusedRe.String() + "|" + clientConnReuseRe.String() + ")")
)
var spamMu = struct {
syncutil.Mutex
strs map[string]time.Time
}{
strs: make(map[string]time.Time),
}
func shouldPrint(argsRe *regexp.Regexp, freq time.Duration, args ...interface{}) bool {
for _, arg := range args {
if argStr, ok := arg.(string); ok {
if argsRe.MatchString(argStr) {
now := timeutil.Now()
spamMu.Lock()
t, ok := spamMu.strs[argStr]
doPrint := !(ok && now.Sub(t) < freq)
if doPrint {
spamMu.strs[argStr] = now
}
spamMu.Unlock()
return doPrint
}
}
}
return true
}