-
Notifications
You must be signed in to change notification settings - Fork 14
/
collector.go
118 lines (102 loc) · 2.87 KB
/
collector.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
package metrics
import (
"context"
"fmt"
"net/http"
"sync"
"time"
v3 "github.com/coreos/etcd/clientv3"
"github.com/cybozu-go/cke"
"github.com/cybozu-go/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type logger struct{}
func (l logger) Println(v ...interface{}) {
log.Error(fmt.Sprint(v...), nil)
}
const (
scrapeTimeout = time.Second * 8
)
// collector is a metrics collector for CKE.
type collector struct {
metrics map[string]metricGroup
storage storage
}
// metricGroup represents collectors and availability of metric.
type metricGroup struct {
collectors []prometheus.Collector
isAvailable func(context.Context, storage) (bool, error)
}
// storage is abstraction of cke.Storage.
// This abstraction is for mock test.
type storage interface {
IsSabakanDisabled(context.Context) (bool, error)
}
// NewCollector returns a new prometheus.Collector.
func NewCollector(client *v3.Client) prometheus.Collector {
return &collector{
metrics: map[string]metricGroup{
"leader": {
collectors: []prometheus.Collector{leader},
isAvailable: alwaysAvailable,
},
"operation_phase": {
collectors: []prometheus.Collector{operationPhase, operationPhaseTimestampSeconds},
isAvailable: isOperationPhaseAvailable,
},
"sabakan_integration": {
collectors: []prometheus.Collector{sabakanIntegrationSuccessful, sabakanIntegrationTimestampSeconds, sabakanWorkers, sabakanUnusedMachines},
isAvailable: isSabakanIntegrationAvailable,
},
},
storage: &cke.Storage{Client: client},
}
}
// GetHandler returns http.Handler for prometheus metrics.
func GetHandler(collector prometheus.Collector) http.Handler {
registry := prometheus.NewRegistry()
registry.MustRegister(collector)
gathers := prometheus.Gatherers{registry, prometheus.DefaultGatherer}
handler := promhttp.HandlerFor(gathers,
promhttp.HandlerOpts{
ErrorLog: logger{},
ErrorHandling: promhttp.ContinueOnError,
})
return handler
}
// Describe implements Collector.Describe().
func (c collector) Describe(ch chan<- *prometheus.Desc) {
for _, metric := range c.metrics {
for _, col := range metric.collectors {
col.Describe(ch)
}
}
}
// Collect implements Collector.Collect().
func (c collector) Collect(ch chan<- prometheus.Metric) {
ctx, cancel := context.WithTimeout(context.Background(), scrapeTimeout)
defer cancel()
var wg sync.WaitGroup
for key, metric := range c.metrics {
wg.Add(1)
go func(key string, metric metricGroup) {
defer wg.Done()
available, err := metric.isAvailable(ctx, c.storage)
if err != nil {
log.Warn("unable to decide whether metrics are available", map[string]interface{}{
"name": key,
log.FnError: err,
})
return
}
if !available {
return
}
for _, col := range metric.collectors {
col.Collect(ch)
}
}(key, metric)
}
wg.Wait()
}