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
Expose prometheus metrics at /metrics endpoint #168
Expose prometheus metrics at /metrics endpoint #168
Conversation
Hi @lebaptiste, thanks a lot for contributing! First of all, yes, the omission of Also, thanks to your PR, I discovered some invalid paths in the metrics example that we overlooked #169 Regarding the use of the gochannel, you're right, probably So, regarding this PR: |
Also the test_race and test_short are not passing, but I ran them locally and did not have any issue. I suppose the port 8080 is already used in you docker compose. Picking an other port is simple enough but happy to discuss if there is a preferable way for the tests. Thanks. |
} | ||
|
||
go func() { | ||
err := server.ListenAndServe() | ||
if err != nil { | ||
if err != http.ErrServerClosed { |
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.
This will panic for a nil err too.
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.
server.ListenAndServe is guaranteeing to return non-nil error. Checking for nil might be more explicit though.
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.
Ah, indeed, I only checked the error handling, but not what it actually handles.
@lebaptiste probably About the test still failing, even with port This could happen if the tests are being run in parallel, or the port is not released properly after |
// server might have small delay before being able to server traffic | ||
func waitServerReady(t *testing.T, addr string) { | ||
for i := 0; i < 50; i++ { | ||
_, err := http.DefaultClient.Get(addr) |
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.
I'd suggest not using the default client because he has no timeouts set https://golang.org/src/net/http/client.go line 110
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.
Thanks for the feedback, it was a while and I forgot a bit about this PR.
For the default http client, I agree with you and I would not use it for production code. However since we are in tests, I was considering leaving the default test timeout handling this instead, I found it simpler. What do you think?
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.
ok, tests have their timeouts so it can stay as it is
Thanks for the feedbacks, I got some issues with the port setup and using different ports as suggested has fixed it 👍 |
I think that this message is the most important
|
} | ||
waitServerReady(t, "http://localhost:8090") | ||
resp, err := http.DefaultClient.Get("http://localhost:8090/metrics") | ||
if resp != nil { |
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.
+1 for checking the resp before the error :)
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.
In fact I did this by habit, however if both err and response are returned, you don't have to close the body.
https://stackoverflow.com/questions/32818472/do-we-need-to-close-the-response-object-if-an-error-occurs-while-calling-http-ge
I have no idea why this PR was stuck for such a long time 🤔 Thanks for the contribution! |
The chi.Router is not used and I believe it is not intentional.
I have added tests to cover the expected behaviour.
go fmt
has added a small diff inpubsub.go
.Also out of curiosity, is there any benefit to do
instead of directly?
Thanks