forked from GoogleCloudPlatform/k8s-stackdriver
/
client.go
124 lines (109 loc) · 3.39 KB
/
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
Copyright 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package controller
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
"github.com/prometheus/common/expfmt"
"github.com/prometheus/common/model"
)
// Metrics are parsed values from the kube-controller.
type Metrics struct {
CreateTime int64
NodeEvictions int64
}
// parseMetrics takes the text format for prometheus metrics, and converts
// them into our Metrics object.
func (c *Metrics) parseMetrics(data string) error {
dec := expfmt.NewDecoder(strings.NewReader(data), expfmt.FmtText)
decoder := expfmt.SampleDecoder{
Dec: dec,
Opts: &expfmt.DecodeOptions{},
}
for {
var v model.Vector
if err := decoder.Decode(&v); err != nil {
if err == io.EOF {
// Expected loop termination condition.
return nil
}
return fmt.Errorf("Invalid decode: %v", err)
}
for _, metric := range v {
switch name := string(metric.Metric[model.MetricNameLabel]); name {
case "node_collector_evictions_number":
c.NodeEvictions = int64(metric.Value)
case "process_start_time_seconds":
c.CreateTime = int64(metric.Value)
}
}
}
}
// NewMetrics creates a Metrics object from a Prometheus response body.
func NewMetrics(body []byte) (*Metrics, error) {
metrics := &Metrics{}
if err := metrics.parseMetrics(string(body)); err != nil {
return nil, fmt.Errorf("Failed to create a new Metrics object: %v", err)
}
return metrics, nil
}
// Client queries metrics from the controller process.
type Client struct {
client *http.Client
metricsURL *url.URL
}
// NewClient generates a client to hit the given controller.
func NewClient(host string, port uint, client *http.Client) (*Client, error) {
// Parse our URL upfront, so we can fail fast.
urlStr := fmt.Sprintf("http://%s:%d/metrics", host, port)
metricsURL, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
return &Client{
client: client,
metricsURL: metricsURL,
}, nil
}
// doRequest makes the request to the controller, and returns the body.
func (c *Client) doRequestAndParse(req *http.Request) (*Metrics, error) {
response, err := c.client.Do(req)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body - %v", err)
}
if response.StatusCode == http.StatusNotFound {
return nil, fmt.Errorf("%q not found", req.URL.String())
} else if response.StatusCode != http.StatusOK {
return nil, fmt.Errorf("request failed - %q, response: %q", response.Status, string(body))
}
metrics, err := NewMetrics(body)
return metrics, err
}
// GetMetrics returns the latest Metrics parsed from the kube-controller endpoint.
func (c *Client) GetMetrics() (*Metrics, error) {
req, err := http.NewRequest("GET", c.metricsURL.String(), nil)
if err != nil {
return nil, err
}
metrics, err := c.doRequestAndParse(req)
return metrics, err
}