-
Notifications
You must be signed in to change notification settings - Fork 8
/
metric_config.go
108 lines (96 loc) · 2.12 KB
/
metric_config.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
package main
import (
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"regexp"
"strings"
)
var stringToValueType = map[string]prometheus.ValueType{
"counter": prometheus.CounterValue,
"gauge": prometheus.GaugeValue,
"untyped": prometheus.UntypedValue,
}
type metricConfig struct {
Metrics map[string]*metric `yaml:"metrics"`
LabelMetrics map[string]*labelMetric `yaml:"label_metrics"`
}
type metric struct {
Help string `yaml:"help"`
Type prometheus.ValueType `yaml:"type"`
}
type labelMetric struct {
Help string `yaml:"help"`
Name string `yaml:"name"`
Labels []string `yaml:"labels"`
Type prometheus.ValueType `yaml:"type"`
Regex *regexp.Regexp
}
// Convert config to prom types
func stringToPromType(s string) prometheus.ValueType {
valueType, ok := stringToValueType[strings.ToLower(s)]
if !ok {
log.Println("Invalid type:", s, "Assuming Gauge")
valueType = prometheus.GaugeValue
}
return valueType
}
// Serialize YAML into correct types
func (m *metric) UnmarshalYAML(unmarshal func(interface{}) error) error {
var tmp struct {
Help string
Type string
}
err := unmarshal(&tmp)
if err != nil {
return err
}
m.Help = tmp.Help
m.Type = stringToPromType(tmp.Type)
return nil
}
func (m *labelMetric) UnmarshalYAML(unmarshal func(interface{}) error) error {
var tmp struct {
Name string
Help string
Type string
Labels []string
}
err := unmarshal(&tmp)
if err != nil {
return err
}
m.Name = tmp.Name
m.Help = tmp.Help
m.Labels = tmp.Labels
m.Type = stringToPromType(tmp.Type)
return nil
}
func loadConfig(path string, metricConf *metricConfig) error {
var b []byte
if path == "" {
var err error
b, err = Asset("config.yaml")
if err != nil {
return err
}
} else {
var err error
b, err = ioutil.ReadFile(*metricConfigPath)
if err != nil {
return err
}
}
err := yaml.Unmarshal(b, metricConf)
if err != nil {
return err
}
for k, v := range metricConf.LabelMetrics {
v.Regex, err = regexp.Compile(k)
if err != nil {
return err
}
}
return nil
}