forked from kubernetes-sigs/gateway-api-inference-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpod_client.go
34 lines (28 loc) · 979 Bytes
/
pod_client.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
package backend
import (
"fmt"
"net/http"
dto "github.com/prometheus/client_model/go"
"github.com/prometheus/common/expfmt"
klog "k8s.io/klog/v2"
)
type PodMetricsClientImpl struct {
}
// FetchMetrics fetches metrics from a given pod.
func (p *PodMetricsClientImpl) FetchMetrics(pod Pod) (map[string]*dto.MetricFamily, error) {
// Currently the metrics endpoint is hard-coded.
// TODO: Consider making this configurable.
url := fmt.Sprintf("http://%s/metrics", pod.Address)
resp, err := http.Get(url)
if err != nil {
klog.Errorf("failed to fetch metrics from %s: %v", pod, err)
return nil, fmt.Errorf("failed to fetch metrics from %s: %w", pod, err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
klog.Errorf("unexpected status code from %s: %v", pod, resp.StatusCode)
return nil, fmt.Errorf("unexpected status code from %s: %v", pod, resp.StatusCode)
}
parser := expfmt.TextParser{}
return parser.TextToMetricFamilies(resp.Body)
}