Skip to content

Commit

Permalink
Do not apply offsets for explicit timeouts (#492)
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Dabasinskas <[email protected]>
  • Loading branch information
tdabasinskas authored and brian-brazil committed Jun 21, 2019
1 parent dc53582 commit 8ad0722
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 16 deletions.
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)
}
}
}

0 comments on commit 8ad0722

Please sign in to comment.