feat: per-scraper query timeout to prevent slow scraper from cascading-failing the rest#1025
Open
AadiSanghani wants to merge 3 commits into
Open
feat: per-scraper query timeout to prevent slow scraper from cascading-failing the rest#1025AadiSanghani wants to merge 3 commits into
AadiSanghani wants to merge 3 commits into
Conversation
0b4b13a to
3946eb0
Compare
Author
|
cc: @SuperQ |
3946eb0 to
438876e
Compare
| db.SetMaxOpenConns(1) | ||
| db.SetMaxOpenConns(2) | ||
| db.SetMaxIdleConns(1) | ||
| db.SetConnMaxLifetime(1 * time.Minute) |
There was a problem hiding this comment.
I'm wondering if these changes are related and required to the main idea behind this PR.
Author
There was a problem hiding this comment.
Removed the db.SetConnMaxLifetime(1 * time.Minute) as its not needed for this PR.
|
|
||
| if err := instance.Ping(); err != nil { | ||
| pingCtx := ctx | ||
| if e.queryTimeout > 0 { |
There was a problem hiding this comment.
I'm wondering if we can consolidate this check queryTimeout > 0 and thinking if we really need a context for ping and another for scrape, since they are using the same timeout value.
Author
There was a problem hiding this comment.
Fair! I moved them out to its method now.
|
@AadiSanghani can you check DCO 🙏🏽 |
3ff2bb6 to
25add20
Compare
Signed-off-by: Aadi Sanghani <aadi.sanghani@shopify.com>
Signed-off-by: Aadi Sanghani <aadi.sanghani@shopify.com>
Co-authored-by: Nicolas Takashi <nicolas.tcs@hotmail.com> Signed-off-by: Aadi Sanghani <42876004+AadiSanghani@users.noreply.github.com> Signed-off-by: Aadi Sanghani <aadi.sanghani@shopify.com>
25add20 to
74a9ded
Compare
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.
Summary
A single slow scraper currently causes every other scraper to fail on the same
/metricsrequest. Two coupled changes fix this:Per-scraper context timeout — each scraper gets its own
context.WithTimeout(parent, --exporter.query_timeout)instead of sharing the request context. A new--exporter.query_timeoutflag controlsthe timeout. Default is
0(disabled), so existing deployments see no behavior change.Connection pool capacity —
MaxOpenConnsraised from1to2andConnMaxLifetimeset to1m. With only one connection, scrapers serialize behind a slow query and timeout while waiting for theconnection rather than while running, which the per-scraper timeout cannot catch on its own.
The
instance.Ping()call also takes a context now and usesdb.PingContextso the ping respects the same timeout.Why both changes are needed
Per-scraper timeout alone is not sufficient. With
MaxOpenConns(1), fast scrapers queue behind a slow one waiting for the connection, then hit their per-scraper deadline while still in the queue and failwith
context deadline exceeded. Bumping the pool to 2 lets the fast scrapers run on a second connection while the slow one is in flight, so the per-scraper timeout fires only on the genuinely slowscraper.
Reproduction
A scraper that runs
SELECT SLEEP(15)was added locally to reproduce the failure mode against MySQL 8.4 with a 5s scrape budget (X-Prometheus-Scrape-Timeout-Seconds: 5).MaxOpenConns=1, no per-scraper timeout (pre-fix)MaxOpenConns=2+--exporter.query_timeout=3(post-fix)Pre-fix uses the entire scrape budget regardless of what's actually slow; post-fix uses exactly the per-scraper timeout that's configured.
Also full credit to @joshbroughton for this PR.