-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathstats_test.go
131 lines (104 loc) · 5.74 KB
/
stats_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
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/prometheus/client_golang/prometheus/testutil"
"go.uber.org/mock/gomock"
)
func TestStatsResponseWriterWritesResponseMetricOnce(t *testing.T) {
responseSuccessCounter.Reset()
responseStatusCounter.Reset()
responseSuccessGauge.Reset()
responseStatusGauge.Reset()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
recorder := httptest.NewRecorder()
statsWriter := newStatsWriter(recorder, "myhandler")
statsWriter.WriteHeader(http.StatusBadRequest)
if recorder.Code != http.StatusBadRequest {
t.Fatalf("expected status code %d, got %d", http.StatusBadRequest, recorder.Code)
}
statsWriter.WriteHeader(http.StatusCreated)
if recorder.Code != http.StatusBadRequest {
t.Fatalf("tried to write to the headers again: Expected status code %d, got %d", http.StatusBadRequest, recorder.Code)
}
if testutil.ToFloat64(responseSuccessCounter.WithLabelValues("myhandler", "client_failure")) != float64(1) {
t.Fatalf("Expected responseSuccessCounter to be 1, got %f", testutil.ToFloat64(responseSuccessCounter.WithLabelValues("myhandler", "client_failure")))
}
if testutil.ToFloat64(responseStatusCounter.WithLabelValues("myhandler", "400")) != float64(1) {
t.Fatalf("Expected responseStatusCounter to be 1, got %f", testutil.ToFloat64(responseStatusCounter.WithLabelValues("myhandler", "400")))
}
// With Gauge Metric
if testutil.ToFloat64(responseSuccessGauge.WithLabelValues("myhandler", "client_failure")) != float64(1) {
t.Fatalf("Expected responseSuccessGauge to be 1, got %f", testutil.ToFloat64(responseSuccessGauge.WithLabelValues("myhandler", "client_failure")))
}
if testutil.ToFloat64(responseStatusGauge.WithLabelValues("myhandler", "400")) != float64(1) {
t.Fatalf("Expected responseStatusGauge to be 1, got %f", testutil.ToFloat64(responseStatusGauge.WithLabelValues("myhandler", "400")))
}
}
func TestStatsResponseWriterWritesToHeaderOnWrite(t *testing.T) {
responseSuccessCounter.Reset()
responseStatusCounter.Reset()
responseSuccessGauge.Reset()
responseStatusGauge.Reset()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
recorder := httptest.NewRecorder()
statsWriter := newStatsWriter(recorder, "myhandler")
statsWriter.Write([]byte("hello"))
if recorder.Code != http.StatusOK {
t.Fatalf("expected status code %d, got %d", http.StatusOK, recorder.Code)
}
if testutil.ToFloat64(responseSuccessCounter.WithLabelValues("myhandler", "success")) != float64(1) {
t.Fatalf("Expected responseSuccessCounter to be 1, got %f", testutil.ToFloat64(responseSuccessCounter.WithLabelValues("myhandler", "success")))
}
if testutil.ToFloat64(responseStatusCounter.WithLabelValues("myhandler", "200")) != float64(1) {
t.Fatalf("Expected responseStatusCounter to be 1, got %f", testutil.ToFloat64(responseStatusCounter.WithLabelValues("myhandler", "200")))
}
// With Gauge Metric
if testutil.ToFloat64(responseSuccessGauge.WithLabelValues("myhandler", "success")) != float64(1) {
t.Fatalf("Expected responseSuccessGauge to be 1, got %f", testutil.ToFloat64(responseSuccessGauge.WithLabelValues("myhandler", "success")))
}
if testutil.ToFloat64(responseStatusGauge.WithLabelValues("myhandler", "200")) != float64(1) {
t.Fatalf("Expected responseStatusGauge to be 1, got %f", testutil.ToFloat64(responseStatusGauge.WithLabelValues("myhandler", "200")))
}
}
func TestWrappingStatsResponseWriteWritesAllMetrics(t *testing.T) {
responseSuccessCounter.Reset()
responseStatusCounter.Reset()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
recorder := httptest.NewRecorder()
inner := newStatsWriter(recorder, "inner")
wrapper := newStatsWriter(inner, "wrapper")
wrapper.WriteHeader(http.StatusInternalServerError)
if recorder.Code != http.StatusInternalServerError {
t.Fatalf("expected status code %d, got %d", http.StatusInternalServerError, recorder.Code)
}
if testutil.ToFloat64(responseSuccessCounter.WithLabelValues("wrapper", "failure")) != float64(1) {
t.Fatalf("Expected responseSuccessCounter to be 1, got %f", testutil.ToFloat64(responseSuccessCounter.WithLabelValues("myhandler", "failure")))
}
if testutil.ToFloat64(responseStatusCounter.WithLabelValues("wrapper", "500")) != float64(1) {
t.Fatalf("Expected responseStatusCounter to be 1, got %f", testutil.ToFloat64(responseStatusCounter.WithLabelValues("myhandler", "500")))
}
if testutil.ToFloat64(responseSuccessCounter.WithLabelValues("inner", "failure")) != float64(1) {
t.Fatalf("Expected responseSuccessCounter to be 1, got %f", testutil.ToFloat64(responseSuccessCounter.WithLabelValues("myhandler", "failure")))
}
// With Gauge Metric
if testutil.ToFloat64(responseStatusGauge.WithLabelValues("inner", "500")) != float64(1) {
t.Fatalf("Expected responseStatusGauge to be 1, got %f", testutil.ToFloat64(responseStatusGauge.WithLabelValues("myhandler", "500")))
}
if testutil.ToFloat64(responseSuccessGauge.WithLabelValues("wrapper", "failure")) != float64(1) {
t.Fatalf("Expected responseSuccessGauge to be 1, got %f", testutil.ToFloat64(responseSuccessGauge.WithLabelValues("myhandler", "failure")))
}
if testutil.ToFloat64(responseStatusGauge.WithLabelValues("wrapper", "500")) != float64(1) {
t.Fatalf("Expected responseStatusGauge to be 1, got %f", testutil.ToFloat64(responseStatusGauge.WithLabelValues("myhandler", "500")))
}
if testutil.ToFloat64(responseSuccessGauge.WithLabelValues("inner", "failure")) != float64(1) {
t.Fatalf("Expected responseSuccessGauge to be 1, got %f", testutil.ToFloat64(responseSuccessGauge.WithLabelValues("myhandler", "failure")))
}
if testutil.ToFloat64(responseStatusGauge.WithLabelValues("inner", "500")) != float64(1) {
t.Fatalf("Expected responseStatusGauge to be 1, got %f", testutil.ToFloat64(responseStatusGauge.WithLabelValues("myhandler", "500")))
}
}