2019-01-12 04:58:27 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 The Kubernetes 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package httplog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
2019-09-27 21:51:53 +00:00
|
|
|
"context"
|
2019-01-12 04:58:27 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"runtime"
|
|
|
|
"time"
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
"k8s.io/klog/v2"
|
2019-01-12 04:58:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// StacktracePred returns true if a stacktrace should be logged for this status.
|
|
|
|
type StacktracePred func(httpStatus int) (logStacktrace bool)
|
|
|
|
|
|
|
|
type logger interface {
|
|
|
|
Addf(format string, data ...interface{})
|
|
|
|
}
|
|
|
|
|
2019-09-27 21:51:53 +00:00
|
|
|
type respLoggerContextKeyType int
|
|
|
|
|
|
|
|
// respLoggerContextKey is used to store the respLogger pointer in the request context.
|
|
|
|
const respLoggerContextKey respLoggerContextKeyType = iota
|
|
|
|
|
2019-01-12 04:58:27 +00:00
|
|
|
// Add a layer on top of ResponseWriter, so we can track latency and error
|
|
|
|
// message sources.
|
|
|
|
//
|
|
|
|
// TODO now that we're using go-restful, we shouldn't need to be wrapping
|
|
|
|
// the http.ResponseWriter. We can recover panics from go-restful, and
|
|
|
|
// the logging value is questionable.
|
|
|
|
type respLogger struct {
|
|
|
|
hijacked bool
|
|
|
|
statusRecorded bool
|
|
|
|
status int
|
|
|
|
statusStack string
|
|
|
|
addedInfo string
|
|
|
|
startTime time.Time
|
|
|
|
|
|
|
|
captureErrorOutput bool
|
|
|
|
|
|
|
|
req *http.Request
|
|
|
|
w http.ResponseWriter
|
|
|
|
|
|
|
|
logStacktracePred StacktracePred
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple logger that logs immediately when Addf is called
|
|
|
|
type passthroughLogger struct{}
|
|
|
|
|
|
|
|
// Addf logs info immediately.
|
|
|
|
func (passthroughLogger) Addf(format string, data ...interface{}) {
|
|
|
|
klog.V(2).Info(fmt.Sprintf(format, data...))
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultStacktracePred is the default implementation of StacktracePred.
|
|
|
|
func DefaultStacktracePred(status int) bool {
|
|
|
|
return (status < http.StatusOK || status >= http.StatusInternalServerError) && status != http.StatusSwitchingProtocols
|
|
|
|
}
|
|
|
|
|
2019-09-27 21:51:53 +00:00
|
|
|
// WithLogging wraps the handler with logging.
|
|
|
|
func WithLogging(handler http.Handler, pred StacktracePred) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
ctx := req.Context()
|
|
|
|
if old := respLoggerFromContext(req); old != nil {
|
|
|
|
panic("multiple WithLogging calls!")
|
|
|
|
}
|
|
|
|
rl := newLogged(req, w).StacktraceWhen(pred)
|
|
|
|
req = req.WithContext(context.WithValue(ctx, respLoggerContextKey, rl))
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
if klog.V(3).Enabled() {
|
|
|
|
defer func() { klog.InfoS("HTTP", rl.LogArgs()...) }()
|
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
handler.ServeHTTP(rl, req)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// respLoggerFromContext returns the respLogger or nil.
|
|
|
|
func respLoggerFromContext(req *http.Request) *respLogger {
|
|
|
|
ctx := req.Context()
|
|
|
|
val := ctx.Value(respLoggerContextKey)
|
|
|
|
if rl, ok := val.(*respLogger); ok {
|
|
|
|
return rl
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
2019-09-27 21:51:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newLogged turns a normal response writer into a logged response writer.
|
|
|
|
func newLogged(req *http.Request, w http.ResponseWriter) *respLogger {
|
|
|
|
return &respLogger{
|
2019-01-12 04:58:27 +00:00
|
|
|
startTime: time.Now(),
|
|
|
|
req: req,
|
2019-09-27 21:51:53 +00:00
|
|
|
w: w,
|
2019-01-12 04:58:27 +00:00
|
|
|
logStacktracePred: DefaultStacktracePred,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogOf returns the logger hiding in w. If there is not an existing logger
|
|
|
|
// then a passthroughLogger will be created which will log to stdout immediately
|
|
|
|
// when Addf is called.
|
|
|
|
func LogOf(req *http.Request, w http.ResponseWriter) logger {
|
2019-09-27 21:51:53 +00:00
|
|
|
if rl := respLoggerFromContext(req); rl != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return rl
|
|
|
|
}
|
|
|
|
return &passthroughLogger{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlogged returns the original ResponseWriter, or w if it is not our inserted logger.
|
2019-09-27 21:51:53 +00:00
|
|
|
func Unlogged(req *http.Request, w http.ResponseWriter) http.ResponseWriter {
|
|
|
|
if rl := respLoggerFromContext(req); rl != nil {
|
2019-01-12 04:58:27 +00:00
|
|
|
return rl.w
|
|
|
|
}
|
|
|
|
return w
|
|
|
|
}
|
|
|
|
|
|
|
|
// StacktraceWhen sets the stacktrace logging predicate, which decides when to log a stacktrace.
|
|
|
|
// There's a default, so you don't need to call this unless you don't like the default.
|
|
|
|
func (rl *respLogger) StacktraceWhen(pred StacktracePred) *respLogger {
|
|
|
|
rl.logStacktracePred = pred
|
|
|
|
return rl
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatusIsNot returns a StacktracePred which will cause stacktraces to be logged
|
|
|
|
// for any status *not* in the given list.
|
|
|
|
func StatusIsNot(statuses ...int) StacktracePred {
|
2019-08-30 18:33:25 +00:00
|
|
|
statusesNoTrace := map[int]bool{}
|
|
|
|
for _, s := range statuses {
|
|
|
|
statusesNoTrace[s] = true
|
|
|
|
}
|
2019-01-12 04:58:27 +00:00
|
|
|
return func(status int) bool {
|
2019-08-30 18:33:25 +00:00
|
|
|
_, ok := statusesNoTrace[status]
|
|
|
|
return !ok
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Addf adds additional data to be logged with this request.
|
|
|
|
func (rl *respLogger) Addf(format string, data ...interface{}) {
|
|
|
|
rl.addedInfo += "\n" + fmt.Sprintf(format, data...)
|
|
|
|
}
|
|
|
|
|
2020-08-10 17:43:49 +00:00
|
|
|
func (rl *respLogger) LogArgs() []interface{} {
|
2019-01-12 04:58:27 +00:00
|
|
|
latency := time.Since(rl.startTime)
|
2020-08-10 17:43:49 +00:00
|
|
|
if rl.hijacked {
|
|
|
|
return []interface{}{
|
|
|
|
"verb", rl.req.Method,
|
|
|
|
"URI", rl.req.RequestURI,
|
|
|
|
"latency", latency,
|
|
|
|
"userAgent", rl.req.UserAgent(),
|
|
|
|
"srcIP", rl.req.RemoteAddr,
|
|
|
|
"hijacked", true,
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-10 17:43:49 +00:00
|
|
|
args := []interface{}{
|
|
|
|
"verb", rl.req.Method,
|
|
|
|
"URI", rl.req.RequestURI,
|
|
|
|
"latency", latency,
|
|
|
|
"userAgent", rl.req.UserAgent(),
|
|
|
|
"srcIP", rl.req.RemoteAddr,
|
|
|
|
"resp", rl.status,
|
|
|
|
}
|
|
|
|
if len(rl.statusStack) > 0 {
|
|
|
|
args = append(args, "statusStack", rl.statusStack)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(rl.addedInfo) > 0 {
|
|
|
|
args = append(args, "addedInfo", rl.addedInfo)
|
|
|
|
}
|
|
|
|
return args
|
2019-01-12 04:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Header implements http.ResponseWriter.
|
|
|
|
func (rl *respLogger) Header() http.Header {
|
|
|
|
return rl.w.Header()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements http.ResponseWriter.
|
|
|
|
func (rl *respLogger) Write(b []byte) (int, error) {
|
|
|
|
if !rl.statusRecorded {
|
|
|
|
rl.recordStatus(http.StatusOK) // Default if WriteHeader hasn't been called
|
|
|
|
}
|
|
|
|
if rl.captureErrorOutput {
|
|
|
|
rl.Addf("logging error output: %q\n", string(b))
|
|
|
|
}
|
|
|
|
return rl.w.Write(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush implements http.Flusher even if the underlying http.Writer doesn't implement it.
|
|
|
|
// Flush is used for streaming purposes and allows to flush buffered data to the client.
|
|
|
|
func (rl *respLogger) Flush() {
|
|
|
|
if flusher, ok := rl.w.(http.Flusher); ok {
|
|
|
|
flusher.Flush()
|
2020-08-10 17:43:49 +00:00
|
|
|
} else if klog.V(2).Enabled() {
|
2019-01-12 04:58:27 +00:00
|
|
|
klog.InfoDepth(1, fmt.Sprintf("Unable to convert %+v into http.Flusher", rl.w))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteHeader implements http.ResponseWriter.
|
|
|
|
func (rl *respLogger) WriteHeader(status int) {
|
|
|
|
rl.recordStatus(status)
|
|
|
|
rl.w.WriteHeader(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hijack implements http.Hijacker.
|
|
|
|
func (rl *respLogger) Hijack() (net.Conn, *bufio.ReadWriter, error) {
|
|
|
|
rl.hijacked = true
|
|
|
|
return rl.w.(http.Hijacker).Hijack()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseNotify implements http.CloseNotifier
|
|
|
|
func (rl *respLogger) CloseNotify() <-chan bool {
|
|
|
|
return rl.w.(http.CloseNotifier).CloseNotify()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rl *respLogger) recordStatus(status int) {
|
|
|
|
rl.status = status
|
|
|
|
rl.statusRecorded = true
|
|
|
|
if rl.logStacktracePred(status) {
|
|
|
|
// Only log stacks for errors
|
|
|
|
stack := make([]byte, 50*1024)
|
|
|
|
stack = stack[:runtime.Stack(stack, false)]
|
|
|
|
rl.statusStack = "\n" + string(stack)
|
|
|
|
rl.captureErrorOutput = true
|
|
|
|
} else {
|
|
|
|
rl.statusStack = ""
|
|
|
|
}
|
|
|
|
}
|