This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
default_collector.go
77 lines (65 loc) · 2.43 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
package main
import (
"os"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)
const NAMESPACE = "saptune"
// DefaultCollector for prometheus
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...)
}
// check that all the given paths exist and are executable files
func checkExecutables(paths ...string) error {
for _, path := range paths {
fileInfo, err := os.Stat(path)
if err != nil || os.IsNotExist(err) {
return errors.Errorf("'%s' does not exist", path)
}
if fileInfo.IsDir() {
return errors.Errorf("'%s' is a directory", path)
}
if (fileInfo.Mode() & 0111) == 0 {
return errors.Errorf("'%s' is not executable", path)
}
}
return nil
}