cmd/scollector: httpunit - rescode metric and url metadata
also checks nils to avoid possibility of panics
@@ -4,6 +4,8 @@ import (
"fmt"
"time"
+ "bosun.org/slog"
+
"bosun.org/metadata"
"bosun.org/opentsdb"
"github.com/BurntSushi/toml"
@@ -46,16 +48,32 @@ func cHTTPUnit(plans *httpunit.Plans) (opentsdb.MultiDataPoint, error) {
unix_now := time.Now().Unix()
var md opentsdb.MultiDataPoint
for r := range ch {
+ if r == nil || r.Case == nil || r.Plan == nil {
+ slog.Errorf("httpunit: unexpected test plan on channel, the plan result, the case, and/or the plan result is missing.")
+ continue
+ }
tags := opentsdb.TagSet{
"protocol": r.Case.URL.Scheme,
"ip": r.Case.IP.String(),
"url_host": r.Case.URL.Host,
"hc_test_case": r.Plan.Label,
}
+ metadata.AddMeta("", opentsdb.TagSet{"hc_test_case": r.Plan.Label}, "url", r.Case.URL.String(), false)
+ if r.Result == nil || r.Result.Result != nil {
+ Add(&md, "hu.error", true, tags, metadata.Gauge, metadata.Bool, descHTTPUnitError)
+ if r.Result == nil {
+ continue
+ }
+ }
+ Add(&md, "hu.error", false, tags, metadata.Gauge, metadata.Bool, descHTTPUnitError)
ms := int64(r.Result.TimeTotal / time.Millisecond)
- Add(&md, "hu.error", r.Result.Result != nil, tags, metadata.Gauge, metadata.Bool, descHTTPUnitError)
Add(&md, "hu.socket_connected", r.Result.Connected, tags, metadata.Gauge, metadata.Bool, descHTTPUnitSocketConnected)
Add(&md, "hu.time_total", ms, tags, metadata.Gauge, metadata.MilliSecond, descHTTPUnitTotalTime)
+ code := 0
+ if r.Result.Resp != nil {
+ code = r.Result.Resp.StatusCode
+ }
+ Add(&md, "hu.response_code", code, tags, metadata.Gauge, metadata.StatusCode, descHTTPUnitStatusCode)
switch r.Case.URL.Scheme {
case "http", "https":
Add(&md, "hu.http.got_expected_code", r.Result.GotCode, tags, metadata.Gauge, metadata.Bool, descHTTPUnitExpectedCode)
@@ -84,4 +102,5 @@ const (
descHTTPUnitCertExpires = "Unix epoch time of the certificate expiration."
descHTTPUnitCertValidFor = "Number of seconds until certificate expiration."
descHTTPUnitTotalTime = "Total time consumed by test case."
+ descHTTPUnitStatusCode = "The HTTP status response code of the response, or 0 if no response."
)
