-
Notifications
You must be signed in to change notification settings - Fork 892
/
Copy pathmetrics.go
128 lines (101 loc) · 3.38 KB
/
metrics.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
package exporter
import (
"regexp"
"strconv"
"strings"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
var metricNameRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
func sanitizeMetricName(n string) string {
return metricNameRE.ReplaceAllString(n, "_")
}
func newMetricDescr(namespace string, metricName string, docString string, labels []string) *prometheus.Desc {
return prometheus.NewDesc(prometheus.BuildFQName(namespace, "", metricName), docString, labels, nil)
}
func (e *Exporter) includeMetric(s string) bool {
if strings.HasPrefix(s, "db") || strings.HasPrefix(s, "cmdstat_") || strings.HasPrefix(s, "cluster_") {
return true
}
if _, ok := e.metricMapGauges[s]; ok {
return true
}
_, ok := e.metricMapCounters[s]
return ok
}
func (e *Exporter) parseAndRegisterConstMetric(ch chan<- prometheus.Metric, fieldKey, fieldValue string) {
orgMetricName := sanitizeMetricName(fieldKey)
metricName := orgMetricName
if newName, ok := e.metricMapGauges[metricName]; ok {
metricName = newName
} else {
if newName, ok := e.metricMapCounters[metricName]; ok {
metricName = newName
}
}
var err error
var val float64
switch fieldValue {
case "ok", "true":
val = 1
case "err", "fail", "false":
val = 0
default:
val, err = strconv.ParseFloat(fieldValue, 64)
}
if err != nil {
log.Debugf("couldn't parse %s, err: %s", fieldValue, err)
return
}
t := prometheus.GaugeValue
if e.metricMapCounters[orgMetricName] != "" {
t = prometheus.CounterValue
}
switch metricName {
case "latest_fork_usec":
metricName = "latest_fork_seconds"
val = val / 1e6
}
e.registerConstMetric(ch, metricName, val, t)
}
func (e *Exporter) registerConstMetricGauge(ch chan<- prometheus.Metric, metric string, val float64, labels ...string) {
e.registerConstMetric(ch, metric, val, prometheus.GaugeValue, labels...)
}
func (e *Exporter) registerConstMetric(ch chan<- prometheus.Metric, metric string, val float64, valType prometheus.ValueType, labelValues ...string) {
description := e.findOrCreateMetricDescription(metric, labelValues)
m, err := prometheus.NewConstMetric(description, valType, val, labelValues...)
if err != nil {
log.Debugf("registerConstMetric( %s , %.2f) err: %s", metric, val, err)
return
}
ch <- m
}
func (e *Exporter) registerConstSummary(ch chan<- prometheus.Metric, metric string, labelValues []string, count uint64, sum float64, latencyMap map[float64]float64, cmd string) {
description := e.findOrCreateMetricDescription(metric, labelValues)
// Create a constant summary from values we got from a 3rd party telemetry system.
summary := prometheus.MustNewConstSummary(
description,
count, sum,
latencyMap,
cmd,
)
ch <- summary
}
func (e *Exporter) registerConstHistogram(ch chan<- prometheus.Metric, metric string, labelValues []string, count uint64, sum float64, buckets map[float64]uint64, cmd string) {
description := e.findOrCreateMetricDescription(metric, labelValues)
histogram := prometheus.MustNewConstHistogram(
description,
count, sum,
buckets,
cmd,
)
ch <- histogram
}
func (e *Exporter) findOrCreateMetricDescription(metricName string, labels []string) *prometheus.Desc {
description, found := e.metricDescriptions[metricName]
if !found {
description = newMetricDescr(e.options.Namespace, metricName, metricName+" metric", labels)
e.metricDescriptions[metricName] = description
}
return description
}