Skip to content
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

Do not apply offsets for explicit timeouts #492

Merged
merged 7 commits into from
Jun 21, 2019
Merged
Show file tree
Hide file tree
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
43 changes: 27 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,12 @@ func probeHandler(w http.ResponseWriter, r *http.Request, c *config.Config, logg
return
}

// If a timeout is configured via the Prometheus header, add it to the request.
var timeoutSeconds float64
if v := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"); v != "" {
var err error
timeoutSeconds, err = strconv.ParseFloat(v, 64)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to parse timeout from Prometheus header: %s", err), http.StatusInternalServerError)
return
}
}
if timeoutSeconds == 0 {
timeoutSeconds = 10
timeoutSeconds, err := getTimeout(r, module, *timeoutOffset)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to parse timeout from Prometheus header: %s", err), http.StatusInternalServerError)
return
}

if module.Timeout.Seconds() < timeoutSeconds && module.Timeout.Seconds() > 0 {
timeoutSeconds = module.Timeout.Seconds()
}
timeoutSeconds -= *timeoutOffset
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutSeconds*float64(time.Second)))
defer cancel()
r = r.WithContext(ctx)
Expand Down Expand Up @@ -355,3 +343,26 @@ func run() int {
}

}

func getTimeout(r *http.Request, module config.Module, offset float64) (timeoutSeconds float64, err error) {
// If a timeout is configured via the Prometheus header, add it to the request.
if v := r.Header.Get("X-Prometheus-Scrape-Timeout-Seconds"); v != "" {
var err error
timeoutSeconds, err = strconv.ParseFloat(v, 64)
if err != nil {
return 0, err
}
}
if timeoutSeconds == 0 {
timeoutSeconds = 10
}

var maxTimeoutSeconds = timeoutSeconds - offset
if module.Timeout.Seconds() < maxTimeoutSeconds && module.Timeout.Seconds() > 0 {
timeoutSeconds = module.Timeout.Seconds()
} else {
timeoutSeconds = maxTimeoutSeconds
}

return timeoutSeconds, nil
}
35 changes: 35 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,38 @@ func TestDebugOutputSecretsHidden(t *testing.T) {
t.Errorf("Hidden secret missing from debug output: %v", out)
}
}

func TestTimeoutIsSetCorrectly(t *testing.T) {
var tests = []struct {
inModuleTimeout time.Duration
inPrometheusTimeout string
inOffset float64
outTimeout float64
}{
{0 * time.Second, "15", 0.5, 14.5},
{0 * time.Second, "15", 0, 15},
{20 * time.Second, "15", 0.5, 14.5},
{20 * time.Second, "15", 0, 15},
{5 * time.Second, "15", 0, 5},
{5 * time.Second, "15", 0.5, 5},
{10 * time.Second, "", 0.5, 9.5},
{10 * time.Second, "10", 0.5, 9.5},
{9500 * time.Millisecond, "", 0.5, 9.5},
{9500 * time.Millisecond, "", 1, 9},
{0 * time.Second, "", 0.5, 9.5},
{0 * time.Second, "", 0, 10},
}

for _, v := range tests {
request, _ := http.NewRequest("GET", "", nil)
request.Header.Set("X-Prometheus-Scrape-Timeout-Seconds", v.inPrometheusTimeout)
module := config.Module{
Timeout: v.inModuleTimeout,
}

timeout, _ := getTimeout(request, module, v.inOffset)
if timeout != v.outTimeout {
t.Errorf("timeout is incorrect: %v, want %v", timeout, v.outTimeout)
}
}
}