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

Account response read time for http prober #363

Merged
merged 1 commit into from
Oct 4, 2018
Merged
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: 21 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()
SuperQ marked this conversation as resolved.
Show resolved Hide resolved

var httpVersionNumber float64
httpVersionNumber, err = strconv.ParseFloat(strings.TrimPrefix(resp.Proto, "HTTP/"), 64)
if err != nil {
Expand Down Expand Up @@ -385,6 +395,12 @@ func ProbeHTTP(ctx context.Context, target string, module config.Module, registr
continue
}
durationGaugeVec.WithLabelValues("processing").Add(trace.responseStart.Sub(trace.gotConn).Seconds())

// Continue here if we never read the full response from the server.
// Usually this means that request either failed or was redirected.
if trace.end.IsZero() {
continue
}
durationGaugeVec.WithLabelValues("transfer").Add(trace.end.Sub(trace.responseStart).Seconds())
}

Expand Down