/
prometheus.go
172 lines (145 loc) · 4.3 KB
/
prometheus.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package prometheus
import (
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"os"
"time"
"github.com/openshift-scale/perf-analyzer/pkg/config"
"github.com/openshift-scale/perf-analyzer/pkg/result"
"github.com/openshift-scale/perf-analyzer/pkg/stats"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/model"
)
var insecureRoundTripper http.RoundTripper = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
TLSHandshakeTimeout: 10 * time.Second,
}
type TimePoint [2]float64
type TimeSeriesPoints []TimePoint
type TimeSeries struct {
Name string
Points TimeSeriesPoints
Tags []rawData
}
type rawData struct {
Name string
Values []float64
}
type prometheusConfig struct {
config api.Config
API v1.API
Errors []error
}
func newPrometheusConfig(url, token string, insecureTLS bool) (prometheusConfig, error) {
rt, err := NewBearerAuthRoundTripper(token, insecureRoundTripper)
if err != nil {
return prometheusConfig{}, err
}
return prometheusConfig{config: api.Config{Address: url, RoundTripper: rt}}, nil
}
func NewBearerAuthRoundTripper(bearer string, rt http.RoundTripper) (http.RoundTripper, error) {
if len(bearer) == 0 {
return nil, fmt.Errorf("No bearer token provided for RoundTripper\n")
}
return &bearerAuthRoundTripper{bearer, rt}, nil
}
type bearerAuthRoundTripper struct {
bearer string
rt http.RoundTripper
}
func (rt *bearerAuthRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if len(req.Header.Get("Authorization")) != 0 {
return rt.rt.RoundTrip(req)
}
token := rt.bearer
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
return rt.rt.RoundTrip(req)
}
func (c *prometheusConfig) newPrometheusAPI() {
client, err := api.NewClient(c.config)
if err != nil {
fmt.Printf("Error creating Prometheus client: %v\n", err)
c.Errors = append(c.Errors, err)
} else {
c.API = v1.NewAPI(client)
}
}
// DoPrometheusQuery will run queries against Prometheus endpoint
func DoPrometheusQuery(cfg config.ScrapeConfig) {
config, err := newPrometheusConfig(cfg.UrlFlag, cfg.TokenFlag, cfg.InsecureTLSFlag)
if err != nil {
fmt.Printf("Unable to create Prometheus config: %v\n", err)
}
config.newPrometheusAPI()
if len(config.Errors) != 0 {
fmt.Printf("Client Error %v\n", config.Errors[0])
}
queries := map[string]string{
"cpu": `sum(rate(container_cpu_usage_seconds_total{job="kubelet", image!="", container!="POD"}[5m])) by (namespace)`,
"memory": `sum(container_memory_usage_bytes{container_name!=""}) by (namespace)`,
}
end := time.Now()
start := end.Add(time.Duration(-1*cfg.DurationFlag) * time.Minute)
step, err := time.ParseDuration("1m")
if err != nil {
fmt.Printf("Error parsing step duration %s\n", cfg.StepFlag)
}
r := v1.Range{Start: start, End: end, Step: step}
var results []result.ResultType
for resource, query := range queries {
queryResult, _, err := config.API.QueryRange(context.Background(), query, r)
if err != nil {
fmt.Printf("Prometheus query error: %v\n", err)
return
}
fmt.Printf("Query returned: %+v\n", queryResult)
data, ok := queryResult.(model.Matrix)
if !ok {
fmt.Printf("Unsupported result format: %s\n", queryResult.Type().String())
return
}
series := TimeSeries{
Name: query,
Tags: []rawData{},
}
for _, j := range data {
var newData rawData
for _, v := range j.Metric {
newData.Name = string(v)
}
for _, v := range j.Values {
newData.Values = append(newData.Values, float64(v.Value))
}
series.Tags = append(series.Tags, newData)
}
fmt.Fprintf(os.Stderr, "Series: %+v\n", series)
for _, r := range series.Tags {
results = append(results, AddResult(r.Values, r.Name))
results[len(results)-1].Resource = resource
}
}
fmt.Printf("Results: %+v\n", results)
}
func AddResult(newResult []float64, kind string) result.ResultType {
min, _ := stats.Minimum(newResult)
max, _ := stats.Maximum(newResult)
avg, _ := stats.Mean(newResult)
pct95, _ := stats.Percentile(newResult, 95)
result := result.ResultType{
Kind: kind,
Min: min,
Max: max,
Avg: avg,
Pct95: pct95,
}
return result
}