Added metric for http/tcp regex match (#191)
authorConor Broderick <conor.broderick@robustperception.io>
Wed, 12 Jul 2017 13:31:37 +0000 (14:31 +0100)
committerBrian Brazil <brian.brazil@robustperception.io>
Wed, 12 Jul 2017 13:31:37 +0000 (14:31 +0100)
http.go
http_test.go
tcp.go
tcp_test.go
utils_test.go

diff --git a/http.go b/http.go
index 8c75d2e5d4249aff6ce1d6e18ab04c11a6587e57..bd1f37a3d981e1bef2189a86e954e470129d28c9 100644 (file)
--- a/http.go
+++ b/http.go
@@ -91,6 +91,11 @@ func probeHTTP(ctx context.Context, target string, module Module, registry *prom
                        Name: "probe_http_version",
                        Help: "Returns the version of HTTP of the probe response",
                })
+
+               probeFailedDueToRegex = prometheus.NewGauge(prometheus.GaugeOpts{
+                       Name: "probe_failed_due_to_regex",
+                       Help: "Indicates if probe failed due to regex",
+               })
        )
 
        registry.MustRegister(contentLengthGauge)
@@ -98,6 +103,7 @@ func probeHTTP(ctx context.Context, target string, module Module, registry *prom
        registry.MustRegister(isSSLGauge)
        registry.MustRegister(statusCodeGauge)
        registry.MustRegister(probeHTTPVersionGauge)
+       registry.MustRegister(probeFailedDueToRegex)
 
        httpConfig := module.HTTP
 
@@ -186,6 +192,11 @@ func probeHTTP(ctx context.Context, target string, module Module, registry *prom
 
                if success && (len(httpConfig.FailIfMatchesRegexp) > 0 || len(httpConfig.FailIfNotMatchesRegexp) > 0) {
                        success = matchRegularExpressions(resp.Body, httpConfig)
+                       if success {
+                               probeFailedDueToRegex.Set(0)
+                       } else {
+                               probeFailedDueToRegex.Set(1)
+                       }
                }
 
                var httpVersionNumber float64
index f897dfd8f65ba8ef4f77d56a3ad9532043627175..eec5c1bbf91b91229459326fff6236473d2a8494 100644 (file)
@@ -241,6 +241,14 @@ func TestFailIfMatchesRegexp(t *testing.T) {
        if result {
                t.Fatalf("Regexp test succeeded unexpectedly, got %s", body)
        }
+       mfs, err := registry.Gather()
+       if err != nil {
+               t.Fatal(err)
+       }
+       expectedResults := map[string]float64{
+               "probe_failed_due_to_regex": 1,
+       }
+       checkRegistryResults(expectedResults, mfs, t)
 
        ts = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                fmt.Fprintf(w, "Download the latest version here")
@@ -255,6 +263,14 @@ func TestFailIfMatchesRegexp(t *testing.T) {
        if !result {
                t.Fatalf("Regexp test failed unexpectedly, got %s", body)
        }
+       mfs, err = registry.Gather()
+       if err != nil {
+               t.Fatal(err)
+       }
+       expectedResults = map[string]float64{
+               "probe_failed_due_to_regex": 0,
+       }
+       checkRegistryResults(expectedResults, mfs, t)
 
        // With multiple regexps configured, verify that any matching regexp causes
        // the probe to fail, but probes succeed when no regexp matches.
diff --git a/tcp.go b/tcp.go
index 4a3138718a12cb4f73a1a3087aa80c4b908acdd1..61dc9fe18e471376ee7bface6d35ef88c67045e3 100644 (file)
--- a/tcp.go
+++ b/tcp.go
@@ -68,6 +68,11 @@ func probeTCP(ctx context.Context, target string, module Module, registry *prome
                Name: "probe_ssl_earliest_cert_expiry",
                Help: "Returns earliest SSL cert expiry date",
        })
+       probeFailedDueToRegex := prometheus.NewGauge(prometheus.GaugeOpts{
+               Name: "probe_failed_due_to_regex",
+               Help: "Indicates if probe failed due to regex",
+       })
+       registry.MustRegister(probeFailedDueToRegex)
        deadline := time.Now().Add(module.Timeout)
        conn, err := dialTCP(ctx, target, module, registry)
        if err != nil {
@@ -112,8 +117,10 @@ func probeTCP(ctx context.Context, target string, module Module, registry *prome
                                return false
                        }
                        if match == nil {
+                               probeFailedDueToRegex.Set(1)
                                return false
                        }
+                       probeFailedDueToRegex.Set(0)
                        send = string(re.Expand(nil, []byte(send), scanner.Bytes(), match))
                }
                if send != "" {
index 222ab06badf1f8f8da43f85eabf2052abcd81a4a..7c2367c508114485e5afbfb68c239d8c329e4535 100644 (file)
@@ -117,6 +117,14 @@ func TestTCPConnectionQueryResponseIRC(t *testing.T) {
        if probeTCP(testCTX, ln.Addr().String(), module, registry) {
                t.Fatalf("TCP module succeeded, expected failure.")
        }
+       mfs, err := registry.Gather()
+       if err != nil {
+               t.Fatal(err)
+       }
+       expectedResults := map[string]float64{
+               "probe_failed_due_to_regex": 1,
+       }
+       checkRegistryResults(expectedResults, mfs, t)
        <-ch
 }
 
@@ -162,6 +170,15 @@ func TestTCPConnectionQueryResponseMatching(t *testing.T) {
        if got, want := <-ch, "OpenSSH_6.9p1"; got != want {
                t.Fatalf("Read unexpected version: got %q, want %q", got, want)
        }
+       mfs, err := registry.Gather()
+       if err != nil {
+               t.Fatal(err)
+       }
+       expectedResults := map[string]float64{
+               "probe_failed_due_to_regex": 0,
+       }
+       checkRegistryResults(expectedResults, mfs, t)
+
 }
 
 func TestTCPConnectionProtocol(t *testing.T) {
index 1cbc4f0d9a8d9af9e0d5f43bba125f8df3fad7d8..c121c583ee39424202bd3d9156449a76b5186afb 100644 (file)
@@ -13,7 +13,11 @@ func checkRegistryResults(expRes map[string]float64, mfs []*dto.MetricFamily, t
                res[mfs[i].GetName()] = mfs[i].Metric[0].GetGauge().GetValue()
        }
        for k, v := range expRes {
-               if val, ok := res[k]; !ok || val != v {
+               val, ok := res[k]
+               if !ok {
+                       t.Fatalf("Expected metric %v not found in returned metrics", k)
+               }
+               if val != v {
                        t.Fatalf("Expected: %v: %v, got: %v: %v", k, v, k, val)
                }
        }