-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdefault_collector.go
91 lines (75 loc) · 2.89 KB
/
default_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
package collector
import (
"sync"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
const NAMESPACE = "sap"
type DefaultCollector struct {
subsystem string
descriptors map[string]*prometheus.Desc
}
func NewDefaultCollector(subsystem string) DefaultCollector {
return DefaultCollector{
subsystem,
make(map[string]*prometheus.Desc),
}
}
func (c *DefaultCollector) GetDescriptor(name string) *prometheus.Desc {
desc, ok := c.descriptors[name]
if !ok {
// we hard panic on this because it's most certainly a coding error
panic(errors.Errorf("undeclared metric '%s'", name))
}
return desc
}
// Convenience wrapper around prometheus.NewDesc constructor.
// Stores a metric descriptor with a fully qualified name like `NAMESPACE_subsystem_name`.
// `name` is the last and most relevant part of the metrics Full Qualified Name;
// `help` is the message displayed in the HELP line
// `variableLabels` is a list of labels to declare. Use `nil` to declare no labels.
func (c *DefaultCollector) SetDescriptor(name, help string, variableLabels []string) {
c.descriptors[name] = prometheus.NewDesc(prometheus.BuildFQName(NAMESPACE, c.subsystem, name), help, variableLabels, nil)
}
func (c *DefaultCollector) Describe(ch chan<- *prometheus.Desc) {
for _, descriptor := range c.descriptors {
ch <- descriptor
}
}
func (c *DefaultCollector) MakeGaugeMetric(name string, value float64, labelValues ...string) prometheus.Metric {
return c.makeMetric(name, value, prometheus.GaugeValue, labelValues...)
}
func (c *DefaultCollector) MakeCounterMetric(name string, value float64, labelValues ...string) prometheus.Metric {
return c.makeMetric(name, value, prometheus.CounterValue, labelValues...)
}
func (c *DefaultCollector) makeMetric(name string, value float64, valueType prometheus.ValueType, labelValues ...string) prometheus.Metric {
desc := c.GetDescriptor(name)
return prometheus.MustNewConstMetric(desc, valueType, value, labelValues...)
}
// Run multiple metric recording functions concurrently
func RecordConcurrently(recorders []func(ch chan<- prometheus.Metric) error, ch chan<- prometheus.Metric) []error {
results := make(chan error, len(recorders))
var errs []error
var wg sync.WaitGroup
// For each recorder we start a goroutine which will send its result in a channel.
// A Waitgroup is used to later wait for all of them.
for _, recorder := range recorders {
wg.Add(1)
go func(recorder func(ch chan<- prometheus.Metric) error, wg *sync.WaitGroup) {
defer wg.Done()
results <- recorder(ch)
}(recorder, &wg)
}
// As soon as all the goroutines in the Waitgroup are done, close the channel where the errors are sent
go func() {
wg.Wait()
close(results)
}()
// Scroll the results channel and store potential errors in an array. This will block until the channel is closed.
for err := range results {
if err != nil {
errs = append(errs, err)
}
}
return errs
}