-
Notifications
You must be signed in to change notification settings - Fork 10
/
metrics_test.go
120 lines (106 loc) · 2.86 KB
/
metrics_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"bytes"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
)
func TestHandlerMetrics(t *testing.T) {
c := defaultCfg
// Set a non-zero Prometheus port so that the metrics middleware is
// installed.
c.PrometheusPort = 80
enclave := createEnclave(&c)
makeReq := makeReqToSrv(enclave.extPubSrv)
// GET /enclave/config
assertResponse(t,
makeReq(http.MethodGet, pathConfig, nil),
newResp(http.StatusOK, c.String()),
)
// Make sure that Prometheus recorded the above request.
labels := enclave.metrics.reqs.WithLabelValues
assertEqual(t, testutil.ToFloat64(labels(
pathConfig,
http.MethodGet,
fmt.Sprint(http.StatusOK),
notAvailable),
), float64(1))
// POST /enclave/config
assertResponse(t,
makeReq(http.MethodPost, pathConfig, nil),
newResp(http.StatusMethodNotAllowed, ""),
)
// Again, make sure that Prometheus recorded the above request.
assertEqual(t, testutil.ToFloat64(labels(
pathConfig,
http.MethodPost,
fmt.Sprint(http.StatusMethodNotAllowed),
notAvailable),
), float64(1))
// POST /enclave/hash
makeReq = makeReqToSrv(enclave.intSrv)
assertResponse(t,
makeReq(http.MethodPost, pathHash, bytes.NewBufferString("foo")),
newResp(http.StatusBadRequest, errNoBase64.Error()),
)
// One final time, make sure that Prometheus recorded the above request.
assertEqual(t, testutil.ToFloat64(labels(
pathHash,
http.MethodPost,
fmt.Sprint(http.StatusBadRequest),
notAvailable),
), float64(1))
}
func TestMetrics(t *testing.T) {
err1, err2 := errors.New("backend timeout"), errors.New("backend exploded")
expectedStatus1, expectedStatus2 := 200, 404
expectedPath := "/foo"
expectedMethod := http.MethodGet
reg := prometheus.NewRegistry()
m := newMetrics(reg, "nitriding")
req, err := http.NewRequest(expectedMethod, expectedPath, nil)
if err != nil {
t.Fatalf("Failed to create new HTTP request: %v", err)
}
r := httptest.NewRecorder()
m.checkRevProxyErr(r, req, err1)
m.checkRevProxyErr(r, req, err1)
m.checkRevProxyErr(r, req, err2)
labels := m.proxiedReqs.WithLabelValues
assertEqual(t, testutil.ToFloat64(labels(
expectedPath,
expectedMethod,
notAvailable,
err1.Error()),
), float64(2))
assertEqual(t, testutil.ToFloat64(labels(
expectedPath,
expectedMethod,
notAvailable,
err2.Error()),
), float64(1))
_ = m.checkRevProxyResp(&http.Response{
StatusCode: expectedStatus1,
Request: req,
})
_ = m.checkRevProxyResp(&http.Response{
StatusCode: expectedStatus2,
Request: req,
})
assertEqual(t, testutil.ToFloat64(labels(
expectedPath,
expectedMethod,
fmt.Sprint(expectedStatus1),
notAvailable),
), float64(1))
assertEqual(t, testutil.ToFloat64(labels(
expectedPath,
expectedMethod,
fmt.Sprint(expectedStatus2),
notAvailable),
), float64(1))
}