Debian Codegraph @

exporter/metrics.go

raw symbols SCIP
1 package exporter
2
3 import (
4 "fmt"
5 "regexp"
6 "strconv"
7 "strings"
8
9 "github.com/prometheus/client_golang/prometheus"
10 log "github.com/sirupsen/logrus"
11 )
12
13 var metricNameRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
14
15 func sanitizeMetricName(n string) string {
16 return metricNameRE.ReplaceAllString(n, "_")
17 }
18
19 func newMetricDescr(namespace string, metricName string, docString string, labels []string) *prometheus.Desc {
20 return prometheus.NewDesc(prometheus.BuildFQName(namespace, "", metricName), docString, labels, nil)
21 }
22
23 func (e *Exporter) includeMetric(s string) bool {
24 if strings.HasPrefix(s, "db") || strings.HasPrefix(s, "cmdstat_") || strings.HasPrefix(s, "cluster_") {
25 return true
26 }
27 if _, ok := e.metricMapGauges[s]; ok {
28 return true
29 }
30
31 _, ok := e.metricMapCounters[s]
32 return ok
33 }
34
35 func (e *Exporter) parseAndRegisterConstMetric(ch chan<- prometheus.Metric, fieldKey, fieldValue string) {
36 orgMetricName := sanitizeMetricName(fieldKey)
37 metricName := orgMetricName
38 if newName, ok := e.metricMapGauges[metricName]; ok {
39 metricName = newName
40 } else {
41 if newName, ok := e.metricMapCounters[metricName]; ok {
42 metricName = newName
43 }
44 }
45
46 var err error
47 var val float64
48
49 switch fieldValue {
50
51 case "ok", "true":
52 val = 1
53
54 case "err", "fail", "false":
55 val = 0
56
57 default:
58 val, err = strconv.ParseFloat(fieldValue, 64)
59
60 }
61 if err != nil {
62 log.Debugf("couldn't parse %s, err: %s", fieldValue, err)
63 return
64 }
65
66 t := prometheus.GaugeValue
67 if e.metricMapCounters[orgMetricName] != "" {
68 t = prometheus.CounterValue
69 }
70
71 switch metricName {
72 case "latest_fork_usec":
73 metricName = "latest_fork_seconds"
74 val = val / 1e6
75 }
76
77 e.registerConstMetric(ch, metricName, val, t)
78 }
79
80 func (e *Exporter) registerConstMetricGauge(ch chan<- prometheus.Metric, metric string, val float64, labels ...string) {
81 e.registerConstMetric(ch, metric, val, prometheus.GaugeValue, labels...)
82 }
83
84 func (e *Exporter) registerConstMetric(ch chan<- prometheus.Metric, metric string, val float64, valType prometheus.ValueType, labelValues ...string) {
85 var desc *prometheus.Desc
86 if len(labelValues) == 0 {
87 desc = e.createMetricDescription(metric, nil)
88 } else {
89 desc = e.mustFindMetricDescription(metric)
90 }
91
92 if e.options.AppendInstanceRoleLabel && metric != "exporter_last_scrape_connect_time_seconds" && metric != "exporter_last_scrape_ping_time_seconds" {
93 labelValues = append(labelValues, e.instanceRole) // append instance_role label to all metrics
94 }
95 m, err := prometheus.NewConstMetric(desc, valType, val, labelValues...)
96 if err != nil {
97 log.Debugf("registerConstMetric( %s , %.2f) err: %s", metric, val, err)
98 return
99 }
100
101 ch <- m
102 }
103
104 func (e *Exporter) registerConstSummary(ch chan<- prometheus.Metric, metric string, count uint64, sum float64, latencyMap map[float64]float64, labelValues ...string) {
105 if e.options.AppendInstanceRoleLabel { // append instance_role label to all metrics
106 labelValues = append(labelValues, e.instanceRole)
107 }
108 // Create a constant summary from values we got from a 3rd party telemetry system.
109 summary := prometheus.MustNewConstSummary(
110 e.mustFindMetricDescription(metric),
111 count, sum,
112 latencyMap,
113 labelValues...,
114 )
115 ch <- summary
116 }
117
118 func (e *Exporter) registerConstHistogram(ch chan<- prometheus.Metric, metric string, count uint64, sum float64, buckets map[float64]uint64, labelValues ...string) {
119 if e.options.AppendInstanceRoleLabel { // append instance_role label to all metrics
120 labelValues = append(labelValues, e.instanceRole)
121 }
122 histogram := prometheus.MustNewConstHistogram(
123 e.mustFindMetricDescription(metric),
124 count, sum,
125 buckets,
126 labelValues...,
127 )
128 ch <- histogram
129 }
130
131 func (e *Exporter) mustFindMetricDescription(metricName string) *prometheus.Desc {
132 description, found := e.metricDescriptions[metricName]
133 if !found {
134 panic(fmt.Sprintf("couldn't find metric description for %s", metricName))
135 }
136 return description
137 }
138
139 func (e *Exporter) createMetricDescription(metricName string, labels []string) *prometheus.Desc {
140 if desc, found := e.metricDescriptions[metricName]; found {
141 return desc
142 }
143 if e.options.AppendInstanceRoleLabel && metricName != "exporter_last_scrape_connect_time_seconds" && metricName != "exporter_last_scrape_ping_time_seconds" {
144 labels = append(labels, "instance_role") // append instance_role label to all metrics (except 2 collected before instanceRole)
145 }
146 d := newMetricDescr(e.options.Namespace, metricName, metricName+" metric", labels)
147 e.metricDescriptions[metricName] = d
148 return d
149 }