From c12875d6df893b7cb4ad7757f236708b339cdcac Mon Sep 17 00:00:00 2001 From: Cezar Sa Espinola Date: Wed, 29 Aug 2018 17:06:45 -0300 Subject: [PATCH] api: add user-agent to access logs --- api/middleware.go | 2 +- api/middleware_test.go | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/api/middleware.go b/api/middleware.go index 67ff96ef5a..c59e17132a 100644 --- a/api/middleware.go +++ b/api/middleware.go @@ -275,7 +275,7 @@ func (l *loggerMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, ne if r.TLS != nil { scheme = "https" } - l.logger.Printf("%s %s %s %s %d in %0.6fms%s", nowFormatted, scheme, r.Method, r.URL.Path, statusCode, float64(duration)/float64(time.Millisecond), requestID) + l.logger.Printf("%s %s %s %s %d %q in %0.6fms%s", nowFormatted, scheme, r.Method, r.URL.Path, statusCode, r.UserAgent(), float64(duration)/float64(time.Millisecond), requestID) } type contentHijackMiddleware struct { diff --git a/api/middleware_test.go b/api/middleware_test.go index 70e3c9637d..b88b5a3487 100644 --- a/api/middleware_test.go +++ b/api/middleware_test.go @@ -497,6 +497,7 @@ func (s *S) TestLoggerMiddleware(c *check.C) { recorder := httptest.NewRecorder() request, err := http.NewRequest("PUT", "/my/path", nil) c.Assert(err, check.IsNil) + request.Header.Set("User-Agent", "ardata 1.1") h, handlerLog := doHandler() handlerLog.sleep = 100 * time.Millisecond handlerLog.response = http.StatusOK @@ -507,7 +508,7 @@ func (s *S) TestLoggerMiddleware(c *check.C) { middle.ServeHTTP(negroni.NewResponseWriter(recorder), request, h) c.Assert(handlerLog.called, check.Equals, true) timePart := time.Now().Format(time.RFC3339Nano)[:19] - c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? http PUT /my/path 200 in 1\d{2}\.\d+ms`+"\n", timePart)) + c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? http PUT /my/path 200 "ardata 1.1" in 1\d{2}\.\d+ms`+"\n", timePart)) } func (s *S) TestLoggerMiddlewareWithoutStatusCode(c *check.C) { @@ -524,7 +525,7 @@ func (s *S) TestLoggerMiddlewareWithoutStatusCode(c *check.C) { middle.ServeHTTP(negroni.NewResponseWriter(recorder), request, h) c.Assert(handlerLog.called, check.Equals, true) timePart := time.Now().Format(time.RFC3339Nano)[:19] - c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? http PUT /my/path 200 in 1\d{2}\.\d+ms`+"\n", timePart)) + c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? http PUT /my/path 200 "" in 1\d{2}\.\d+ms`+"\n", timePart)) } func (s *S) TestLoggerMiddlewareWithRequestID(c *check.C) { @@ -544,7 +545,7 @@ func (s *S) TestLoggerMiddlewareWithRequestID(c *check.C) { middle.ServeHTTP(negroni.NewResponseWriter(recorder), request, h) c.Assert(handlerLog.called, check.Equals, true) timePart := time.Now().Format(time.RFC3339Nano)[:19] - c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? http PUT /my/path 200 in 1\d{2}\.\d+ms \[Request-ID: my-rid\]`+"\n", timePart)) + c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? http PUT /my/path 200 "" in 1\d{2}\.\d+ms \[Request-ID: my-rid\]`+"\n", timePart)) } func (s *S) TestLoggerMiddlewareHTTPS(c *check.C) { @@ -567,7 +568,7 @@ func (s *S) TestLoggerMiddlewareHTTPS(c *check.C) { c.Assert(rsp.StatusCode, check.Equals, http.StatusOK) c.Assert(handlerLog.called, check.Equals, true) timePart := time.Now().Format(time.RFC3339Nano)[:19] - c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? https PUT /my/path 200 in \d{1}\.\d+ms`+"\n", timePart)) + c.Assert(out.String(), check.Matches, fmt.Sprintf(`%s\..+? https PUT /my/path 200 "Go-http-client/1.1" in \d{1}\.\d+ms`+"\n", timePart)) } func (s *S) TestContentHijackerMiddleware(c *check.C) {