-
Notifications
You must be signed in to change notification settings - Fork 332
/
logging.go
179 lines (146 loc) · 5.59 KB
/
logging.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
/*
Copyright 2018 The Knative Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// logging.go contains the logic to configure and interact with the
// logging and metrics libraries.
package logging
import (
"context"
"flag"
"os"
"strconv"
"strings"
"sync"
"time"
"github.com/davecgh/go-spew/spew"
"go.opencensus.io/stats/view"
"go.opencensus.io/trace"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/klog/v2"
)
const (
// 1 second was chosen arbitrarily
metricViewReportingPeriod = 1 * time.Second
// prefix attached to metric name that indicates to the
// ExportSpan method that span needs to be emitted.
emitableSpanNamePrefix = "emitspan-"
)
// FormatLogger is a printf style function for logging in tests.
type FormatLogger func(template string, args ...interface{})
var exporter *zapMetricExporter
// zapMetricExporter is a stats and trace exporter that logs the
// exported data to the provided (probably test specific) zap logger.
// It conforms to the view.Exporter and trace.Exporter interfaces.
type zapMetricExporter struct {
logger *zap.SugaredLogger
}
// ExportView will emit the view data vd (i.e. the stats that have been
// recorded) to the zap logger.
func (e *zapMetricExporter) ExportView(vd *view.Data) {
// We are not currently consuming these metrics, so for now we'll juse
// dump the view.Data object as is.
e.logger.Debug(spew.Sprint(vd))
}
// GetEmitableSpan starts and returns a trace.Span with a name that
// is used by the ExportSpan method to emit the span.
//
//nolint:spancheck
func GetEmitableSpan(ctx context.Context, metricName string) *trace.Span {
_, span := trace.StartSpan(ctx, emitableSpanNamePrefix+metricName)
return span
}
// ExportSpan will emit the trace data to the zap logger. The span is emitted
// only if the metric name is prefix with emitableSpanNamePrefix constant.
func (e *zapMetricExporter) ExportSpan(vd *trace.SpanData) {
if strings.HasPrefix(vd.Name, emitableSpanNamePrefix) {
duration := vd.EndTime.Sub(vd.StartTime)
// We will start the log entry with `metric` to identify it as a metric for parsing
e.logger.Infof("metric %s %d %d %s", vd.Name[len(emitableSpanNamePrefix):], vd.StartTime.UnixNano(), vd.EndTime.UnixNano(), duration)
}
}
const (
logrZapDebugLevel = 3
)
func zapLevelFromLogrLevel(logrLevel int) zapcore.Level {
// Zap levels are -1, 0, 1, 2,... corresponding to DebugLevel, InfoLevel, WarnLevel, ErrorLevel,...
// zapr library just does zapLevel := -1*logrLevel; which means:
// 1. Info level is only active at 0 (versus 2 in klog being generally equivalent to Info)
// 2. Only verbosity of 0 and 1 map to valid Zap levels
// According to https://github.com/uber-go/zap/issues/713 custom levels (i.e. < -1) aren't guaranteed to work, so not using them (for now).
l := zap.InfoLevel
if logrLevel >= logrZapDebugLevel {
l = zap.DebugLevel
}
return l
}
// InitializeMetricExporter initializes the metric exporter logger
func InitializeMetricExporter(context string) {
// If there was a previously registered exporter, unregister it so we only emit
// the metrics in the current context.
if exporter != nil {
view.UnregisterExporter(exporter)
trace.UnregisterExporter(exporter)
}
l := logger.Named(context).Sugar()
exporter = &zapMetricExporter{logger: l}
view.RegisterExporter(exporter)
trace.RegisterExporter(exporter)
view.SetReportingPeriod(metricViewReportingPeriod)
trace.ApplyConfig(trace.Config{DefaultSampler: trace.AlwaysSample()})
}
func printFlags() {
var flagList []interface{}
flag.CommandLine.VisitAll(func(f *flag.Flag) {
flagList = append(flagList, f.Name, f.Value.String())
})
logger.Sugar().Debugw("Test Flags", flagList...)
}
var (
zapCore zapcore.Core
logger *zap.Logger
verbosity int // Amount of log verbosity
loggerInitializeOnce = &sync.Once{}
)
// InitializeLogger initializes logging for Knative tests.
// It should be called prior to executing tests but after command-line flags have been processed.
// It is recommended doing it in the TestMain() function.
func InitializeLogger() {
loggerInitializeOnce.Do(func() {
humanEncoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
// Output streams
// TODO(coryrc): also open a log file if in Prow?
stdOut := zapcore.Lock(os.Stdout)
// Level function helper
zapLevel := zapLevelFromLogrLevel(verbosity)
isPriority := zap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= zapLevel
})
// Assemble the output streams
zapCore = zapcore.NewTee(
// TODO(coryrc): log JSON output somewhere?
zapcore.NewCore(humanEncoder, stdOut, isPriority),
)
logger = zap.New(zapCore)
zap.ReplaceGlobals(logger) // Gets used by klog/glog proxy libraries
// Set klog/glog verbosities (works with and without proxy libraries)
klogLevel := klog.Level(0)
klogLevel.Set(strconv.Itoa(verbosity))
if verbosity > 2 {
printFlags()
}
})
}
func init() {
flag.IntVar(&verbosity, "verbosity", 2,
"Amount of verbosity, 0-10. See https://github.com/go-logr/logr#how-do-i-choose-my-v-levels and https://github.com/kubernetes/community/blob/master/contributors/devel/sig-instrumentation/logging.md")
}