-
Notifications
You must be signed in to change notification settings - Fork 463
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
Fix the response status code logged in 'query stats' on error #8407
Changes from 1 commit
e8cc9b6
9567322
f83caa0
2c20298
b20987d
793436b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -220,8 +220,9 @@ func (f *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
queryResponseTime := time.Since(startTime) | ||
|
||
if err != nil { | ||
writeError(w, err) | ||
f.reportQueryStats(r, params, startTime, queryResponseTime, 0, queryDetails, 0, err) | ||
// TODO unit test it | ||
statusCode := writeError(w, err) | ||
f.reportQueryStats(r, params, startTime, queryResponseTime, 0, queryDetails, statusCode, err) | ||
return | ||
} | ||
|
||
|
@@ -428,7 +429,8 @@ func formatRequestHeaders(h *http.Header, headersToLog []string) (fields []any) | |
return fields | ||
} | ||
|
||
func writeError(w http.ResponseWriter, err error) { | ||
// writeError writes the error response to http.ResponseWriter, and returns the response HTTP status code. | ||
func writeError(w http.ResponseWriter, err error) (statusCode int) { | ||
switch { | ||
case errors.Is(err, context.Canceled): | ||
err = errCanceled | ||
|
@@ -440,13 +442,31 @@ func writeError(w http.ResponseWriter, err error) { | |
} | ||
} | ||
|
||
// if the error is an APIError, ensure it gets written as a JSON response | ||
if resp, ok := apierror.HTTPResponseFromError(err); ok { | ||
_ = httpgrpc.WriteResponse(w, resp) | ||
var ( | ||
res *httpgrpc.HTTPResponse | ||
ok bool | ||
) | ||
|
||
// If the error is an APIError, ensure it gets written as a JSON response. | ||
// Otherwise, check if there's a response encoded in the gRPC error. | ||
res, ok = apierror.HTTPResponseFromError(err) | ||
if !ok { | ||
res, ok = httpgrpc.HTTPResponseFromError(err) | ||
} | ||
|
||
// If we've been able to get the HTTP response from the error, then we send | ||
// it with the right status code and response body content. | ||
if res != nil { | ||
statusCode = int(res.Code) | ||
_ = httpgrpc.WriteResponse(w, res) | ||
return | ||
} | ||
|
||
httpgrpc.WriteError(w, err) | ||
// Otherwise, we do fallback to a 5xx error, returning the non-formatted error | ||
// message in the response body. | ||
statusCode = http.StatusInternalServerError | ||
krajorama marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: I think it works better as a fallback if you put this line at the top of the function, then any time we cannot or miss updating it, the result will be http.StatusInternalServerError There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see the problem but I'm not convinced that having a default is the right thing to do. I want to make sure that we always return the right status code. To solve it, I took a different approach and I removed the named return parameter so that it's intentional what we return: 793436b |
||
http.Error(w, err.Error(), statusCode) | ||
return | ||
} | ||
|
||
func writeServiceTimingHeader(queryResponseTime time.Duration, headers http.Header, stats *querier_stats.Stats) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to reviewers: the logic should have not been changed at all (at least not intentionally). What I'm doing here is exactly what we did before calling
httpgrpc.WriteResponse(w, resp)
andhttpgrpc.WriteError(w, err)
.