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

Fix Content-Type header in /healthz when status is not 200 OK #4437

Merged
merged 1 commit into from Aug 31, 2023

Conversation

mdawar
Copy link
Contributor

@mdawar mdawar commented Aug 26, 2023

The function ResponseHandler that handles the monitoring server responses is exported so I had to add another internal function to prevent a breaking change to the API.
I don't know if dependent code should use this function, maybe in the next major version this function should be unexported because it's only used for the monitoring endpoints.

  • You have certified that the contribution is your original work and that you license the work to the project under the Apache 2 license

Resolves #4436

@mdawar mdawar requested a review from a team as a code owner August 26, 2023 10:34
@Jarema Jarema closed this Aug 26, 2023
@Jarema Jarema reopened this Aug 26, 2023
Copy link
Member

@bruth bruth left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the contribution!

@wallyqs wallyqs changed the title Fix HTTP headers not set for monitoring server when status is not 200 OK Fix Content-Type header for monitoring server when status is not 200 OK in healthz Aug 28, 2023
@wallyqs wallyqs changed the title Fix Content-Type header for monitoring server when status is not 200 OK in healthz Fix Content-Type header in /healthz when status is not 200 OK Aug 28, 2023
Copy link
Member

@wallyqs wallyqs left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the contribution. I will make a follow up PR to include this into v2.10 dev branch here: dev...healthz-mon-fixes

func ResponseHandler(w http.ResponseWriter, r *http.Request, data []byte) {
handleResponse(http.StatusOK, w, r, data)
Copy link
Member

Choose a reason for hiding this comment

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

Note that we currently do not use this handler in most monitoring APIs when serving error responses (one of the reasons why we missed setting header right in /healthz)

c, err := s.Connz(connzOpts)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return

Copy link
Contributor Author

Choose a reason for hiding this comment

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

BTW shouldn't error responses be also returned as JSON for more consistency? Maybe in the next major version? Because this change might break the current consumers of the API.

Copy link
Member

Choose a reason for hiding this comment

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

yes this could be done in a different version, maybe in the dev branch for next release cycle

@@ -2301,19 +2301,26 @@ func (s *Server) HandleAccountStatz(w http.ResponseWriter, r *http.Request) {
ResponseHandler(w, r, b)
}

// ResponseHandler handles responses for monitoring routes
// ResponseHandler handles responses for monitoring routes.
Copy link
Member

Choose a reason for hiding this comment

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

It was not necessary to have this exported but it has been public for quite some time already, introduced in https://github.com/nats-io/nats-server/pull/103/files


// Another check for JSONP requests.
jsonpURL := createCallbackURL(t, url) // Adds a callback query param.
jsonpBody := readBodyEx(t, jsonpURL, statusCode, appJSContent)
Copy link
Member

Choose a reason for hiding this comment

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

as you may have found readBodyEx currently uses misses the line of the error which can be fixed by using t.Helper, fixing that in a separate PR when including this change

@wallyqs wallyqs merged commit 6d6d3cf into nats-io:main Aug 31, 2023
1 check passed
derekcollison added a commit that referenced this pull request Sep 1, 2023
Follow up from #4437 content-type fix for v2.9.22, some fixes to the
response from `/healthz` for dev:

- In #[3326](#4097) it was
changed to return 500 status when before we used to return 503 so this
changes it back.
- Also as part of #3326 we started to return `status_code` in the
healthz response (e.g `{"status":"ok","status_code":200}`) so this
removes it for http responses just relying on the http header.
wallyqs pushed a commit that referenced this pull request Sep 1, 2023
- Added a new internal function `handleResponse` that accepts the HTTP 
  status code and sets it after setting the headers
- Added tests for the `/healthz` endpoint for the `ok`, `error` and `unavailable` statuses
- Changed the IETF API health check URL to 
https://datatracker.ietf.org/doc/html/draft-inadarei-api-health-check

Resolves #4436
@mdawar mdawar deleted the monitoring-headers branch September 1, 2023 11:16
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.

HTTP headers are not set for the monitoring server when the status is not 200 OK
4 participants