-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
318 lines (274 loc) · 8.53 KB
/
logger.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package logger
import (
"context"
"errors"
"fmt"
"log"
"runtime"
"runtime/debug"
"strings"
"syscall"
_ "github.com/KimMachineGun/automemlimit"
"go.uber.org/automaxprocs/maxprocs"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
opentracing "github.com/opentracing/opentracing-go"
)
var (
Plain *zap.Logger
Sugar *WrappedLogger
undoLogger func()
undoMaxProcs func()
Recorded *observer.ObservedLogs
)
const (
serviceNameKey = "servicename"
// We repeat this constant here as we don't want the circular dependency
// of importint our tracing package
TraceIDKey = "x-b3-traceid"
)
// so we dont have to import zap everywhere
type Option = zap.Option
type WrappedLogger struct {
*zap.SugaredLogger
}
func (l *WrappedLogger) ErrorR(msg string, args ...any) {
keyVals := []any{}
for i, v := range args {
keyVals = append(keyVals, fmt.Sprintf("arg%d", i))
keyVals = append(keyVals, v)
}
l.WithOptions(zap.AddCallerSkip(1)).Errorw(msg, keyVals...)
}
func (l *WrappedLogger) InfoR(msg string, args ...any) {
keyVals := []any{}
for i, v := range args {
keyVals = append(keyVals, fmt.Sprintf("arg%d", i))
keyVals = append(keyVals, v)
}
l.WithOptions(zap.AddCallerSkip(1)).Infow(msg, keyVals...)
}
func (l *WrappedLogger) DebugR(msg string, args ...any) {
keyVals := []any{}
for i, v := range args {
keyVals = append(keyVals, fmt.Sprintf("arg%d", i))
keyVals = append(keyVals, v)
}
l.WithOptions(zap.AddCallerSkip(1)).Debugw(msg, keyVals...)
}
// OnExit should be deferred immediately after calling the
// New() method.
func OnExit() {
_ = Sugar.Sync()
_ = Plain.Sync()
undoMaxProcs()
undoLogger()
Recorded = nil
}
// Resource - the counter is initialised with a zero value which indicates that
// the uber correction is made (default).
type Resource struct {
console bool
filename string
}
type ResourceOption func(*Resource)
func WithFile(filename string) ResourceOption {
return func(r *Resource) {
r.filename = filename
}
}
func WithConsole() ResourceOption {
return func(r *Resource) {
r.console = true
}
}
type Syncer func() error
// New creates 2 loggers (plain and sugared) as global variables according
// to the desired loglevel ("DEBUG", "NOOP", "TEST", default is "INFO").
// Additionally log output from other loggers in 3rd-party packages
// is redirected to the INFO label of these loggers.
// Both ResourceOption and zap.Option types are supported option types. The
// zap.Options are passed on the to zap logger.
func New(level string, opts ...any) {
r := &Resource{}
for _, iopt := range opts {
if opt, ok := iopt.(ResourceOption); ok {
opt(r)
}
}
var zopts []zap.Option
for _, opt := range opts {
if opt, ok := opt.(zap.Option); ok {
zopts = append(zopts, opt)
}
}
var err error
// Use opinionated presets for now.
switch level {
case "DEBUG":
cfg := zap.NewDevelopmentConfig()
if r.filename != "" {
cfg.OutputPaths = []string{r.filename}
}
if r.console {
cfg.Encoding = "console"
cfg.EncoderConfig = zapcore.EncoderConfig{
MessageKey: "message",
}
}
Plain, err = cfg.Build(zopts...)
if err != nil {
log.Panicf("cannot initialise zap logger: %v", err)
}
case "NOOP":
Plain = zap.NewNop()
case "TEST":
core, recorded := observer.New(zapcore.DebugLevel)
ram := zap.WrapCore(
func(zapcore.Core) zapcore.Core {
return core
},
)
cfg := zap.NewDevelopmentConfig()
if r.filename != "" {
cfg.OutputPaths = []string{r.filename}
}
if r.console {
cfg.Encoding = "console"
cfg.EncoderConfig = zapcore.EncoderConfig{
MessageKey: "message",
}
}
var plain *zap.Logger
plain, err = cfg.Build(zopts...)
if err != nil {
log.Panicf("cannot initialise zap logger: %v", err)
}
Plain = plain.WithOptions(ram)
Recorded = recorded
default:
cfg := zap.NewProductionConfig()
if r.filename != "" {
cfg.OutputPaths = []string{r.filename}
}
if r.console {
cfg.Encoding = "console"
cfg.EncoderConfig = zapcore.EncoderConfig{
MessageKey: "message",
}
}
Plain, err = cfg.Build(zopts...)
if err != nil {
log.Panicf("cannot initialise zap logger: %v", err)
}
}
undoLogger = zap.RedirectStdLog(Plain)
Sugar = &WrappedLogger{
Plain.Sugar(),
}
Sugar.Debugf("Go version %s", runtime.Version())
// Set CPU quota correctly so that stalls on non-existent cores do not occur.
// This must be done as early as possible on task startup - this way all services
// will have this behaviour as this method is called by everyone..
//
// Refs: https://groups.google.com/forum/#%21topic/prometheus-users/QPQ-UbtvS44
// https://github.com/golang/go/issues/19378
//
// To summarise golang applications in kubernetes suffer from intermittent gc
// pauses when the golang application thinks it has access to more cores than
// really available. This results in intermittently high latency when the the
// gc thread stalls when it cannot access the cores it thinks it has.. Both
// Uber and Google noticed this and the solution is to set GOMAXPROCS to the
// number of cores allocated by Kubernetes. This is obtained from the cgroups
// setting and the logic is encapsulated in the automaxprocs package.
//
// At time of writing. archivist has GOMAXPROCS set to 4 but the kubernetes
// setting is 1. This could result in 75% of CPU cycles being lost when the gc
// thread is stalled.
//
// When load testing is implemented, benchmarks should be run and this code
// modified/removed and/or kubernetes limits set more cleverly.
//
// Please note that the runtime.GOMAXPROCS setting will be removed at some
// future date.
//
// See https://github.com/golang/go/issues/33803 for proposal to make this
// go away so that automaxprocs is no longer reqyuired.
var GOMAXPROCS int = runtime.GOMAXPROCS(-1)
Sugar.Debugf("Cores allocation GOMAXPROCS %v", GOMAXPROCS)
// modified/removed and/or kubernetes limits set more cleverly.
undoMaxProcs, err = maxprocs.Set(maxprocs.Logger(Sugar.Infof))
if err != nil {
Sugar.Infof("Error for automaxprocs: %v", err)
}
Sugar.Debugf("Cores allocation GOMAXPROCS %v", runtime.GOMAXPROCS(-1))
// If AUTOMEMLIMIT is not set, it defaults to 0.9. (10% is the headroom for memory sources the Go runtime is unaware of.)
// If GOMEMLIMIT is already set or AUTOMEMLIMIT=off, automatic setting og GMEMLIMIT is disabled.
var GOMEMLIMIT int64 = debug.SetMemoryLimit(-1)
Sugar.Debugf("Memory Limit GOMEMLIMIT %v", GOMEMLIMIT)
}
func valueFromCarrier(carrier opentracing.TextMapCarrier, key string) string {
value, found := carrier[key]
if !found || value == "" {
Sugar.Debugf("%s not found", key)
return ""
}
return value
}
// FromContext takes the trace ID from the current span and adds it to a child wrapped logger:
//
// returns:
// - the new wrapped logger with a context metadata value for traceID
//
// This will be called on entry to a method or a function that has a context.Context.
func (wl *WrappedLogger) FromContext(ctx context.Context) *WrappedLogger {
span := opentracing.SpanFromContext(ctx)
if span == nil {
Sugar.WithOptions(zap.AddCallerSkip(1)).Infof("FromContext: span is nil - this should not happen - the context where this happened is missing tracing content - probably a middleware problem")
return wl
}
carrier := opentracing.TextMapCarrier{}
err := opentracing.GlobalTracer().Inject(span.Context(), opentracing.TextMap, carrier)
if err != nil {
Sugar.Infof("FromContext: can't inject span: %v", err)
return wl
}
fields := []any{}
traceID := valueFromCarrier(carrier, TraceIDKey)
if traceID != "" {
fields = append(fields, zap.String(TraceIDKey, traceID))
}
if len(fields) == 0 {
return wl
}
// add the fields to the logger
sugaredLogger := wl.With(fields...)
return &WrappedLogger{
SugaredLogger: sugaredLogger,
}
}
func (wl *WrappedLogger) WithServiceName(servicename string) *WrappedLogger {
return wl.WithIndex(serviceNameKey, servicename)
}
func (wl *WrappedLogger) WithIndex(key, value string) *WrappedLogger {
return &WrappedLogger{
wl.SugaredLogger.With(zap.String(key, strings.ToLower(value))),
}
}
func (wl *WrappedLogger) WithOptions(opts ...Option) *WrappedLogger {
s := &WrappedLogger{
wl.SugaredLogger.WithOptions(opts...),
}
return s
}
// Close attempts to flush any buffered log entries.
func (wl *WrappedLogger) Close() {
err := wl.Sync()
// not alot we can do other than log that we couldn't flush the log
// This is usually an error 'sync /dev/stderr invalid argument'
// which is pointless
if err != nil && !errors.Is(err, syscall.EINVAL) {
wl.Debugf("Close: Failed to flush log: %v", err)
}
}