@@ -53,11 +53,11 @@ var (
53
53
func TestInitMetrics (t * testing.T ) {
54
54
getMetrics (t )
55
55
56
- rr := mockMetricsRequest ()
56
+ responseRecorder := mockMetricsRequest ()
57
57
58
- validateStatus (t , rr )
58
+ validateStatus (t , responseRecorder )
59
59
60
- metricsMap := getMetricsMap (rr .Body .String ())
60
+ metricsMap := getMetricsMap (responseRecorder .Body .String ())
61
61
62
62
runtimeMetrics := []string {
63
63
"go_gc_gogc_percent" ,
@@ -76,11 +76,11 @@ func TestErrorEventsInc(t *testing.T) {
76
76
77
77
metrics .ErrorEventsInc (mockErrorEvent )
78
78
79
- rr := mockMetricsRequest ()
79
+ responseRecorder := mockMetricsRequest ()
80
80
81
- validateStatus (t , rr )
81
+ validateStatus (t , responseRecorder )
82
82
83
- metricsMap := getMetricsMap (rr .Body .String ())
83
+ metricsMap := getMetricsMap (responseRecorder .Body .String ())
84
84
85
85
validateEventErrorTotal (t , metricsMap , 1 )
86
86
validateActionTotalV2 (t , metricsMap , 0 , successStatus )
@@ -94,11 +94,11 @@ func TestNodeActionsInc(t *testing.T) {
94
94
metrics .NodeActionsInc (mockAction , mockNodeName2 , mockEventID2 , nil )
95
95
metrics .NodeActionsInc (mockAction , mockNodeName3 , mockEventID3 , errors .New ("mockError" ))
96
96
97
- rr := mockMetricsRequest ()
97
+ responseRecorder := mockMetricsRequest ()
98
98
99
- validateStatus (t , rr )
99
+ validateStatus (t , responseRecorder )
100
100
101
- metricsMap := getMetricsMap (rr .Body .String ())
101
+ metricsMap := getMetricsMap (responseRecorder .Body .String ())
102
102
103
103
validateEventErrorTotal (t , metricsMap , 0 )
104
104
validateActionTotalV2 (t , metricsMap , 2 , successStatus )
@@ -112,25 +112,25 @@ func TestRegisterMetricsWith(t *testing.T) {
112
112
113
113
metrics := getMetrics (t )
114
114
115
- errorEventLables := []attribute.KeyValue {labelEventErrorWhereKey .String (mockErrorEvent )}
116
- successActionLables := []attribute.KeyValue {labelNodeActionKey .String (mockAction ), labelNodeStatusKey .String (successStatus )}
117
- errorActionLables := []attribute.KeyValue {labelNodeActionKey .String (mockAction ), labelNodeStatusKey .String (errorStatus )}
115
+ errorEventlabels := []attribute.KeyValue {labelEventErrorWhereKey .String (mockErrorEvent )}
116
+ successActionlabels := []attribute.KeyValue {labelNodeActionKey .String (mockAction ), labelNodeStatusKey .String (successStatus )}
117
+ errorActionlabels := []attribute.KeyValue {labelNodeActionKey .String (mockAction ), labelNodeStatusKey .String (errorStatus )}
118
118
119
119
for i := 0 ; i < errorEventMetricsTotal ; i ++ {
120
- metrics .errorEventsCounter .Add (context .Background (), 1 , api .WithAttributes (errorEventLables ... ))
120
+ metrics .errorEventsCounter .Add (context .Background (), 1 , api .WithAttributes (errorEventlabels ... ))
121
121
}
122
122
for i := 0 ; i < successActionMetricsTotal ; i ++ {
123
- metrics .actionsCounterV2 .Add (context .Background (), 1 , api .WithAttributes (successActionLables ... ))
123
+ metrics .actionsCounterV2 .Add (context .Background (), 1 , api .WithAttributes (successActionlabels ... ))
124
124
}
125
125
for i := 0 ; i < errorActionMetricsTotal ; i ++ {
126
- metrics .actionsCounterV2 .Add (context .Background (), 1 , api .WithAttributes (errorActionLables ... ))
126
+ metrics .actionsCounterV2 .Add (context .Background (), 1 , api .WithAttributes (errorActionlabels ... ))
127
127
}
128
128
129
- rr := mockMetricsRequest ()
129
+ responseRecorder := mockMetricsRequest ()
130
130
131
- validateStatus (t , rr )
131
+ validateStatus (t , responseRecorder )
132
132
133
- metricsMap := getMetricsMap (rr .Body .String ())
133
+ metricsMap := getMetricsMap (responseRecorder .Body .String ())
134
134
135
135
validateEventErrorTotal (t , metricsMap , errorEventMetricsTotal )
136
136
validateActionTotalV2 (t , metricsMap , successActionMetricsTotal , successStatus )
@@ -152,14 +152,14 @@ func TestServeMetrics(t *testing.T) {
152
152
153
153
conn , err := net .DialTimeout ("tcp" , fmt .Sprintf ("localhost:%d" , mockDefaultPort ), time .Second )
154
154
if err != nil {
155
- t .Errorf ("server not listening on port %d: %v" , mockDefaultPort , err )
155
+ t .Errorf ("server is not listening on port %d: %v" , mockDefaultPort , err )
156
156
}
157
157
conn .Close ()
158
158
159
159
conn , err = net .DialTimeout ("tcp" , fmt .Sprintf ("localhost:%d" , mockClosedPort ), time .Second )
160
160
if err == nil {
161
161
conn .Close ()
162
- t .Errorf ("server should not listening on port %d: %v" , mockClosedPort , err )
162
+ t .Errorf ("server should not be listening on port %d: %v" , mockClosedPort , err )
163
163
}
164
164
}
165
165
@@ -189,16 +189,21 @@ func getMetrics(t *testing.T) *Metrics {
189
189
func mockMetricsRequest () * httptest.ResponseRecorder {
190
190
handler := promhttp .Handler ()
191
191
req := httptest .NewRequest ("GET" , metricsEndpoint , nil )
192
- rr := httptest .NewRecorder ()
193
- handler .ServeHTTP (rr , req )
194
- return rr
192
+ responseRecorder := httptest .NewRecorder ()
193
+ handler .ServeHTTP (responseRecorder , req )
194
+ return responseRecorder
195
195
}
196
196
197
- func validateStatus (t * testing.T , rr * httptest.ResponseRecorder ) {
198
- status := rr .Code
197
+ func validateStatus (t * testing.T , responseRecorder * httptest.ResponseRecorder ) {
198
+ status := responseRecorder .Code
199
199
h .Equals (t , http .StatusOK , status )
200
200
}
201
201
202
+ // This method take response body got from Prometheus exporter as arg
203
+ // Example:
204
+ // # HELP go_goroutines Number of goroutines that currently exist.
205
+ // # TYPE go_goroutines gauge
206
+ // go_goroutines 6
202
207
func getMetricsMap (body string ) map [string ]string {
203
208
metricsMap := make (map [string ]string )
204
209
lines := strings .Split (body , "\n " )
0 commit comments