Skip to content

Commit

Permalink
http: add body transfer speed metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
julian7 committed Mar 20, 2020
1 parent e76288f commit 5e55f8f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

No changes so far.
Added:

* http: new measurements: body size, body transfer speed, body transfer errors

## [v0.2.2] - Feb 10. 2020

Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,11 @@ When `--metrics` is provided, it shows the following measurements:
- http.time.connect: time to connect (from start; in microseconds)
- http.time.pretransfer: time to TLS handshake (from start; in microseconds)
- http.time.starttransfer: time to first byte arrived (from start; in microseconds)
- http.time.body_transfer: time from first byte to finish (in microseconds)
- http.http.http_code: returned status code
- http.body_bytes: number of received bytes in HTTP body
- http.http.error: received error while reading HTTP body (`<nil>` if no error received)
- http.speed.body_transfer: body transfer speed (in bytes/s; only if no errors, and non-zero body_transfer and body_bytes values)

Provided tags:

Expand Down
22 changes: 17 additions & 5 deletions cmd/sensu-base-checks/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"crypto/x509"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptrace"
Expand Down Expand Up @@ -167,10 +168,7 @@ func (conf *httpConfig) Run(cmd *cobra.Command, args []string) error {
defer resp.Body.Close()

if conf.Metrics {
conf.tracer.Done()
conf.printMetrics(req, resp)

return nil
return conf.printMetrics(req, resp)
}

if len(conf.Expiry) != 0 {
Expand Down Expand Up @@ -332,8 +330,13 @@ func (conf *httpConfig) checkJSONKey(body []byte) (interface{}, error) {
return raw, nil
}

func (conf *httpConfig) printMetrics(req *http.Request, resp *http.Response) {
func (conf *httpConfig) printMetrics(req *http.Request, resp *http.Response) error {
written, err := io.Copy(ioutil.Discard, resp.Body)

conf.tracer.Done()

log := metrics.New("http").With(map[string]string{"url": conf.URL})
transfer_time := conf.tracer.Finished.Sub(conf.tracer.StartResponding)

log.Log("time.total", conf.tracer.Total().Microseconds())
log.Log("time.namelookup", conf.tracer.Namelookup().Microseconds())
Expand All @@ -344,5 +347,14 @@ func (conf *httpConfig) printMetrics(req *http.Request, resp *http.Response) {
}

log.Log("time.starttransfer", conf.tracer.Starttransfer().Microseconds())
log.Log("time.body_transfer", transfer_time.Microseconds())
log.Log("http.http_code", resp.StatusCode)
log.Log("http.body_bytes", written)
log.Log("http.error", err)

if err == nil && transfer_time > 0 && written > 0 {
log.Log("speed.body_transfer", float64(written)/transfer_time.Seconds())
}

return nil
}

0 comments on commit 5e55f8f

Please sign in to comment.