diff --git a/fabio.properties b/fabio.properties index 6abc7303e..e35f74282 100644 --- a/fabio.properties +++ b/fabio.properties @@ -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 # diff --git a/logger/logger.go b/logger/logger.go index 2dc7db890..b9077320e 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -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 diff --git a/logger/logger_test.go b/logger/logger_test.go index a28975f20..2d310d8a6 100644 --- a/logger/logger_test.go +++ b/logger/logger_test.go @@ -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 { @@ -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 { diff --git a/logger/pattern.go b/logger/pattern.go index 4d418d786..664bf74b6 100644 --- a/logger/pattern.go +++ b/logger/pattern.go @@ -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{ diff --git a/proxy/http_integration_test.go b/proxy/http_integration_test.go index 50a28fceb..fcf627b37 100644 --- a/proxy/http_integration_test.go +++ b/proxy/http_integration_test.go @@ -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, } @@ -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()) diff --git a/proxy/http_proxy.go b/proxy/http_proxy.go index 45af4e030..8c52338fe 100644 --- a/proxy/http_proxy.go +++ b/proxy/http_proxy.go @@ -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, }) } }