Move origin fields capture to service level
Co-authored-by: lbenguigui <lbenguigui@gmail.com>
This commit is contained in:
parent
b786f58f80
commit
b966215e6c
7 changed files with 121 additions and 17 deletions
|
@ -46,11 +46,6 @@ func AddServiceFields(rw http.ResponseWriter, req *http.Request, next http.Handl
|
|||
data.Core[ServiceURL] = req.URL // note that this is *not* the original incoming URL
|
||||
data.Core[ServiceAddr] = req.URL.Host
|
||||
|
||||
next.ServeHTTP(rw, req)
|
||||
}
|
||||
|
||||
// AddOriginFields add origin fields.
|
||||
func AddOriginFields(rw http.ResponseWriter, req *http.Request, next http.Handler, data *LogData) {
|
||||
start := time.Now().UTC()
|
||||
|
||||
next.ServeHTTP(rw, req)
|
||||
|
@ -72,3 +67,14 @@ func AddOriginFields(rw http.ResponseWriter, req *http.Request, next http.Handle
|
|||
data.Core[OriginStatus] = capt.StatusCode()
|
||||
data.Core[OriginContentSize] = capt.ResponseSize()
|
||||
}
|
||||
|
||||
// InitServiceFields init service fields.
|
||||
func InitServiceFields(rw http.ResponseWriter, req *http.Request, next http.Handler, data *LogData) {
|
||||
// Because they are expected to be initialized when the logger is processing the data table,
|
||||
// the origin fields are initialized in case the response is returned by Traefik itself, and not a service.
|
||||
data.Core[OriginDuration] = time.Duration(0)
|
||||
data.Core[OriginStatus] = 0
|
||||
data.Core[OriginContentSize] = int64(0)
|
||||
|
||||
next.ServeHTTP(rw, req)
|
||||
}
|
||||
|
|
|
@ -40,8 +40,8 @@ func (f *CommonLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|||
toLog(entry.Data, RequestMethod, defaultValue, false),
|
||||
toLog(entry.Data, RequestPath, defaultValue, false),
|
||||
toLog(entry.Data, RequestProtocol, defaultValue, false),
|
||||
toLog(entry.Data, OriginStatus, defaultValue, true),
|
||||
toLog(entry.Data, OriginContentSize, defaultValue, true),
|
||||
toLog(entry.Data, DownstreamStatus, defaultValue, true),
|
||||
toLog(entry.Data, DownstreamContentSize, defaultValue, true),
|
||||
toLog(entry.Data, "request_Referer", `"-"`, true),
|
||||
toLog(entry.Data, "request_User-Agent", `"-"`, true),
|
||||
toLog(entry.Data, RequestCount, defaultValue, true),
|
||||
|
|
|
@ -18,7 +18,7 @@ func TestCommonLogFormatter_Format(t *testing.T) {
|
|||
expectedLog string
|
||||
}{
|
||||
{
|
||||
name: "OriginStatus & OriginContentSize are nil",
|
||||
name: "DownstreamStatus & DownstreamContentSize are nil",
|
||||
data: map[string]interface{}{
|
||||
StartUTC: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
|
||||
Duration: 123 * time.Second,
|
||||
|
@ -27,8 +27,8 @@ func TestCommonLogFormatter_Format(t *testing.T) {
|
|||
RequestMethod: http.MethodGet,
|
||||
RequestPath: "/foo",
|
||||
RequestProtocol: "http",
|
||||
OriginStatus: nil,
|
||||
OriginContentSize: nil,
|
||||
DownstreamStatus: nil,
|
||||
DownstreamContentSize: nil,
|
||||
RequestRefererHeader: "",
|
||||
RequestUserAgentHeader: "",
|
||||
RequestCount: 0,
|
||||
|
@ -48,8 +48,8 @@ func TestCommonLogFormatter_Format(t *testing.T) {
|
|||
RequestMethod: http.MethodGet,
|
||||
RequestPath: "/foo",
|
||||
RequestProtocol: "http",
|
||||
OriginStatus: 123,
|
||||
OriginContentSize: 132,
|
||||
DownstreamStatus: 123,
|
||||
DownstreamContentSize: 132,
|
||||
RequestRefererHeader: "referer",
|
||||
RequestUserAgentHeader: "agent",
|
||||
RequestCount: nil,
|
||||
|
@ -69,8 +69,8 @@ func TestCommonLogFormatter_Format(t *testing.T) {
|
|||
RequestMethod: http.MethodGet,
|
||||
RequestPath: "/foo",
|
||||
RequestProtocol: "http",
|
||||
OriginStatus: 123,
|
||||
OriginContentSize: 132,
|
||||
DownstreamStatus: 123,
|
||||
DownstreamContentSize: 132,
|
||||
RequestRefererHeader: "referer",
|
||||
RequestUserAgentHeader: "agent",
|
||||
RequestCount: nil,
|
||||
|
|
|
@ -76,7 +76,7 @@ func (m *Manager) BuildHandlers(rootCtx context.Context, entryPoints []string, t
|
|||
}
|
||||
|
||||
handlerWithAccessLog, err := alice.New(func(next http.Handler) (http.Handler, error) {
|
||||
return accesslog.NewFieldHandler(next, log.EntryPointName, entryPointName, accesslog.AddOriginFields), nil
|
||||
return accesslog.NewFieldHandler(next, log.EntryPointName, entryPointName, accesslog.InitServiceFields), nil
|
||||
}).Then(handler)
|
||||
if err != nil {
|
||||
log.FromContext(ctx).Error(err)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue