Skip to content

cmd/prom2json: exit non-zero when reading the metrics fails - #303

Open
vignesh2027 wants to merge 1 commit into
prometheus:masterfrom
vignesh2027:cmd-exit-on-fetch-error
Open

vignesh2027 wants to merge 1 commit into
prometheus:masterfrom
vignesh2027:cmd-exit-on-fetch-error

Conversation

@vignesh2027

Copy link
Copy Markdown

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.

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>
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.

1 participant