Repository navigation
cmd/prom2json: exit non-zero when reading the metrics fails - #303
Open
vignesh2027 wants to merge 1 commit into
Open
vignesh2027 wants to merge 1 commit into
vignesh2027 wants to merge 1 commit into
Conversation
ParseReader and FetchMetricFamilies close the channel before they return an error. main only stopped at the end of the range loop, so it raced with the goroutine that printed the error and called os.Exit(1). When main won, prom2json printed [] with no message and exited 0, which is easy to hit against an endpoint returning HTTP 500. Collect the families in a helper that waits for the producer's error after draining the channel, and report it from main. Signed-off-by: vigneshwar <applemacbook6sep2004@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the metrics can't be read, prom2json sometimes prints
[]and exits 0 without any message on stderr. ParseReader and FetchMetricFamilies close the channel before they return the error, so main leaves the range loop and races with the goroutine that is about to print the error and call os.Exit(1).It's easy to see against an endpoint that answers with HTTP 500. In a loop of 2000 runs on my machine, between a few percent and 40% of them exited 0 depending on load. Scripts that check the exit code then treat a failed scrape as an empty one.
This moves the collection into a small helper that drains the channel and then waits for the producer's error, and main reports it as before. The messages on stderr are unchanged. With the change, 2000 runs against the same endpoint all exit 1.
The new tests in cmd/prom2json cover an HTTP error, malformed text input and a normal parse.
I left CHANGELOG.md alone since entries seem to be written when cutting a release. Happy to add one if you prefer.