forked from kubernetes/test-infra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.go
174 lines (149 loc) · 4.88 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
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
173
174
/*
Copyright 2017 The Kubernetes Authors.
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 main
import (
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"k8s.io/test-infra/boskos/client"
)
type prometheusMetrics struct {
GceStats map[string]prometheus.Gauge
GkeStats map[string]prometheus.Gauge
}
var (
promMetrics = prometheusMetrics{
GceStats: map[string]prometheus.Gauge{
"free": prometheus.NewGauge(prometheus.GaugeOpts{
Name: "boskos_gce_project_free",
Help: "Number of free gce-project",
}),
"busy": prometheus.NewGauge(prometheus.GaugeOpts{
Name: "boskos_gce_project_busy",
Help: "Number of busy gce-project",
}),
"dirty": prometheus.NewGauge(prometheus.GaugeOpts{
Name: "boskos_gce_project_dirty",
Help: "Number of dirty gce-project",
}),
"cleaning": prometheus.NewGauge(prometheus.GaugeOpts{
Name: "boskos_gce_project_cleaning",
Help: "Number of cleaning gce-project",
}),
},
GkeStats: map[string]prometheus.Gauge{
"free": prometheus.NewGauge(prometheus.GaugeOpts{
Name: "boskos_gke_project_free",
Help: "Number of free gke-project",
}),
"busy": prometheus.NewGauge(prometheus.GaugeOpts{
Name: "boskos_gke_project_busy",
Help: "Number of busy gke-project",
}),
"dirty": prometheus.NewGauge(prometheus.GaugeOpts{
Name: "boskos_gke_project_dirty",
Help: "Number of dirty gke-project",
}),
"cleaning": prometheus.NewGauge(prometheus.GaugeOpts{
Name: "boskos_gke_project_cleaning",
Help: "Number of cleaning gke-project",
}),
},
}
)
func init() {
for _, gce := range promMetrics.GceStats {
prometheus.MustRegister(gce)
}
for _, gke := range promMetrics.GkeStats {
prometheus.MustRegister(gke)
}
}
func main() {
logrus.SetFormatter(&logrus.JSONFormatter{})
boskos := client.NewClient("Metrics", "http://boskos")
logrus.Infof("Initialzied boskos client!")
http.Handle("/prometheus", promhttp.Handler())
http.Handle("/", handleMetric(boskos))
go func() {
logTick := time.NewTicker(time.Minute).C
for {
select {
case <-logTick:
if err := update(boskos); err != nil {
logrus.WithError(err).Warning("[Boskos Metrics]Update failed!")
}
}
}
}()
logrus.Info("Start Service")
logrus.WithError(http.ListenAndServe(":8080", nil)).Fatal("ListenAndServe returned.")
}
func update(boskos *client.Client) error {
gce, err := boskos.Metric("gce-project")
if err != nil {
return fmt.Errorf("fail to get metric for gce-project : %v", err)
}
promMetrics.GceStats["free"].Set(float64(gce.Current["free"]))
promMetrics.GceStats["busy"].Set(float64(gce.Current["busy"]))
promMetrics.GceStats["dirty"].Set(float64(gce.Current["dirty"]))
promMetrics.GceStats["cleaning"].Set(float64(gce.Current["cleaning"]))
gke, err := boskos.Metric("gke-project")
if err != nil {
return fmt.Errorf("fail to get metric for gke-project : %v", err)
}
promMetrics.GkeStats["free"].Set(float64(gke.Current["free"]))
promMetrics.GkeStats["busy"].Set(float64(gke.Current["busy"]))
promMetrics.GkeStats["dirty"].Set(float64(gke.Current["dirty"]))
promMetrics.GkeStats["cleaning"].Set(float64(gke.Current["cleaning"]))
return nil
}
// handleMetric: Handler for /
// Method: GET
func handleMetric(boskos *client.Client) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
log := logrus.WithField("handler", "handleMetric")
log.Infof("From %v", req.RemoteAddr)
if req.Method != "GET" {
log.Warning("[BadRequest]method %v, expect GET", req.Method)
http.Error(res, "only accepts GET request", http.StatusMethodNotAllowed)
return
}
rtype := req.URL.Query().Get("type")
if rtype == "" {
msg := "type must be set in the request."
log.Warning(msg)
http.Error(res, msg, http.StatusBadRequest)
return
}
log.Infof("Request for metric %v", rtype)
metric, err := boskos.Metric(rtype)
if err != nil {
log.WithError(err).Errorf("Fail to get metic for %v", rtype)
http.Error(res, err.Error(), http.StatusNotFound)
return
}
metricJSON, err := json.Marshal(metric)
if err != nil {
log.WithError(err).Errorf("json.Marshal failed: %v", metricJSON)
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
log.Infof("Metric query for %v: %v", rtype, string(metricJSON))
fmt.Fprint(res, string(metricJSON))
}
}