Skip to content

Commit

Permalink
Account response read time for http prober
Browse files Browse the repository at this point in the history
Withot this change `transfer` time is near zero, because `trace.end`
time is clocked when response body is available, not when it's read.

Signed-off-by: Ivan Babrou <[email protected]>
  • Loading branch information
bobrik committed Sep 27, 2018
1 parent 9a02cbf commit 6bf5cc5
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions prober/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
}
t.current = trace
t.traces = append(t.traces, trace)
defer func() { trace.end = time.Now() }()
return t.Transport.RoundTrip(req)
}

Expand Down Expand Up @@ -296,10 +295,8 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
if err != nil && resp == nil {
level.Error(logger).Log("msg", "Error for HTTP request", "err", err)
} else {
defer func() {
io.Copy(ioutil.Discard, resp.Body)
resp.Body.Close()
}()
requestErrored := (err != nil)

level.Info(logger).Log("msg", "Received HTTP response", "status_code", resp.StatusCode)
if len(httpConfig.ValidStatusCodes) != 0 {
for _, code := range httpConfig.ValidStatusCodes {
Expand Down Expand Up @@ -327,6 +324,19 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
}
}

if resp != nil && !requestErrored {
_, err = io.Copy(ioutil.Discard, resp.Body)
if err != nil {
level.Info(logger).Log("msg", "Failed to read HTTP response body", "err", err)
success = false
}

resp.Body.Close()
}

// At this point body is fully read and we can write end time.
tt.current.end = time.Now()

var httpVersionNumber float64
httpVersionNumber, err = strconv.ParseFloat(strings.TrimPrefix(resp.Proto, "HTTP/"), 64)
if err != nil {
Expand Down

0 comments on commit 6bf5cc5

Please sign in to comment.