Skip to content

Commit

Permalink
Issue #278: Add service name field to access log
Browse files Browse the repository at this point in the history
This patch adds a $upstream_service field to the
available access log fields.

Fixes #278
  • Loading branch information
magiconair committed Apr 28, 2017
1 parent 77a489c commit 1be3ba7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions fabio.properties
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@
# $upstream_request_scheme - upstream request scheme
# $upstream_request_uri - upstream request URI
# $upstream_request_url - upstream request URL
# $upstream_service - name of the upstream service
#
# The default is
#
Expand Down
4 changes: 4 additions & 0 deletions logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ type Event struct {
// upstream server which handled the proxied request.
UpstreamAddr string

// UpstreamService is the name of the upstream service as
// defined in the route.
UpstreamService string

// UpstreamURL is the URL which was sent to the upstream server.
// It should only be set for HTTP log events.
UpstreamURL *url.URL
Expand Down
8 changes: 5 additions & 3 deletions logger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ func TestLog(t *testing.T) {
RemoteAddr: "5.6.7.8:1234",
},
},
RequestURL: rurl,
UpstreamAddr: uurl.Host,
UpstreamURL: uurl,
RequestURL: rurl,
UpstreamAddr: uurl.Host,
UpstreamService: "svc-a",
UpstreamURL: uurl,
}

tests := []struct {
Expand Down Expand Up @@ -121,6 +122,7 @@ func TestLog(t *testing.T) {
{"$upstream_request_scheme", "http\n"},
{"$upstream_request_uri", "/foo?q=x\n"},
{"$upstream_request_url", "http://7.8.9.0:5678/foo?q=x\n"},
{"$upstream_service", "svc-a\n"},
}

for _, tt := range tests {
Expand Down
3 changes: 3 additions & 0 deletions logger/pattern.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ var fields = map[string]field{
}
b.WriteString(e.UpstreamURL.String())
},
"$upstream_service": func(b *bytes.Buffer, e *Event) {
b.WriteString(e.UpstreamService)
},
}

var shortMonthNames = []string{
Expand Down
6 changes: 5 additions & 1 deletion proxy/http_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ func TestProxyLogOutput(t *testing.T) {
},
Transport: http.DefaultTransport,
Lookup: func(r *http.Request) *route.Target {
return &route.Target{URL: mustParse(server.URL)}
return &route.Target{
Service: "svc-a",
URL: mustParse(server.URL),
}
},
Logger: l,
}
Expand Down Expand Up @@ -182,6 +185,7 @@ func TestProxyLogOutput(t *testing.T) {
"upstream_request_scheme:" + upstreamURL.Scheme,
"upstream_request_uri:/foo?x=y",
"upstream_request_url:" + upstreamURL.String() + "/foo?x=y",
"upstream_service:svc-a",
}

data := string(b.Bytes())
Expand Down
15 changes: 8 additions & 7 deletions proxy/http_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,14 @@ func (p *HTTPProxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// write access log
if p.Logger != nil {
p.Logger.Log(&logger.Event{
Start: start,
End: end,
Request: r,
Response: resp,
RequestURL: requestURL,
UpstreamAddr: targetURL.Host,
UpstreamURL: targetURL,
Start: start,
End: end,
Request: r,
Response: resp,
RequestURL: requestURL,
UpstreamAddr: targetURL.Host,
UpstreamService: t.Service,
UpstreamURL: targetURL,
})
}
}
Expand Down

0 comments on commit 1be3ba7

Please sign in to comment.