@@ -53,11 +53,11 @@ var (
5353func TestInitMetrics (t * testing.T ) {
5454 getMetrics (t )
5555
56- rr := mockMetricsRequest ()
56+ responseRecorder := mockMetricsRequest ()
5757
58- validateStatus (t , rr )
58+ validateStatus (t , responseRecorder )
5959
60- metricsMap := getMetricsMap (rr .Body .String ())
60+ metricsMap := getMetricsMap (responseRecorder .Body .String ())
6161
6262 runtimeMetrics := []string {
6363 "go_gc_gogc_percent" ,
@@ -76,11 +76,11 @@ func TestErrorEventsInc(t *testing.T) {
7676
7777 metrics .ErrorEventsInc (mockErrorEvent )
7878
79- rr := mockMetricsRequest ()
79+ responseRecorder := mockMetricsRequest ()
8080
81- validateStatus (t , rr )
81+ validateStatus (t , responseRecorder )
8282
83- metricsMap := getMetricsMap (rr .Body .String ())
83+ metricsMap := getMetricsMap (responseRecorder .Body .String ())
8484
8585 validateEventErrorTotal (t , metricsMap , 1 )
8686 validateActionTotalV2 (t , metricsMap , 0 , successStatus )
@@ -94,11 +94,11 @@ func TestNodeActionsInc(t *testing.T) {
9494 metrics .NodeActionsInc (mockAction , mockNodeName2 , mockEventID2 , nil )
9595 metrics .NodeActionsInc (mockAction , mockNodeName3 , mockEventID3 , errors .New ("mockError" ))
9696
97- rr := mockMetricsRequest ()
97+ responseRecorder := mockMetricsRequest ()
9898
99- validateStatus (t , rr )
99+ validateStatus (t , responseRecorder )
100100
101- metricsMap := getMetricsMap (rr .Body .String ())
101+ metricsMap := getMetricsMap (responseRecorder .Body .String ())
102102
103103 validateEventErrorTotal (t , metricsMap , 0 )
104104 validateActionTotalV2 (t , metricsMap , 2 , successStatus )
@@ -112,25 +112,25 @@ func TestRegisterMetricsWith(t *testing.T) {
112112
113113 metrics := getMetrics (t )
114114
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 )}
118118
119119 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 ... ))
121121 }
122122 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 ... ))
124124 }
125125 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 ... ))
127127 }
128128
129- rr := mockMetricsRequest ()
129+ responseRecorder := mockMetricsRequest ()
130130
131- validateStatus (t , rr )
131+ validateStatus (t , responseRecorder )
132132
133- metricsMap := getMetricsMap (rr .Body .String ())
133+ metricsMap := getMetricsMap (responseRecorder .Body .String ())
134134
135135 validateEventErrorTotal (t , metricsMap , errorEventMetricsTotal )
136136 validateActionTotalV2 (t , metricsMap , successActionMetricsTotal , successStatus )
@@ -152,14 +152,14 @@ func TestServeMetrics(t *testing.T) {
152152
153153 conn , err := net .DialTimeout ("tcp" , fmt .Sprintf ("localhost:%d" , mockDefaultPort ), time .Second )
154154 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 )
156156 }
157157 conn .Close ()
158158
159159 conn , err = net .DialTimeout ("tcp" , fmt .Sprintf ("localhost:%d" , mockClosedPort ), time .Second )
160160 if err == nil {
161161 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 )
163163 }
164164}
165165
@@ -189,16 +189,21 @@ func getMetrics(t *testing.T) *Metrics {
189189func mockMetricsRequest () * httptest.ResponseRecorder {
190190 handler := promhttp .Handler ()
191191 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
195195}
196196
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
199199 h .Equals (t , http .StatusOK , status )
200200}
201201
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
202207func getMetricsMap (body string ) map [string ]string {
203208 metricsMap := make (map [string ]string )
204209 lines := strings .Split (body , "\n " )
0 commit comments