traefik/pkg/middlewares/accesslog/field_middleware.go

75 lines
2.1 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package accesslog
import (
"net/http"
"time"
"github.com/traefik/traefik/v2/pkg/log"
"github.com/traefik/traefik/v2/pkg/middlewares/capture"
2018-11-14 09:18:03 +00:00
"github.com/vulcand/oxy/utils"
)
2020-05-11 10:06:07 +00:00
// FieldApply function hook to add data in accesslog.
2018-11-14 09:18:03 +00:00
type FieldApply func(rw http.ResponseWriter, r *http.Request, next http.Handler, data *LogData)
// FieldHandler sends a new field to the logger.
type FieldHandler struct {
next http.Handler
name string
value string
applyFn FieldApply
}
// NewFieldHandler creates a Field handler.
2020-07-07 12:42:03 +00:00
func NewFieldHandler(next http.Handler, name, value string, applyFn FieldApply) http.Handler {
2018-11-14 09:18:03 +00:00
return &FieldHandler{next: next, name: name, value: value, applyFn: applyFn}
}
func (f *FieldHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
table := GetLogData(req)
if table == nil {
f.next.ServeHTTP(rw, req)
return
}
table.Core[f.name] = f.value
if f.applyFn != nil {
f.applyFn(rw, req, f.next, table)
} else {
f.next.ServeHTTP(rw, req)
}
}
2020-05-11 10:06:07 +00:00
// AddServiceFields add service fields.
2018-11-14 09:18:03 +00:00
func AddServiceFields(rw http.ResponseWriter, req *http.Request, next http.Handler, data *LogData) {
data.Core[ServiceURL] = req.URL // note that this is *not* the original incoming URL
data.Core[ServiceAddr] = req.URL.Host
next.ServeHTTP(rw, req)
}
2020-05-11 10:06:07 +00:00
// AddOriginFields add origin fields.
func AddOriginFields(rw http.ResponseWriter, req *http.Request, next http.Handler, data *LogData) {
2018-11-14 09:18:03 +00:00
start := time.Now().UTC()
next.ServeHTTP(rw, req)
2018-11-14 09:18:03 +00:00
// use UTC to handle switchover of daylight saving correctly
data.Core[OriginDuration] = time.Now().UTC().Sub(start)
// make copy of headers, so we can ensure there is no subsequent mutation
// during response processing
2018-11-14 09:18:03 +00:00
data.OriginResponse = make(http.Header)
utils.CopyHeaders(data.OriginResponse, rw.Header())
ctx := req.Context()
capt, err := capture.FromContext(ctx)
if err != nil {
log.FromContext(log.With(ctx, log.Str(log.MiddlewareType, "AccessLogs"))).Errorf("Could not get Capture: %v", err)
return
}
data.Core[OriginStatus] = capt.StatusCode()
data.Core[OriginContentSize] = capt.ResponseSize()
2018-11-14 09:18:03 +00:00
}