2019-01-25 17:27:27 +03:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
2021-11-03 17:50:29 +03:00
|
|
|
"fmt"
|
2021-11-03 11:21:54 +03:00
|
|
|
"io"
|
2020-06-10 16:04:12 +03:00
|
|
|
"sync"
|
2021-11-03 11:21:54 +03:00
|
|
|
"time"
|
2019-01-26 12:33:51 +03:00
|
|
|
|
2019-01-25 17:27:27 +03:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-11-03 11:21:54 +03:00
|
|
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
"github.com/prometheus/common/expfmt"
|
|
|
|
"go.unistack.org/micro/v3/meter"
|
2019-01-25 17:27:27 +03:00
|
|
|
)
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
type prometheusMeter struct {
|
|
|
|
opts meter.Options
|
|
|
|
set prometheus.Registerer
|
|
|
|
counter map[string]prometheusCounter
|
|
|
|
floatCounter map[string]prometheusFloatCounter
|
|
|
|
gauge map[string]prometheusGauge
|
|
|
|
histogram map[string]prometheusHistogram
|
|
|
|
summary map[string]prometheusSummary
|
|
|
|
sync.Mutex
|
|
|
|
}
|
2021-01-19 16:51:58 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func NewMeter(opts ...meter.Option) meter.Meter {
|
|
|
|
return &prometheusMeter{
|
|
|
|
set: prometheus.DefaultRegisterer,
|
|
|
|
opts: meter.NewOptions(opts...),
|
|
|
|
counter: make(map[string]prometheusCounter),
|
|
|
|
floatCounter: make(map[string]prometheusFloatCounter),
|
|
|
|
gauge: make(map[string]prometheusGauge),
|
|
|
|
histogram: make(map[string]prometheusHistogram),
|
|
|
|
summary: make(map[string]prometheusSummary),
|
|
|
|
}
|
|
|
|
}
|
2021-01-19 16:51:58 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) buildMetric(name string, labels ...string) string {
|
|
|
|
if len(m.opts.MetricPrefix) > 0 {
|
|
|
|
name = m.opts.MetricPrefix + name
|
|
|
|
}
|
2020-06-10 16:04:12 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
nl := len(m.opts.Labels) + len(labels)
|
|
|
|
if nl == 0 {
|
|
|
|
return name
|
|
|
|
}
|
2020-06-10 16:04:12 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
nlabels := make([]string, 0, nl)
|
|
|
|
nlabels = append(nlabels, m.opts.Labels...)
|
|
|
|
nlabels = append(nlabels, labels...)
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
if len(m.opts.LabelPrefix) == 0 {
|
|
|
|
return meter.BuildName(name, nlabels...)
|
|
|
|
}
|
2019-01-26 12:33:51 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
for idx := 0; idx < nl; idx++ {
|
|
|
|
nlabels[idx] = m.opts.LabelPrefix + nlabels[idx]
|
|
|
|
idx++
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
2021-11-03 11:21:54 +03:00
|
|
|
return meter.BuildName(name, nlabels...)
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) buildName(name string) string {
|
|
|
|
if len(m.opts.MetricPrefix) > 0 {
|
|
|
|
name = m.opts.MetricPrefix + name
|
2019-01-26 12:33:51 +03:00
|
|
|
}
|
2021-11-03 11:21:54 +03:00
|
|
|
return name
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2019-01-26 12:33:51 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) buildLabels(labels ...string) []string {
|
|
|
|
nl := len(m.opts.Labels) + len(labels)
|
|
|
|
if nl == 0 {
|
|
|
|
return nil
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
nlabels := make([]string, 0, nl)
|
|
|
|
nlabels = append(nlabels, m.opts.Labels...)
|
|
|
|
nlabels = append(nlabels, labels...)
|
|
|
|
|
|
|
|
for idx := 0; idx < nl; idx++ {
|
|
|
|
nlabels[idx] = m.opts.LabelPrefix + nlabels[idx]
|
|
|
|
idx++
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
2021-11-03 11:21:54 +03:00
|
|
|
return nlabels
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) Name() string {
|
|
|
|
return m.opts.Name
|
|
|
|
}
|
2021-01-19 16:51:58 +03:00
|
|
|
|
2022-03-05 19:09:31 +03:00
|
|
|
func (m *prometheusMeter) mapLabels(src ...string) map[string]string {
|
|
|
|
src = m.buildLabels(src...)
|
|
|
|
mp := make(map[string]string, len(src)/2)
|
|
|
|
for idx := 0; idx < len(src); idx++ {
|
|
|
|
mp[src[idx]] = src[idx+1]
|
2021-11-03 11:21:54 +03:00
|
|
|
idx++
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
2022-03-05 19:09:31 +03:00
|
|
|
return mp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *prometheusMeter) metricEqual(src []string, dst []string) bool {
|
|
|
|
if len(src) != len(dst)/2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
dst = m.buildLabels(dst...)
|
|
|
|
mp := make(map[string]struct{}, len(src))
|
|
|
|
for idx := range src {
|
|
|
|
mp[src[idx]] = struct{}{}
|
|
|
|
}
|
|
|
|
for idx := 0; idx < len(dst); idx++ {
|
|
|
|
if _, ok := mp[dst[idx]]; !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *prometheusMeter) labelNames(src map[string]string, dst []string) []string {
|
|
|
|
dst = m.buildLabels(dst...)
|
|
|
|
nlabels := make([]string, 0, len(src))
|
|
|
|
for idx := 0; idx < len(dst); idx++ {
|
|
|
|
if src == nil {
|
|
|
|
nlabels = append(nlabels, dst[idx])
|
|
|
|
} else if _, ok := src[dst[idx]]; ok {
|
|
|
|
nlabels = append(nlabels, dst[idx])
|
|
|
|
} else {
|
|
|
|
nlabels = append(nlabels, dst[idx])
|
|
|
|
}
|
|
|
|
idx++
|
|
|
|
}
|
|
|
|
return nlabels
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2021-01-19 16:51:58 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2022-01-23 16:13:58 +03:00
|
|
|
nm := m.buildName(name)
|
2021-11-03 11:21:54 +03:00
|
|
|
c, ok := m.counter[nm]
|
2022-03-05 19:09:31 +03:00
|
|
|
var lnames []string
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2022-03-05 19:09:31 +03:00
|
|
|
lnames = m.labelNames(nil, labels)
|
|
|
|
fmt.Printf("!ok lnames: %v\n", lnames)
|
|
|
|
nc := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: nm}, lnames)
|
|
|
|
c = prometheusCounter{c: nc, lnames: lnames}
|
|
|
|
m.counter[nm] = c
|
|
|
|
} else if !m.metricEqual(c.lnames, labels) {
|
|
|
|
fmt.Printf("ok && !m.metricEqual lnames: %v labels: %v\n", c.lnames, labels)
|
|
|
|
lnames = m.labelNames(c.labels, labels)
|
|
|
|
m.set.Unregister(c.c)
|
|
|
|
nc := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: nm}, lnames)
|
|
|
|
c = prometheusCounter{c: nc, lnames: lnames}
|
2021-11-03 11:21:54 +03:00
|
|
|
m.counter[nm] = c
|
2022-03-05 19:09:31 +03:00
|
|
|
m.set.MustRegister(c.c)
|
|
|
|
} else {
|
|
|
|
lnames = c.lnames
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2022-03-05 19:09:31 +03:00
|
|
|
fmt.Printf("lnames %v\n", lnames)
|
|
|
|
return prometheusCounter{c: c.c, lnames: lnames, labels: m.mapLabels(labels...)}
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2021-01-19 16:51:58 +03:00
|
|
|
|
2022-01-23 16:13:58 +03:00
|
|
|
nm := m.buildName(name)
|
2021-11-03 11:21:54 +03:00
|
|
|
c, ok := m.floatCounter[nm]
|
|
|
|
if !ok {
|
2022-03-05 19:09:31 +03:00
|
|
|
nc := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: nm}, m.labelNames(c.labels, labels))
|
|
|
|
c = prometheusFloatCounter{c: nc}
|
|
|
|
m.floatCounter[nm] = c
|
|
|
|
} else if !m.metricEqual(c.lnames, labels) {
|
|
|
|
m.set.Unregister(c.c)
|
|
|
|
nc := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: nm}, m.labelNames(c.labels, labels))
|
2021-11-03 11:21:54 +03:00
|
|
|
c = prometheusFloatCounter{c: nc}
|
|
|
|
m.floatCounter[nm] = c
|
2022-03-05 19:09:31 +03:00
|
|
|
m.set.MustRegister(c.c)
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 19:09:31 +03:00
|
|
|
return prometheusFloatCounter{c: c.c, labels: m.mapLabels(labels...)}
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2021-01-19 16:51:58 +03:00
|
|
|
|
2022-01-23 16:13:58 +03:00
|
|
|
nm := m.buildName(name)
|
2021-11-03 11:21:54 +03:00
|
|
|
c, ok := m.gauge[nm]
|
|
|
|
if !ok {
|
2022-03-05 19:09:31 +03:00
|
|
|
nc := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: nm}, m.labelNames(c.labels, labels))
|
2021-11-03 11:21:54 +03:00
|
|
|
c = prometheusGauge{c: nc}
|
|
|
|
m.gauge[nm] = c
|
2022-03-05 19:09:31 +03:00
|
|
|
} else if !m.metricEqual(c.lnames, labels) {
|
|
|
|
m.set.Unregister(c.c)
|
|
|
|
nc := prometheus.NewGaugeVec(prometheus.GaugeOpts{Name: nm}, m.labelNames(c.labels, labels))
|
|
|
|
c = prometheusGauge{c: nc}
|
|
|
|
m.gauge[nm] = c
|
|
|
|
m.set.MustRegister(c.c)
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 19:09:31 +03:00
|
|
|
return prometheusGauge{c: c.c, labels: m.mapLabels(labels...)}
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2021-01-19 16:51:58 +03:00
|
|
|
|
2022-01-23 16:13:58 +03:00
|
|
|
nm := m.buildName(name)
|
2021-11-03 11:21:54 +03:00
|
|
|
c, ok := m.histogram[nm]
|
|
|
|
if !ok {
|
2022-03-05 19:09:31 +03:00
|
|
|
nc := prometheus.NewHistogramVec(prometheus.HistogramOpts{Name: nm}, m.labelNames(c.labels, labels))
|
2021-11-03 11:21:54 +03:00
|
|
|
c = prometheusHistogram{c: nc}
|
|
|
|
m.histogram[nm] = c
|
2022-03-05 19:09:31 +03:00
|
|
|
} else if !m.metricEqual(c.lnames, labels) {
|
|
|
|
m.set.Unregister(c.c)
|
|
|
|
nc := prometheus.NewHistogramVec(prometheus.HistogramOpts{Name: nm}, m.labelNames(c.labels, labels))
|
|
|
|
c = prometheusHistogram{c: nc}
|
|
|
|
m.histogram[nm] = c
|
|
|
|
m.set.MustRegister(c.c)
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 19:09:31 +03:00
|
|
|
return prometheusHistogram{c: c.c, labels: m.mapLabels(labels...)}
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2021-01-19 16:51:58 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2022-01-23 16:13:58 +03:00
|
|
|
nm := m.buildName(name)
|
2021-11-03 11:21:54 +03:00
|
|
|
c, ok := m.summary[nm]
|
|
|
|
if !ok {
|
2022-03-05 19:09:31 +03:00
|
|
|
nc := prometheus.NewSummaryVec(prometheus.SummaryOpts{Name: nm}, m.labelNames(c.labels, labels))
|
|
|
|
c = prometheusSummary{c: nc}
|
|
|
|
m.summary[nm] = c
|
|
|
|
} else if !m.metricEqual(c.lnames, labels) {
|
|
|
|
m.set.Unregister(c.c)
|
|
|
|
nc := prometheus.NewSummaryVec(prometheus.SummaryOpts{Name: nm}, m.labelNames(c.labels, labels))
|
2021-11-03 11:21:54 +03:00
|
|
|
c = prometheusSummary{c: nc}
|
|
|
|
m.summary[nm] = c
|
2022-03-05 19:09:31 +03:00
|
|
|
m.set.MustRegister(c.c)
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
|
|
|
|
2022-03-05 19:09:31 +03:00
|
|
|
return prometheusSummary{c: c.c, labels: m.mapLabels(labels...)}
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2022-01-23 16:13:58 +03:00
|
|
|
nm := m.buildName(name)
|
2021-11-03 11:21:54 +03:00
|
|
|
c, ok := m.summary[nm]
|
|
|
|
if !ok {
|
2022-03-05 19:09:31 +03:00
|
|
|
nc := prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
|
|
|
Name: nm,
|
|
|
|
MaxAge: window,
|
|
|
|
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
|
|
|
|
}, m.labelNames(c.labels, labels))
|
|
|
|
c = prometheusSummary{c: nc}
|
|
|
|
m.summary[nm] = c
|
|
|
|
} else if !m.metricEqual(c.lnames, labels) {
|
|
|
|
m.set.Unregister(c.c)
|
|
|
|
nc := prometheus.NewSummaryVec(prometheus.SummaryOpts{Name: nm}, m.labelNames(c.labels, labels))
|
2021-11-03 11:21:54 +03:00
|
|
|
c = prometheusSummary{c: nc}
|
|
|
|
m.summary[nm] = c
|
2022-03-05 19:09:31 +03:00
|
|
|
m.set.MustRegister(c.c)
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2022-03-05 19:09:31 +03:00
|
|
|
return prometheusSummary{c: c.c, labels: m.mapLabels(labels...)}
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) Init(opts ...meter.Option) error {
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&m.opts)
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
return nil
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
|
|
|
|
options := m.opts
|
2021-01-19 16:51:58 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
if options.WriteProcessMetrics || options.WriteFDMetrics {
|
|
|
|
c := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})
|
|
|
|
_ = m.set.Register(c)
|
|
|
|
}
|
2021-01-19 16:51:58 +03:00
|
|
|
|
2021-11-03 17:50:29 +03:00
|
|
|
g, ok := m.set.(prometheus.Gatherer)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("set type %T not prometheus.Gatherer", m.set)
|
|
|
|
}
|
|
|
|
|
|
|
|
mfs, err := g.Gather()
|
2021-11-03 11:21:54 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := expfmt.NewEncoder(w, expfmt.FmtText)
|
|
|
|
for _, mf := range mfs {
|
|
|
|
_ = enc.Encode(mf)
|
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
if closer, ok := enc.(io.Closer); ok {
|
|
|
|
_ = closer.Close()
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
2021-11-03 11:21:54 +03:00
|
|
|
|
|
|
|
return nil
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) Clone(opts ...meter.Option) meter.Meter {
|
|
|
|
options := m.opts
|
2021-01-19 16:51:58 +03:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
return &prometheusMeter{
|
|
|
|
set: m.set,
|
|
|
|
opts: options,
|
|
|
|
counter: m.counter,
|
|
|
|
gauge: m.gauge,
|
|
|
|
histogram: m.histogram,
|
|
|
|
summary: m.summary,
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) Options() meter.Options {
|
|
|
|
return m.opts
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) String() string {
|
|
|
|
return "prometheus"
|
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (m *prometheusMeter) Set(opts ...meter.Option) meter.Meter {
|
|
|
|
nm := &prometheusMeter{opts: m.opts}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&nm.opts)
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
2021-11-03 11:21:54 +03:00
|
|
|
nm.set = prometheus.NewRegistry()
|
|
|
|
return nm
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
type prometheusCounter struct {
|
2022-03-05 19:09:31 +03:00
|
|
|
c *prometheus.GaugeVec
|
|
|
|
lnames []string
|
|
|
|
labels prometheus.Labels
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusCounter) Add(n int) {
|
2022-03-05 19:09:31 +03:00
|
|
|
nc, _ := c.c.GetMetricWith(c.labels)
|
|
|
|
nc.Add(float64(n))
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusCounter) Dec() {
|
2022-03-05 19:09:31 +03:00
|
|
|
c.c.With(c.labels).Dec()
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusCounter) Inc() {
|
2022-03-05 19:09:31 +03:00
|
|
|
c.c.With(c.labels).Inc()
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusCounter) Get() uint64 {
|
|
|
|
m := &dto.Metric{}
|
2022-03-05 19:09:31 +03:00
|
|
|
if err := c.c.With(c.labels).Write(m); err != nil {
|
2021-11-03 11:21:54 +03:00
|
|
|
return 0
|
2019-01-26 12:33:51 +03:00
|
|
|
}
|
2021-11-03 11:21:54 +03:00
|
|
|
return uint64(m.GetGauge().GetValue())
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusCounter) Set(n uint64) {
|
2022-03-05 19:09:31 +03:00
|
|
|
c.c.With(c.labels).Set(float64(n))
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
type prometheusFloatCounter struct {
|
2022-03-05 19:09:31 +03:00
|
|
|
c *prometheus.GaugeVec
|
|
|
|
lnames []string
|
|
|
|
labels prometheus.Labels
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusFloatCounter) Add(n float64) {
|
2022-03-05 19:09:31 +03:00
|
|
|
c.c.With(c.labels).Add(n)
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusFloatCounter) Get() float64 {
|
2021-11-03 17:50:29 +03:00
|
|
|
m := &dto.Metric{}
|
2022-03-05 19:09:31 +03:00
|
|
|
if err := c.c.With(c.labels).Write(m); err != nil {
|
2021-11-03 17:50:29 +03:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return m.GetGauge().GetValue()
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusFloatCounter) Set(n float64) {
|
2022-03-05 19:09:31 +03:00
|
|
|
c.c.With(c.labels).Set(n)
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusFloatCounter) Sub(n float64) {
|
2022-03-05 19:09:31 +03:00
|
|
|
c.c.With(c.labels).Add(-n)
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
type prometheusGauge struct {
|
2022-03-05 19:09:31 +03:00
|
|
|
c *prometheus.GaugeVec
|
|
|
|
lnames []string
|
|
|
|
labels prometheus.Labels
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
2019-02-04 01:26:31 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusGauge) Get() float64 {
|
2021-11-03 17:50:29 +03:00
|
|
|
m := &dto.Metric{}
|
2022-03-05 19:09:31 +03:00
|
|
|
if err := c.c.With(c.labels).Write(m); err != nil {
|
2021-11-03 17:50:29 +03:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return float64(m.GetGauge().GetValue())
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
type prometheusHistogram struct {
|
2022-03-05 19:09:31 +03:00
|
|
|
c *prometheus.HistogramVec
|
|
|
|
lnames []string
|
|
|
|
labels prometheus.Labels
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2021-01-19 16:51:58 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusHistogram) Reset() {
|
|
|
|
}
|
2019-01-25 17:27:27 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusHistogram) Update(n float64) {
|
2022-03-05 19:09:31 +03:00
|
|
|
c.c.With(c.labels).Observe(n)
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusHistogram) UpdateDuration(n time.Time) {
|
2022-03-05 19:09:31 +03:00
|
|
|
c.c.With(c.labels).Observe(time.Since(n).Seconds())
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
type prometheusSummary struct {
|
2022-03-05 19:09:31 +03:00
|
|
|
c *prometheus.SummaryVec
|
|
|
|
lnames []string
|
|
|
|
labels prometheus.Labels
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusSummary) Update(n float64) {
|
2022-03-05 19:09:31 +03:00
|
|
|
c.c.With(c.labels).Observe(n)
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
func (c prometheusSummary) UpdateDuration(n time.Time) {
|
2022-03-05 19:09:31 +03:00
|
|
|
c.c.With(c.labels).Observe(time.Since(n).Seconds())
|
2019-01-25 17:27:27 +03:00
|
|
|
}
|