Skip to content

Commit f2a0ab0

Browse files
authored
Merge pull request #4438 from aledbf/metrics
Add helper to extract prometheus metrics in e2e tests
2 parents d406c96 + 8a9298a commit f2a0ab0

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

Diff for: test/e2e/framework/metrics.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package framework
18+
19+
import (
20+
"fmt"
21+
"net/http"
22+
23+
dto "github.com/prometheus/client_model/go"
24+
"github.com/prometheus/common/expfmt"
25+
)
26+
27+
// GetMetric returns the current prometheus metric exposed by NGINX
28+
func (f *Framework) GetMetric(metricName, ip string) (*dto.MetricFamily, error) {
29+
url := fmt.Sprintf("http://%v:10254/metrics", ip)
30+
31+
client := &http.Client{}
32+
req, err := http.NewRequest("GET", url, nil)
33+
if err != nil {
34+
return nil, fmt.Errorf("creating GET request for URL %q failed: %v", url, err)
35+
}
36+
resp, err := client.Do(req)
37+
if err != nil {
38+
return nil, fmt.Errorf("executing GET request for URL %q failed: %v", url, err)
39+
}
40+
defer resp.Body.Close()
41+
if resp.StatusCode != http.StatusOK {
42+
return nil, fmt.Errorf("GET request for URL %q returned HTTP status %s", url, resp.Status)
43+
}
44+
45+
var parser expfmt.TextParser
46+
metrics, err := parser.TextToMetricFamilies(resp.Body)
47+
48+
if err != nil {
49+
return nil, fmt.Errorf("reading text format failed: %v", err)
50+
}
51+
52+
for _, m := range metrics {
53+
if m.GetName() == metricName {
54+
return m, nil
55+
}
56+
}
57+
58+
return nil, fmt.Errorf("there is no metric with name %v", metricName)
59+
}

0 commit comments

Comments
 (0)