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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
package prometheusmetrics
import (
"fmt"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/rcrowley/go-metrics"
)
// PrometheusConfig provides a container with config parameters for the
// Prometheus Exporter
type PrometheusConfig struct {
namespace string
Registry metrics.Registry // Registry to be exported
subsystem string
promRegistry prometheus.Registerer //Prometheus registry
FlushInterval time.Duration //interval to update prom metrics
gauges map[string]prometheus.Gauge
gaugeVecs map[string]prometheus.GaugeVec
}
// NewPrometheusProvider returns a Provider that produces Prometheus metrics.
// Namespace and subsystem are applied to all produced metrics.
func NewPrometheusProvider(r metrics.Registry, namespace string, subsystem string, promRegistry prometheus.Registerer, FlushInterval time.Duration) *PrometheusConfig {
return &PrometheusConfig{
namespace: namespace,
subsystem: subsystem,
Registry: r,
promRegistry: promRegistry,
FlushInterval: FlushInterval,
gauges: make(map[string]prometheus.Gauge),
gaugeVecs: make(map[string]prometheus.GaugeVec),
}
}
func (c *PrometheusConfig) flattenKey(key string) string {
key = strings.Replace(key, " ", "_", -1)
key = strings.Replace(key, ".", "_", -1)
key = strings.Replace(key, "-", "_", -1)
key = strings.Replace(key, "=", "_", -1)
return key
}
func (c *PrometheusConfig) gaugeFromNameAndValue(name string, val float64) {
key := fmt.Sprintf("%s_%s_%s", c.namespace, c.subsystem, name)
g, ok := c.gauges[key]
if !ok {
g = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: c.flattenKey(c.namespace),
Subsystem: c.flattenKey(c.subsystem),
Name: c.flattenKey(name),
Help: name,
})
c.promRegistry.MustRegister(g)
c.gauges[key] = g
}
g.Set(val)
}
func (c *PrometheusConfig) histogramFromName(name string, snap metrics.Histogram) {
key := fmt.Sprintf("%s_%s_%s", c.namespace, c.subsystem, name)
g, ok := c.gaugeVecs[key]
if !ok {
g = *prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: c.flattenKey(c.namespace),
Subsystem: c.flattenKey(c.subsystem),
Name: c.flattenKey(name),
Help: name,
},
[]string{
"type",
},
)
c.promRegistry.MustRegister(g)
c.gaugeVecs[key] = g
}
g.WithLabelValues("count").Set(float64(snap.Count()))
g.WithLabelValues("max").Set(float64(snap.Max()))
g.WithLabelValues("min").Set(float64(snap.Min()))
g.WithLabelValues("mean").Set(snap.Mean())
g.WithLabelValues("stddev").Set(snap.StdDev())
g.WithLabelValues("perc75").Set(snap.Percentile(float64(75)))
g.WithLabelValues("perc95").Set(snap.Percentile(float64(95)))
g.WithLabelValues("perc99").Set(snap.Percentile(float64(99)))
g.WithLabelValues("perc999").Set(snap.Percentile(float64(99.9)))
g.WithLabelValues("sum").Set(float64(snap.Sum()))
g.WithLabelValues("variance").Set(float64(snap.Variance()))
}
func (c *PrometheusConfig) meterFromName(name string, snap metrics.Meter) {
key := fmt.Sprintf("%s_%s_%s", c.namespace, c.subsystem, name)
g, ok := c.gaugeVecs[key]
if !ok {
g = *prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: c.flattenKey(c.namespace),
Subsystem: c.flattenKey(c.subsystem),
Name: c.flattenKey(name),
Help: name,
},
[]string{
"type",
},
)
c.promRegistry.MustRegister(g)
c.gaugeVecs[key] = g
}
g.WithLabelValues("count").Set(float64(snap.Count()))
g.WithLabelValues("rate1").Set(snap.Rate1())
g.WithLabelValues("rate5").Set(snap.Rate5())
g.WithLabelValues("rate15").Set(snap.Rate15())
g.WithLabelValues("rate_mean").Set(snap.RateMean())
}
func (c *PrometheusConfig) timerFromName(name string, snap metrics.Timer) {
key := fmt.Sprintf("%s_%s_%s", c.namespace, c.subsystem, name)
g, ok := c.gaugeVecs[key]
if !ok {
g = *prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: c.flattenKey(c.namespace),
Subsystem: c.flattenKey(c.subsystem),
Name: c.flattenKey(name),
Help: name,
},
[]string{
"type",
},
)
c.promRegistry.MustRegister(g)
c.gaugeVecs[key] = g
}
g.WithLabelValues("count").Set(float64(snap.Count()))
g.WithLabelValues("max").Set(float64(snap.Max()))
g.WithLabelValues("min").Set(float64(snap.Min()))
g.WithLabelValues("mean").Set(snap.Mean())
g.WithLabelValues("stddev").Set(snap.StdDev())
g.WithLabelValues("perc75").Set(snap.Percentile(float64(75)))
g.WithLabelValues("perc95").Set(snap.Percentile(float64(95)))
g.WithLabelValues("perc99").Set(snap.Percentile(float64(99)))
g.WithLabelValues("perc999").Set(snap.Percentile(float64(99.9)))
g.WithLabelValues("sum").Set(float64(snap.Sum()))
g.WithLabelValues("variance").Set(float64(snap.Variance()))
g.WithLabelValues("rate1").Set(snap.Rate1())
g.WithLabelValues("rate5").Set(snap.Rate5())
g.WithLabelValues("rate15").Set(snap.Rate15())
g.WithLabelValues("rate_mean").Set(snap.RateMean())
}
func (c *PrometheusConfig) UpdatePrometheusMetrics() {
c.UpdatePrometheusMetricsOnce()
for _ = range time.Tick(c.FlushInterval) {
c.UpdatePrometheusMetricsOnce()
}
}
func (c *PrometheusConfig) UpdatePrometheusMetricsOnce() error {
c.Registry.Each(func(name string, i interface{}) {
switch metric := i.(type) {
case metrics.Counter:
c.gaugeFromNameAndValue(name, float64(metric.Count()))
case metrics.Gauge:
c.gaugeFromNameAndValue(name, float64(metric.Value()))
case metrics.GaugeFloat64:
c.gaugeFromNameAndValue(name, float64(metric.Value()))
case metrics.Histogram:
snap := metric.Snapshot()
c.histogramFromName(name, snap)
case metrics.Meter:
snap := metric.Snapshot()
c.meterFromName(name, snap)
case metrics.Timer:
snap := metric.Snapshot()
c.timerFromName(name, snap)
}
})
return nil
}
|