Skip to content

PMM-14141: Respect Prometheus scrape-timeout header #1107

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,13 @@
e.clientMu.Lock()
defer e.clientMu.Unlock()

// If client is already initialized, return it.
// If client is already initialized, and Ping is successful -- return it.
if e.client != nil {
err := e.client.Ping(ctx, nil)
if err != nil {
return nil, fmt.Errorf("cannot connect to MongoDB: %w", err)
}

Check warning on line 281 in exporter/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporter/exporter.go#L280-L281

Added lines #L280 - L281 were not covered by tests

return e.client, nil
}

Expand All @@ -300,20 +305,25 @@
// run for hooking up custom HTTP servers.
func (e *Exporter) Handler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
seconds, err := strconv.Atoi(r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"))
// To support older ones vmagents.
if err != nil {
seconds = 10
scrapeTimeoutHeader := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds")
seconds := 10.0

if scrapeTimeoutHeader != "" {
if parsedSeconds, err := strconv.ParseFloat(scrapeTimeoutHeader, 64); err == nil {
seconds = parsedSeconds
} else {
e.logger.Info("Invalid X-Prometheus-Scrape-Timeout-Seconds header", "error", err)
}

Check warning on line 316 in exporter/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporter/exporter.go#L312-L316

Added lines #L312 - L316 were not covered by tests
}
seconds -= e.opts.TimeoutOffset
seconds -= float64(e.opts.TimeoutOffset)

var client *mongo.Client
ctx, cancel := context.WithTimeout(r.Context(), time.Duration(seconds)*time.Second)
ctx, cancel := context.WithTimeout(r.Context(), time.Duration(seconds*float64(time.Second)))
defer cancel()

requestOpts := GetRequestOpts(r.URL.Query()["collect[]"], e.opts)

client, err = e.getClient(ctx)
client, err := e.getClient(ctx)
if err != nil {
e.logger.Error("Cannot connect to MongoDB", "error", err)
}
Expand Down