Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support/http: log user agent header at request start #2619

Merged
merged 2 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions services/federation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ bumps. A breaking change will get clearly notified in this log.
## Unreleased

* Dropped support for Go 1.12.
* Log User-Agent header in request logs.

## [v0.3.0] - 2019-11-20

Expand Down
4 changes: 4 additions & 0 deletions services/friendbot/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ file. This project adheres to [Semantic Versioning](http://semver.org/).
As this project is pre 1.0, breaking changes may happen for minor version
bumps. A breaking change will get clearly notified in this log.

## Unreleased

* Log User-Agent header in request logs.

## [v0.0.2] - 2019-11-20

### Changed
Expand Down
11 changes: 6 additions & 5 deletions support/http/logging_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ func logStartOfRequest(
r *stdhttp.Request,
) {
log.Ctx(r.Context()).WithFields(log.F{
"subsys": "http",
"path": r.URL.String(),
"method": r.Method,
"ip": r.RemoteAddr,
"host": r.Host,
"subsys": "http",
"path": r.URL.String(),
"method": r.Method,
"ip": r.RemoteAddr,
"host": r.Host,
"useragent": r.Header.Get("User-Agent"),
}).Info("starting request")
}

Expand Down
4 changes: 4 additions & 0 deletions support/http/logging_middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func TestHTTPMiddleware(t *testing.T) {
assert.Equal(t, "GET", logged[0].Data["method"])
assert.NotEmpty(t, logged[0].Data["req"])
assert.NotEmpty(t, logged[0].Data["path"])
assert.Equal(t, "Go-http-client/1.1", logged[0].Data["useragent"])
req1 := logged[0].Data["req"]

assert.Equal(t, "handler log line", logged[1].Message)
Expand All @@ -52,6 +53,7 @@ func TestHTTPMiddleware(t *testing.T) {
assert.Equal(t, "GET", logged[3].Data["method"])
assert.NotEmpty(t, logged[3].Data["req"])
assert.NotEmpty(t, logged[3].Data["path"])
assert.Equal(t, "Go-http-client/1.1", logged[3].Data["useragent"])
req2 := logged[3].Data["req"]

assert.Equal(t, "finished request", logged[4].Message)
Expand Down Expand Up @@ -89,6 +91,7 @@ func TestHTTPMiddlewareWithLoggerSet(t *testing.T) {
assert.Equal(t, "GET", logged[0].Data["method"])
assert.NotEmpty(t, logged[0].Data["req"])
assert.NotEmpty(t, logged[0].Data["path"])
assert.Equal(t, "Go-http-client/1.1", logged[0].Data["useragent"])
req1 := logged[0].Data["req"]

assert.Equal(t, "handler log line", logged[1].Message)
Expand All @@ -105,6 +108,7 @@ func TestHTTPMiddlewareWithLoggerSet(t *testing.T) {
assert.Equal(t, "GET", logged[3].Data["method"])
assert.NotEmpty(t, logged[3].Data["req"])
assert.NotEmpty(t, logged[3].Data["path"])
assert.Equal(t, "Go-http-client/1.1", logged[3].Data["useragent"])
req2 := logged[3].Data["req"]

assert.Equal(t, "finished request", logged[4].Message)
Expand Down