Skip to content

Log request headers, response headers and some body as warning on 500s#41

Merged
jml merged 7 commits intomasterfrom
log-users
Sep 18, 2017
Merged

Log request headers, response headers and some body as warning on 500s#41
jml merged 7 commits intomasterfrom
log-users

Conversation

@jml
Copy link
Contributor

@jml jml commented Jun 2, 2017

Part of weaveworks/service#1233

This is kind of a significant change, as it intercepts all incoming requests. I haven't done much in the way of testing.

Also changes the logging order slightly. Previously, when LogRequestHeaders was on, we would log headers then request/response summary (method, status, etc). Now, we log method, status, etc. then headers.

@jml jml force-pushed the log-users branch 2 times, most recently from 6176a41 to ebb5295 Compare June 16, 2017 14:53
@jml jml changed the title [WIP] Log request headers, response headers and some body as warning on 500s Log request headers, response headers and some body as warning on 500s Jun 16, 2017
@jml jml requested a review from bboreham September 8, 2017 13:55
Copy link
Collaborator

@bboreham bboreham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code is broadly fine, but I observe that it goes to a lot of work to capture response headers when they are not asked for in the original issue. I can't immediately think why they would be useful in a 5xx.

// response headers if `statusCode` is a 5XX.
func (b *badResponseLogger) WriteHeader(statusCode int) {
b.statusCode = statusCode
b.recorder.WriteHeader(statusCode)

This comment was marked as abuse.

recorder: httptest.NewRecorder(),
logBody: false,
bodyBytesLeft: maxResponseBodyInLogs,
statusCode: http.StatusOK,

This comment was marked as abuse.

This comment was marked as abuse.


if len(data) > b.bodyBytesLeft {
b.recorder.Write(data[:b.bodyBytesLeft])
b.recorder.WriteString("…")

This comment was marked as abuse.


if len(data) > b.bodyBytesLeft {
b.recorder.Write(data[:b.bodyBytesLeft])
b.recorder.WriteString("…")

This comment was marked as abuse.

}

// WriteHeader implements http.ResponseWriter. It will immediately log the
// response headers if `statusCode` is a 5XX.

This comment was marked as abuse.

This comment was marked as abuse.

logWithRequest(r).Warnf("Is websocket request: %v\n%s", IsWSHandshakeRequest(r), string(headers))
response, err := i.dumpResponse()
if err != nil {
logWithRequest(r).Errorf("Could not dump response headers: %v", err)

This comment was marked as abuse.

@jml
Copy link
Contributor Author

jml commented Sep 12, 2017

it goes to a lot of work to capture response headers when they are not asked for in the original issue. I can't immediately think why they would be useful in a 5xx.

Does it really? It's been a while since I've looked at this, but I thought most of the work was around making sure we're still streaming headers & body, and that we don't accidentally store massive bodies in memory while building up a log entry.

I'm not sure what I'd change if we wanted to stop capturing response headers. Maybe change badResponseLogger.recorder to be a simple io.Writer rather than an httptest.ResponseRecorder. Or is there some other more significant simplification I'm missing?

@bboreham
Copy link
Collaborator

work

meaning we build a data structure of headers on every response, including the 200s and 400s that we're not going to log

change badResponseLogger.recorder to be a simple io.Writer rather than an httptest.ResponseRecorder.

Exactly. Like a bytes.Buffer.

@jml
Copy link
Contributor Author

jml commented Sep 12, 2017

Makes sense. Doing that means that MultiResponseWriter is surplus to requirements and that a more specialised approach is warranted. Have merged badResponseLogger and MultiResponseWriter, which results in same behaviour with much less code.

@jml
Copy link
Contributor Author

jml commented Sep 12, 2017

PTAL @bboreham

@bboreham
Copy link
Collaborator

Suggest you note this PR makes a quiet change, in that the order of request and headers is reversed in the logs, when LogRequestHeaders is on.

@jml jml merged commit c8c93e3 into master Sep 18, 2017
@jml jml deleted the log-users branch September 18, 2017 10:59
jml added a commit that referenced this pull request Sep 18, 2017
jml added a commit that referenced this pull request Sep 18, 2017
…g on 500s" (#56)

* Revert "Use new gRPC APIs (#55)"

This reverts commit df441dc.

* Revert "Log request headers, response headers and some body as warning on 500s (#41)"

This reverts commit c8c93e3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants