| 1 |
package prometheusmetrics |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
"time" |
| 7 |
|
| 8 |
"github.com/prometheus/client_golang/prometheus" |
| 9 |
"github.com/rcrowley/go-metrics" |
| 10 |
) |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
type PrometheusConfig struct { |
| 16 |
namespace string |
| 17 |
Registry metrics.Registry |
| 18 |
subsystem string |
| 19 |
promRegistry prometheus.Registerer |
| 20 |
FlushInterval time.Duration |
| 21 |
gauges map[string]prometheus.Gauge |
| 22 |
gaugeVecs map[string]prometheus.GaugeVec |
| 23 |
} |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
func NewPrometheusProvider(r metrics.Registry, namespace string, subsystem string, promRegistry prometheus.Registerer, FlushInterval time.Duration) *PrometheusConfig { |
| 28 |
return &PrometheusConfig{ |
| 29 |
namespace: namespace, |
| 30 |
subsystem: subsystem, |
| 31 |
Registry: r, |
| 32 |
promRegistry: promRegistry, |
| 33 |
FlushInterval: FlushInterval, |
| 34 |
gauges: make(map[string]prometheus.Gauge), |
| 35 |
gaugeVecs: make(map[string]prometheus.GaugeVec), |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
func (c *PrometheusConfig) flattenKey(key string) string { |
| 40 |
key = strings.Replace(key, " ", "_", -1) |
| 41 |
key = strings.Replace(key, ".", "_", -1) |
| 42 |
key = strings.Replace(key, "-", "_", -1) |
| 43 |
key = strings.Replace(key, "=", "_", -1) |
| 44 |
return key |
| 45 |
} |
| 46 |
|
| 47 |
func (c *PrometheusConfig) gaugeFromNameAndValue(name string, val float64) { |
| 48 |
key := fmt.Sprintf("%s_%s_%s", c.namespace, c.subsystem, name) |
| 49 |
g, ok := c.gauges[key] |
| 50 |
if !ok { |
| 51 |
g = prometheus.NewGauge(prometheus.GaugeOpts{ |
| 52 |
Namespace: c.flattenKey(c.namespace), |
| 53 |
Subsystem: c.flattenKey(c.subsystem), |
| 54 |
Name: c.flattenKey(name), |
| 55 |
Help: name, |
| 56 |
}) |
| 57 |
c.promRegistry.MustRegister(g) |
| 58 |
c.gauges[key] = g |
| 59 |
} |
| 60 |
g.Set(val) |
| 61 |
} |
| 62 |
|
| 63 |
func (c *PrometheusConfig) histogramFromName(name string, snap metrics.Histogram) { |
| 64 |
key := fmt.Sprintf("%s_%s_%s", c.namespace, c.subsystem, name) |
| 65 |
g, ok := c.gaugeVecs[key] |
| 66 |
if !ok { |
| 67 |
g = *prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 68 |
Namespace: c.flattenKey(c.namespace), |
| 69 |
Subsystem: c.flattenKey(c.subsystem), |
| 70 |
Name: c.flattenKey(name), |
| 71 |
Help: name, |
| 72 |
}, |
| 73 |
[]string{ |
| 74 |
"type", |
| 75 |
}, |
| 76 |
) |
| 77 |
c.promRegistry.MustRegister(g) |
| 78 |
c.gaugeVecs[key] = g |
| 79 |
} |
| 80 |
|
| 81 |
g.WithLabelValues("count").Set(float64(snap.Count())) |
| 82 |
g.WithLabelValues("max").Set(float64(snap.Max())) |
| 83 |
g.WithLabelValues("min").Set(float64(snap.Min())) |
| 84 |
g.WithLabelValues("mean").Set(snap.Mean()) |
| 85 |
g.WithLabelValues("stddev").Set(snap.StdDev()) |
| 86 |
g.WithLabelValues("perc75").Set(snap.Percentile(float64(75))) |
| 87 |
g.WithLabelValues("perc95").Set(snap.Percentile(float64(95))) |
| 88 |
g.WithLabelValues("perc99").Set(snap.Percentile(float64(99))) |
| 89 |
g.WithLabelValues("perc999").Set(snap.Percentile(float64(99.9))) |
| 90 |
g.WithLabelValues("sum").Set(float64(snap.Sum())) |
| 91 |
g.WithLabelValues("variance").Set(float64(snap.Variance())) |
| 92 |
} |
| 93 |
|
| 94 |
func (c *PrometheusConfig) meterFromName(name string, snap metrics.Meter) { |
| 95 |
key := fmt.Sprintf("%s_%s_%s", c.namespace, c.subsystem, name) |
| 96 |
g, ok := c.gaugeVecs[key] |
| 97 |
if !ok { |
| 98 |
g = *prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 99 |
Namespace: c.flattenKey(c.namespace), |
| 100 |
Subsystem: c.flattenKey(c.subsystem), |
| 101 |
Name: c.flattenKey(name), |
| 102 |
Help: name, |
| 103 |
}, |
| 104 |
[]string{ |
| 105 |
"type", |
| 106 |
}, |
| 107 |
) |
| 108 |
c.promRegistry.MustRegister(g) |
| 109 |
c.gaugeVecs[key] = g |
| 110 |
} |
| 111 |
|
| 112 |
g.WithLabelValues("count").Set(float64(snap.Count())) |
| 113 |
g.WithLabelValues("rate1").Set(snap.Rate1()) |
| 114 |
g.WithLabelValues("rate5").Set(snap.Rate5()) |
| 115 |
g.WithLabelValues("rate15").Set(snap.Rate15()) |
| 116 |
g.WithLabelValues("rate_mean").Set(snap.RateMean()) |
| 117 |
} |
| 118 |
|
| 119 |
func (c *PrometheusConfig) timerFromName(name string, snap metrics.Timer) { |
| 120 |
key := fmt.Sprintf("%s_%s_%s", c.namespace, c.subsystem, name) |
| 121 |
g, ok := c.gaugeVecs[key] |
| 122 |
if !ok { |
| 123 |
g = *prometheus.NewGaugeVec(prometheus.GaugeOpts{ |
| 124 |
Namespace: c.flattenKey(c.namespace), |
| 125 |
Subsystem: c.flattenKey(c.subsystem), |
| 126 |
Name: c.flattenKey(name), |
| 127 |
Help: name, |
| 128 |
}, |
| 129 |
[]string{ |
| 130 |
"type", |
| 131 |
}, |
| 132 |
) |
| 133 |
c.promRegistry.MustRegister(g) |
| 134 |
c.gaugeVecs[key] = g |
| 135 |
} |
| 136 |
|
| 137 |
g.WithLabelValues("count").Set(float64(snap.Count())) |
| 138 |
g.WithLabelValues("max").Set(float64(snap.Max())) |
| 139 |
g.WithLabelValues("min").Set(float64(snap.Min())) |
| 140 |
g.WithLabelValues("mean").Set(snap.Mean()) |
| 141 |
g.WithLabelValues("stddev").Set(snap.StdDev()) |
| 142 |
g.WithLabelValues("perc75").Set(snap.Percentile(float64(75))) |
| 143 |
g.WithLabelValues("perc95").Set(snap.Percentile(float64(95))) |
| 144 |
g.WithLabelValues("perc99").Set(snap.Percentile(float64(99))) |
| 145 |
g.WithLabelValues("perc999").Set(snap.Percentile(float64(99.9))) |
| 146 |
g.WithLabelValues("sum").Set(float64(snap.Sum())) |
| 147 |
g.WithLabelValues("variance").Set(float64(snap.Variance())) |
| 148 |
|
| 149 |
g.WithLabelValues("rate1").Set(snap.Rate1()) |
| 150 |
g.WithLabelValues("rate5").Set(snap.Rate5()) |
| 151 |
g.WithLabelValues("rate15").Set(snap.Rate15()) |
| 152 |
g.WithLabelValues("rate_mean").Set(snap.RateMean()) |
| 153 |
} |
| 154 |
|
| 155 |
func (c *PrometheusConfig) UpdatePrometheusMetrics() { |
| 156 |
c.UpdatePrometheusMetricsOnce() |
| 157 |
for _ = range time.Tick(c.FlushInterval) { |
| 158 |
c.UpdatePrometheusMetricsOnce() |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func (c *PrometheusConfig) UpdatePrometheusMetricsOnce() error { |
| 163 |
c.Registry.Each(func(name string, i interface{}) { |
| 164 |
switch metric := i.(type) { |
| 165 |
case metrics.Counter: |
| 166 |
c.gaugeFromNameAndValue(name, float64(metric.Count())) |
| 167 |
case metrics.Gauge: |
| 168 |
c.gaugeFromNameAndValue(name, float64(metric.Value())) |
| 169 |
case metrics.GaugeFloat64: |
| 170 |
c.gaugeFromNameAndValue(name, float64(metric.Value())) |
| 171 |
case metrics.Histogram: |
| 172 |
snap := metric.Snapshot() |
| 173 |
c.histogramFromName(name, snap) |
| 174 |
case metrics.Meter: |
| 175 |
snap := metric.Snapshot() |
| 176 |
c.meterFromName(name, snap) |
| 177 |
case metrics.Timer: |
| 178 |
snap := metric.Snapshot() |
| 179 |
c.timerFromName(name, snap) |
| 180 |
} |
| 181 |
}) |
| 182 |
return nil |
| 183 |
} |