-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
207 lines (171 loc) · 5.92 KB
/
main.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"compress/gzip"
"context"
"fmt"
"io"
"log"
"net"
"net/http"
"net/http/pprof"
"os"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/klog/v2"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"github.com/tmon546596046/board-state-metrics/pkg/proc"
"github.com/tmon546596046/board-state-metrics/pkg/version"
kcollectors "k8s.io/kube-state-metrics/pkg/collector"
koptions "k8s.io/kube-state-metrics/pkg/options"
"k8s.io/kube-state-metrics/pkg/whiteblacklist"
ocollectors "github.com/tmon546596046/board-state-metrics/pkg/collectors"
"github.com/tmon546596046/board-state-metrics/pkg/options"
)
const (
metricsPath = "/metrics"
healthzPath = "/healthz"
)
// promLogger implements promhttp.Logger
type promLogger struct{}
func (pl promLogger) Println(v ...interface{}) {
klog.Error(v...)
}
func main() {
opts := options.NewOptions()
opts.AddFlags()
err := opts.Parse()
if err != nil {
klog.Fatalf("Error: %s", err)
}
if opts.Version {
fmt.Printf("%#v\n", version.GetVersion())
os.Exit(0)
}
if opts.Help {
opts.Usage()
os.Exit(0)
}
collectorBuilder := ocollectors.NewBuilder(context.TODO())
collectorBuilder.WithApiserver(opts.Apiserver).WithKubeConfig(opts.Kubeconfig).WithPrometheus(opts.Prometheus)
if len(opts.Collectors) == 0 {
klog.Info("Using default collectors")
collectorBuilder.WithEnabledCollectors(options.DefaultCollectors.AsSlice())
} else {
collectorBuilder.WithEnabledCollectors(opts.Collectors.AsSlice())
}
if len(opts.Namespaces) == 0 {
klog.Info("Using all namespace")
collectorBuilder.WithNamespaces(koptions.DefaultNamespaces)
} else {
if opts.Namespaces.IsAllNamespaces() {
klog.Info("Using all namespace")
} else {
klog.Infof("Using %s namespaces", opts.Namespaces)
}
collectorBuilder.WithNamespaces(opts.Namespaces)
}
whiteBlackList, err := whiteblacklist.New(opts.MetricWhitelist, opts.MetricBlacklist)
if err != nil {
klog.Fatal(err)
}
klog.Infof("metric white- blacklisting: %v", whiteBlackList.Status())
collectorBuilder.WithWhiteBlackList(whiteBlackList)
proc.StartReaper()
if err != nil {
klog.Fatalf("Failed to create client: %v", err)
}
osMetricsRegistry := prometheus.NewRegistry()
osMetricsRegistry.Register(ocollectors.ResourcesPerScrapeMetric)
osMetricsRegistry.Register(ocollectors.ScrapeErrorTotalMetric)
osMetricsRegistry.Register(prometheus.NewProcessCollector(prometheus.ProcessCollectorOpts{}))
osMetricsRegistry.Register(prometheus.NewGoCollector())
go telemetryServer(osMetricsRegistry, opts.TelemetryHost, opts.TelemetryPort)
collectors := collectorBuilder.Build()
serveMetrics(collectors, opts.Host, opts.Port, opts.EnableGZIPEncoding)
}
func telemetryServer(registry prometheus.Gatherer, host string, port int) {
// Address to listen on for web interface and telemetry
listenAddress := net.JoinHostPort(host, strconv.Itoa(port))
klog.Infof("Starting board-state-metrics self metrics server: %s", listenAddress)
mux := http.NewServeMux()
// Add metricsPath
mux.Handle(metricsPath, promhttp.HandlerFor(registry, promhttp.HandlerOpts{ErrorLog: promLogger{}}))
// Add index
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>board-State-Metrics Metrics Server</title></head>
<body>
<h1>board-State-Metrics Metrics</h1>
<ul>
<li><a href='` + metricsPath + `'>metrics</a></li>
</ul>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(listenAddress, mux))
}
// TODO: How about accepting an interface Collector instead?
func serveMetrics(collectors []*kcollectors.Collector, host string, port int, enableGZIPEncoding bool) {
// Address to listen on for web interface and telemetry
listenAddress := net.JoinHostPort(host, strconv.Itoa(port))
klog.Infof("Starting metrics server: %s", listenAddress)
mux := http.NewServeMux()
// TODO: This doesn't belong into serveMetrics
mux.Handle("/debug/pprof/", http.HandlerFunc(pprof.Index))
mux.Handle("/debug/pprof/cmdline", http.HandlerFunc(pprof.Cmdline))
mux.Handle("/debug/pprof/profile", http.HandlerFunc(pprof.Profile))
mux.Handle("/debug/pprof/symbol", http.HandlerFunc(pprof.Symbol))
mux.Handle("/debug/pprof/trace", http.HandlerFunc(pprof.Trace))
// Add metricsPath
mux.Handle(metricsPath, &metricHandler{collectors, enableGZIPEncoding})
// Add healthzPath
mux.HandleFunc(healthzPath, func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("ok"))
})
// Add index
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
<head><title>Board Metrics Server</title></head>
<body>
<h1>Kube Metrics</h1>
<ul>
<li><a href='` + metricsPath + `'>metrics</a></li>
<li><a href='` + healthzPath + `'>healthz</a></li>
</ul>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(listenAddress, mux))
}
type metricHandler struct {
collectors []*kcollectors.Collector
enableGZIPEncoding bool
}
func (m *metricHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
resHeader := w.Header()
var writer io.Writer = w
resHeader.Set("Content-Type", `text/plain; version=`+"0.0.4")
if m.enableGZIPEncoding {
// Gzip response if requested. Taken from
// github.com/prometheus/client_golang/prometheus/promhttp.decorateWriter.
reqHeader := r.Header.Get("Accept-Encoding")
parts := strings.Split(reqHeader, ",")
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "gzip" || strings.HasPrefix(part, "gzip;") {
writer = gzip.NewWriter(writer)
resHeader.Set("Content-Encoding", "gzip")
}
}
}
for _, c := range m.collectors {
c.Collect(w)
}
// In case we gziped the response, we have to close the writer.
if closer, ok := writer.(io.Closer); ok {
closer.Close()
}
}