| 1 | package exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/ |
| 2 | |
| 3 | import ( |
| 4 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 5 | "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/" |
| 6 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 7 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 8 | |
| 9 | "github.com/prometheus/client_golang/prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/" |
| 10 | logscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/ "github.com/sirupsen/logrusscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/" |
| 11 | ) |
| 12 | |
| 13 | var metricNameREscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/metricNameRE. = regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile().(`[^a-zA-Z0-9_]`) |
| 14 | |
| 15 | func sanitizeMetricNamescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/sanitizeMetricName().(n string) string { |
| 16 | return metricNameREscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/metricNameRE..ReplaceAllStringscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#ReplaceAllString().(n, "_") |
| 17 | } |
| 18 | |
| 19 | func newMetricDescrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/newMetricDescr().(namespace string, metricName string, docString string, labels []string) *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc# { |
| 20 | return prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.NewDescscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/NewDesc().(prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.BuildFQNamescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/BuildFQName().(namespace, "", metricName), docString, labels, nil) |
| 21 | } |
| 22 | |
| 23 | func (e *Exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#) includeMetricscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#includeMetric().(s string) bool { |
| 24 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(s, "db") || stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(s, "cmdstat_") || stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(s, "cluster_") { |
| 25 | return true |
| 26 | } |
| 27 | if _, ok := e.metricMapGaugesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#metricMapGauges.[s]; ok { |
| 28 | return true |
| 29 | } |
| 30 | |
| 31 | _, ok := e.metricMapCountersscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#metricMapCounters.[s] |
| 32 | return ok |
| 33 | } |
| 34 | |
| 35 | func (e *Exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#) parseAndRegisterConstMetricscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#parseAndRegisterConstMetric().(ch chan<- prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Metricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#, fieldKey, fieldValue string) { |
| 36 | orgMetricName := sanitizeMetricNamescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/sanitizeMetricName().(fieldKey) |
| 37 | metricName := orgMetricName |
| 38 | if newName, ok := e.metricMapGaugesscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#metricMapGauges.[metricName]; ok { |
| 39 | metricName = newName |
| 40 | } else { |
| 41 | if newName, ok := e.metricMapCountersscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#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 = strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.ParseFloatscip-go gomod github.com/golang/go/src go1.25.0 strconv/ParseFloat().(fieldValue, 64) |
| 59 | |
| 60 | } |
| 61 | if err != nil { |
| 62 | logscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/.Debugfscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/Debugf().("couldn't parse %s, err: %s", fieldValue, err) |
| 63 | return |
| 64 | } |
| 65 | |
| 66 | t := prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.GaugeValuescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/GaugeValue. |
| 67 | if e.metricMapCountersscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#metricMapCounters.[orgMetricName] != "" { |
| 68 | t = prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.CounterValuescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/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.registerConstMetricscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#registerConstMetric().(ch, metricName, val, t) |
| 78 | } |
| 79 | |
| 80 | func (e *Exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#) registerConstMetricGaugescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#registerConstMetricGauge().(ch chan<- prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Metricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#, metric string, val float64, labels ...string) { |
| 81 | e.registerConstMetricscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#registerConstMetric().(ch, metric, val, prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.GaugeValuescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/GaugeValue., labels...) |
| 82 | } |
| 83 | |
| 84 | func (e *Exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#) registerConstMetricscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#registerConstMetric().(ch chan<- prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Metricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#, metric string, val float64, valType prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.ValueTypescip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/ValueType#, labelValues ...string) { |
| 85 | var desc *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc# |
| 86 | if len(labelValues) == 0 { |
| 87 | desc = e.createMetricDescriptionscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#createMetricDescription().(metric, nil) |
| 88 | } else { |
| 89 | desc = e.mustFindMetricDescriptionscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#mustFindMetricDescription().(metric) |
| 90 | } |
| 91 | |
| 92 | if e.optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#options..AppendInstanceRoleLabelscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#AppendInstanceRoleLabel. && metric != "exporter_last_scrape_connect_time_seconds" && metric != "exporter_last_scrape_ping_time_seconds" { |
| 93 | labelValues = append(labelValues, e.instanceRolescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#instanceRole.) // append instance_role label to all metrics |
| 94 | } |
| 95 | m, err := prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.NewConstMetricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/NewConstMetric().(desc, valType, val, labelValues...) |
| 96 | if err != nil { |
| 97 | logscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/.Debugfscip-go gomod github.com/sirupsen/logrus v1.9.4 `github.com/sirupsen/logrus`/Debugf().("registerConstMetric( %s , %.2f) err: %s", metric, val, err) |
| 98 | return |
| 99 | } |
| 100 | |
| 101 | ch <- m |
| 102 | } |
| 103 | |
| 104 | func (e *Exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#) registerConstSummaryscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#registerConstSummary().(ch chan<- prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Metricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#, metric string, count uint64, sum float64, latencyMap map[float64]float64, labelValues ...string) { |
| 105 | if e.optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#options..AppendInstanceRoleLabelscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#AppendInstanceRoleLabel. { // append instance_role label to all metrics |
| 106 | labelValues = append(labelValues, e.instanceRolescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#instanceRole.) |
| 107 | } |
| 108 | // Create a constant summary from values we got from a 3rd party telemetry system. |
| 109 | summary := prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.MustNewConstSummaryscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/MustNewConstSummary().( |
| 110 | e.mustFindMetricDescriptionscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#mustFindMetricDescription().(metric), |
| 111 | count, sum, |
| 112 | latencyMap, |
| 113 | labelValues..., |
| 114 | ) |
| 115 | ch <- summary |
| 116 | } |
| 117 | |
| 118 | func (e *Exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#) registerConstHistogramscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#registerConstHistogram().(ch chan<- prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Metricscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Metric#, metric string, count uint64, sum float64, buckets map[float64]uint64, labelValues ...string) { |
| 119 | if e.optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#options..AppendInstanceRoleLabelscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#AppendInstanceRoleLabel. { // append instance_role label to all metrics |
| 120 | labelValues = append(labelValues, e.instanceRolescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#instanceRole.) |
| 121 | } |
| 122 | histogram := prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.MustNewConstHistogramscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/MustNewConstHistogram().( |
| 123 | e.mustFindMetricDescriptionscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#mustFindMetricDescription().(metric), |
| 124 | count, sum, |
| 125 | buckets, |
| 126 | labelValues..., |
| 127 | ) |
| 128 | ch <- histogram |
| 129 | } |
| 130 | |
| 131 | func (e *Exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#) mustFindMetricDescriptionscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#mustFindMetricDescription().(metricName string) *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc# { |
| 132 | description, found := e.metricDescriptionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#metricDescriptions.[metricName] |
| 133 | if !found { |
| 134 | panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("couldn't find metric description for %s", metricName)) |
| 135 | } |
| 136 | return description |
| 137 | } |
| 138 | |
| 139 | func (e *Exporterscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#) createMetricDescriptionscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#createMetricDescription().(metricName string, labels []string) *prometheusscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/.Descscip-go gomod github.com/prometheus/client_golang v1.23.2 `github.com/prometheus/client_golang/prometheus`/Desc# { |
| 140 | if desc, found := e.metricDescriptionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#metricDescriptions.[metricName]; found { |
| 141 | return desc |
| 142 | } |
| 143 | if e.optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#options..AppendInstanceRoleLabelscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/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 := newMetricDescrscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/newMetricDescr().(e.optionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#options..Namespacescip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Options#Namespace., metricName, metricName+" metric", labels) |
| 147 | e.metricDescriptionsscip-go gomod github.com/oliver006/redis_exporter . `github.com/oliver006/redis_exporter/exporter`/Exporter#metricDescriptions.[metricName] = d |
| 148 | return d |
| 149 | } |