/
metrics.go
57 lines (47 loc) · 1.51 KB
/
metrics.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
// Copyright Contributors to the Open Cluster Management project
package common
import (
"context"
"crypto/tls"
"io"
"net/http"
"time"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
)
// GetWithToken makes a GET request to the given target, and puts the
// token in an Authorization header if non-empty. The HTTP client has
// a sane timeout, and will skip verifying the target certificate.
func GetWithToken(url, authToken string) (body, status string, err error) {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
Timeout: 5 * time.Second,
}
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
if err != nil {
return "", "", err
}
if authToken != "" {
req.Header.Add("Authorization", "Bearer "+authToken)
}
resp, err := client.Do(req)
if err != nil {
return "", "", err
}
defer resp.Body.Close()
bodyBytes, err := io.ReadAll(resp.Body)
return string(bodyBytes), resp.Status, err
}
// MatchMetricValue returns a GomegaMatcher to look through the full
// response from a metrics endpoint and check for a specific data point.
func MatchMetricValue(name, label, value string) types.GomegaMatcher {
regex := `(?m)` // multiline mode (makes ^ and $ work)
regex += "^" + name + "{" // full name of metric at start of line
regex += ".*" + label + ".*" // label somewhere inside the {...}
regex += "} " + value + "$" // value at the end of line
return MatchRegexp(regex)
}