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

Please expose body when handling HTTP errors in the v1 package #479

Closed
porridge opened this issue Oct 11, 2018 · 12 comments
Closed

Please expose body when handling HTTP errors in the v1 package #479

porridge opened this issue Oct 11, 2018 · 12 comments
Assignees

Comments

@porridge
Copy link
Contributor

The code here does not expose body on encountering an HTTP error. This makes it hard to troubleshoot issues such as a typo in the prometheus server address when using an HTTP proxy. In that case the proxy returns a helpful message in the HTTP error response body, but this library does not propagate this back.

One way to resolve this would be to add a Detail field to the Error type and fill it with the stringified body here.

@beorn7
Copy link
Member

beorn7 commented Oct 11, 2018

@krasi-georgiev this one is for you again... :o)

@krasi-georgiev
Copy link
Contributor

@porridge I just had a quick look and when there is an error the golang http library doesn't return any Body either. so it is one or the other. Meaning even if we expose it will be empty.

Did you try it locally to see if Body does include anything when the Do method returns an error?

@porridge
Copy link
Contributor Author

porridge commented Oct 11, 2018 via email

@porridge
Copy link
Contributor Author

porridge commented Oct 11, 2018 via email

@krasi-georgiev
Copy link
Contributor

Maybe it depends on the golang version, I checked the go 1.11 implementation.
Could you try go 1.11 as well and let me know.

@krasi-georgiev
Copy link
Contributor

krasi-georgiev commented Oct 11, 2018

one other option is to try with error assertion.

if ok := err.(*url.Error) ;ok ....

@krasi-georgiev
Copy link
Contributor

I mean I hacked the code a little and checked that it does.

just to confirm when you are testing you should test that error is not nil and at the same time the body does contain what you are after..

@porridge
Copy link
Contributor Author

porridge commented Oct 11, 2018 via email

@porridge
Copy link
Contributor Author

porridge commented Oct 11, 2018 via email

porridge added a commit to porridge/client_golang that referenced this issue Oct 12, 2018
This is to illustrate prometheus#479

For this to run, you need to serve an HTTP 500 error on localhost:80.
I did this with the following nginx snippet:
```
	error_page 500 /500.html;
	location /api/v1/query {
		return 500;
	}
```
and the following file in `/var/www/html`:
```
$ head /var/www/html/500.html
important troubleshooting detail
$
```

Then running the test suite shows:
```
--- FAIL: TestReproIssue479 (0.00s)
    api_test.go:720: querying failed with [bad_response: bad response code 500]. Details: [important troubleshooting detail]
FAIL
```
@porridge
Copy link
Contributor Author

porridge commented Oct 12, 2018

@krasi-georgiev Please take a look at porridge@5b54e79

In that case I'm serving a 500 right at the destination server, while this request is for the case of 500 being returned by a proxy, but the mechanics are exactly the same.

@krasi-georgiev
Copy link
Contributor

what you are suggesting is reasonable. PR is welcome.
Maybe even add it to
Msg: fmt.Sprintf("response code:%d, body:%s", resp.StatusCode,body),

@porridge
Copy link
Contributor Author

porridge commented Oct 12, 2018 via email

porridge added a commit to porridge/client_golang that referenced this issue Oct 23, 2018
porridge added a commit to porridge/client_golang that referenced this issue Oct 23, 2018
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

No branches or pull requests

3 participants