Skip to content

Commit db194ac

Browse files
authored
Merge pull request kubernetes#91833 from AkashSarda/structured_logging
Structured logging for Apiserver [Issue kubernetes#1602 Structured logging]
2 parents 9cd906e + af60e7b commit db194ac

File tree

3 files changed

+23
-22
lines changed

3 files changed

+23
-22
lines changed

staging/src/k8s.io/apiserver/pkg/endpoints/handlers/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ func ListResource(r rest.Lister, rw rest.Watcher, scope *RequestScope, forceWatc
248248
if timeout == 0 && minRequestTimeout > 0 {
249249
timeout = time.Duration(float64(minRequestTimeout) * (rand.Float64() + 1.0))
250250
}
251-
klog.V(3).Infof("Starting watch for %s, rv=%s labels=%s fields=%s timeout=%s", req.URL.Path, opts.ResourceVersion, opts.LabelSelector, opts.FieldSelector, timeout)
251+
klog.V(3).InfoS("Starting watch", "path", req.URL.Path, "resourceVersion", opts.ResourceVersion, "labels", opts.LabelSelector, "fields", opts.FieldSelector, "timeout", timeout)
252252
ctx, cancel := context.WithTimeout(ctx, timeout)
253253
defer cancel()
254254
watcher, err := rw.Watch(ctx, &opts)

staging/src/k8s.io/apiserver/pkg/server/httplog/httplog.go

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func WithLogging(handler http.Handler, pred StacktracePred) http.Handler {
8585
rl := newLogged(req, w).StacktraceWhen(pred)
8686
req = req.WithContext(context.WithValue(ctx, respLoggerContextKey, rl))
8787

88-
defer rl.Log()
88+
defer func() { klog.V(3).InfoS("Received HTTP request", rl.LogArgs()...) }()
8989
handler.ServeHTTP(rl, req)
9090
})
9191
}
@@ -153,24 +153,28 @@ func (rl *respLogger) Addf(format string, data ...interface{}) {
153153
rl.addedInfo += "\n" + fmt.Sprintf(format, data...)
154154
}
155155

156-
// Log is intended to be called once at the end of your request handler, via defer
157-
func (rl *respLogger) Log() {
156+
func (rl *respLogger) LogArgs() []interface{} {
158157
latency := time.Since(rl.startTime)
159-
if klog.V(3).Enabled() {
160-
if !rl.hijacked {
161-
klog.InfoDepth(1, fmt.Sprintf("verb=%q URI=%q latency=%v resp=%v UserAgent=%q srcIP=%q: %v%v",
162-
rl.req.Method, rl.req.RequestURI,
163-
latency, rl.status,
164-
rl.req.UserAgent(), rl.req.RemoteAddr,
165-
rl.statusStack, rl.addedInfo,
166-
))
167-
} else {
168-
klog.InfoDepth(1, fmt.Sprintf("verb=%q URI=%q latency=%v UserAgent=%q srcIP=%q: hijacked",
169-
rl.req.Method, rl.req.RequestURI,
170-
latency, rl.req.UserAgent(), rl.req.RemoteAddr,
171-
))
158+
args := []interface{}{
159+
"verb", rl.req.Method,
160+
"URI", rl.req.RequestURI,
161+
"latency", latency,
162+
"userAgent", rl.req.UserAgent(),
163+
"srcIP", rl.req.RemoteAddr,
164+
}
165+
if rl.hijacked {
166+
args = append(args, "hijacked", true)
167+
} else {
168+
args = append(args, "resp", rl.status)
169+
if len(rl.statusStack) > 0 {
170+
args = append(args, "statusStack", rl.statusStack)
171+
}
172+
173+
if len(rl.addedInfo) > 0 {
174+
args = append(args, "addedInfo", rl.addedInfo)
172175
}
173176
}
177+
return args
174178
}
175179

176180
// Header implements http.ResponseWriter.

staging/src/k8s.io/apiserver/pkg/storage/etcd3/watcher.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,7 @@ func (wc *watchChan) processEvent(wg *sync.WaitGroup) {
261261
continue
262262
}
263263
if len(wc.resultChan) == outgoingBufSize {
264-
klog.V(3).Infof("Fast watcher, slow processing. Number of buffered events: %d."+
265-
"Probably caused by slow dispatching events to watchers", outgoingBufSize)
264+
klog.V(3).InfoS("Fast watcher, slow processing. Probably caused by slow dispatching events to watchers", "outgoingEvents", outgoingBufSize)
266265
}
267266
// If user couldn't receive results fast enough, we also block incoming events from watcher.
268267
// Because storing events in local will cause more memory usage.
@@ -368,9 +367,7 @@ func (wc *watchChan) sendError(err error) {
368367

369368
func (wc *watchChan) sendEvent(e *event) {
370369
if len(wc.incomingEventChan) == incomingBufSize {
371-
klog.V(3).Infof("Fast watcher, slow processing. Number of buffered events: %d."+
372-
"Probably caused by slow decoding, user not receiving fast, or other processing logic",
373-
incomingBufSize)
370+
klog.V(3).InfoS("Fast watcher, slow processing. Probably caused by slow decoding, user not receiving fast, or other processing logic", "incomingEvents", incomingBufSize)
374371
}
375372
select {
376373
case wc.incomingEventChan <- e:

0 commit comments

Comments
 (0)