• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

oliver006 / redis_exporter / 17172001870

23 Aug 2025 05:58AM UTC coverage: 84.921% (-0.9%) from 85.87%
17172001870

Pull #1028

github

web-flow
Merge 891f7f01e into 7632b7b20
Pull Request #1028: sirupsen/log --> log/slog

121 of 249 new or added lines in 18 files covered. (48.59%)

6 existing lines in 1 file now uncovered.

2568 of 3024 relevant lines covered (84.92%)

13254.26 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.74
/exporter/metrics.go
1
package exporter
2

3
import (
4
        "fmt"
5
        "log/slog"
6
        "regexp"
7
        "strconv"
8
        "strings"
9

10
        "github.com/prometheus/client_golang/prometheus"
11
)
12

13
var metricNameRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
14

15
func sanitizeMetricName(n string) string {
237,533✔
16
        return metricNameRE.ReplaceAllString(n, "_")
237,533✔
17
}
237,533✔
18

19
func newMetricDescr(namespace string, metricName string, docString string, labels []string) *prometheus.Desc {
869,953✔
20
        return prometheus.NewDesc(prometheus.BuildFQName(namespace, "", metricName), docString, labels, nil)
869,953✔
21
}
869,953✔
22

23
func (e *Exporter) includeMetric(s string) bool {
354,391✔
24
        if strings.HasPrefix(s, "db") || strings.HasPrefix(s, "cmdstat_") || strings.HasPrefix(s, "cluster_") {
363,083✔
25
                return true
8,692✔
26
        }
8,692✔
27
        if _, ok := e.metricMapGauges[s]; ok {
517,579✔
28
                return true
171,880✔
29
        }
171,880✔
30

31
        _, ok := e.metricMapCounters[s]
173,819✔
32
        return ok
173,819✔
33
}
34

35
func (e *Exporter) parseAndRegisterConstMetric(ch chan<- prometheus.Metric, fieldKey, fieldValue string) {
237,531✔
36
        orgMetricName := sanitizeMetricName(fieldKey)
237,531✔
37
        metricName := orgMetricName
237,531✔
38
        if newName, ok := e.metricMapGauges[metricName]; ok {
410,053✔
39
                metricName = newName
172,522✔
40
        } else {
237,531✔
41
                if newName, ok := e.metricMapCounters[metricName]; ok {
121,968✔
42
                        metricName = newName
56,959✔
43
                }
56,959✔
44
        }
45

46
        var err error
237,531✔
47
        var val float64
237,531✔
48

237,531✔
49
        switch fieldValue {
237,531✔
50

51
        case "ok", "true":
6,079✔
52
                val = 1
6,079✔
53

54
        case "err", "fail", "false":
1✔
55
                val = 0
1✔
56

57
        default:
231,451✔
58
                val, err = strconv.ParseFloat(fieldValue, 64)
231,451✔
59

60
        }
61
        if err != nil {
237,561✔
62
                slog.Debug("couldn't parse", "fieldValue", fieldValue, "error", err)
30✔
63
                return
30✔
64
        }
30✔
65

66
        t := prometheus.GaugeValue
237,501✔
67
        if e.metricMapCounters[orgMetricName] != "" {
296,218✔
68
                t = prometheus.CounterValue
58,717✔
69
        }
58,717✔
70

71
        switch metricName {
237,501✔
72
        case "latest_fork_usec":
1,921✔
73
                metricName = "latest_fork_seconds"
1,921✔
74
                val = val / 1e6
1,921✔
75
        }
76

77
        e.registerConstMetric(ch, metricName, val, t)
237,501✔
78
}
79

80
func (e *Exporter) registerConstMetricGauge(ch chan<- prometheus.Metric, metric string, val float64, labels ...string) {
54,230✔
81
        e.registerConstMetric(ch, metric, val, prometheus.GaugeValue, labels...)
54,230✔
82
}
54,230✔
83

84
func (e *Exporter) registerConstMetric(ch chan<- prometheus.Metric, metric string, val float64, valType prometheus.ValueType, labelValues ...string) {
402,956✔
85
        var desc *prometheus.Desc
402,956✔
86
        if len(labelValues) == 0 {
665,517✔
87
                desc = e.createMetricDescription(metric, nil)
262,561✔
88
        } else {
402,956✔
89
                desc = e.mustFindMetricDescription(metric)
140,395✔
90
        }
140,395✔
91

92
        m, err := prometheus.NewConstMetric(desc, valType, val, labelValues...)
402,956✔
93
        if err != nil {
402,956✔
NEW
94
                slog.Debug("registerConstMetric err", "metric", metric, "value", val, "error", err)
×
95
                return
×
96
        }
×
97

98
        ch <- m
402,956✔
99
}
100

101
func (e *Exporter) registerConstSummary(ch chan<- prometheus.Metric, metric string, count uint64, sum float64, latencyMap map[float64]float64, labelValues ...string) {
19,070✔
102
        // Create a constant summary from values we got from a 3rd party telemetry system.
19,070✔
103
        summary := prometheus.MustNewConstSummary(
19,070✔
104
                e.mustFindMetricDescription(metric),
19,070✔
105
                count, sum,
19,070✔
106
                latencyMap,
19,070✔
107
                labelValues...,
19,070✔
108
        )
19,070✔
109
        ch <- summary
19,070✔
110
}
19,070✔
111

112
func (e *Exporter) registerConstHistogram(ch chan<- prometheus.Metric, metric string, count uint64, sum float64, buckets map[float64]uint64, labelValues ...string) {
19,021✔
113
        histogram := prometheus.MustNewConstHistogram(
19,021✔
114
                e.mustFindMetricDescription(metric),
19,021✔
115
                count, sum,
19,021✔
116
                buckets,
19,021✔
117
                labelValues...,
19,021✔
118
        )
19,021✔
119
        ch <- histogram
19,021✔
120
}
19,021✔
121

122
func (e *Exporter) mustFindMetricDescription(metricName string) *prometheus.Desc {
178,486✔
123
        description, found := e.metricDescriptions[metricName]
178,486✔
124
        if !found {
178,486✔
125
                panic(fmt.Sprintf("couldn't find metric description for %s", metricName))
×
126
        }
127
        return description
178,486✔
128
}
129

130
func (e *Exporter) createMetricDescription(metricName string, labels []string) *prometheus.Desc {
413,051✔
131
        if desc, found := e.metricDescriptions[metricName]; found {
574,030✔
132
                return desc
160,979✔
133
        }
160,979✔
134
        d := newMetricDescr(e.options.Namespace, metricName, metricName+" metric", labels)
252,072✔
135
        e.metricDescriptions[metricName] = d
252,072✔
136
        return d
252,072✔
137
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc