2019-01-25 17:27:27 +03:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
import (
|
2021-11-03 17:50:29 +03:00
|
|
|
"fmt"
|
2022-03-11 01:24:14 +03:00
|
|
|
"hash/fnv"
|
2021-11-03 11:21:54 +03:00
|
|
|
"io"
|
2024-04-04 16:36:48 +03:00
|
|
|
"regexp"
|
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
|
|
|
)
|
|
|
|
|
2024-09-20 18:50:08 +03:00
|
|
|
var _ meter.Meter = (*prometheusMeter)(nil)
|
2022-03-11 01:24:14 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
type prometheusMeter struct {
|
|
|
|
opts meter.Options
|
|
|
|
set prometheus.Registerer
|
2024-04-15 00:27:15 +03:00
|
|
|
counter *sync.Map
|
|
|
|
floatCounter *sync.Map
|
|
|
|
gauge *sync.Map
|
|
|
|
histogram *sync.Map
|
|
|
|
summary *sync.Map
|
2021-11-03 11:21:54 +03:00
|
|
|
sync.Mutex
|
|
|
|
}
|
2021-01-19 16:51:58 +03:00
|
|
|
|
2022-03-11 01:24:14 +03:00
|
|
|
type counters struct {
|
2024-04-15 00:27:15 +03:00
|
|
|
cs *sync.Map
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type gauges struct {
|
2024-04-15 00:27:15 +03:00
|
|
|
cs *sync.Map
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type histograms struct {
|
2024-04-15 00:27:15 +03:00
|
|
|
cs *sync.Map
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type summaries struct {
|
2024-04-15 00:27:15 +03:00
|
|
|
cs *sync.Map
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type floatCounters struct {
|
2024-04-15 00:27:15 +03:00
|
|
|
cs *sync.Map
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewMeter(opts ...meter.Option) *prometheusMeter {
|
2021-11-03 11:21:54 +03:00
|
|
|
return &prometheusMeter{
|
2022-03-11 01:24:14 +03:00
|
|
|
set: prometheus.NewRegistry(), // prometheus.DefaultRegisterer,
|
2021-11-03 11:21:54 +03:00
|
|
|
opts: meter.NewOptions(opts...),
|
2024-04-15 00:27:15 +03:00
|
|
|
counter: &sync.Map{},
|
|
|
|
floatCounter: &sync.Map{},
|
|
|
|
gauge: &sync.Map{},
|
|
|
|
histogram: &sync.Map{},
|
|
|
|
summary: &sync.Map{},
|
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) Name() string {
|
|
|
|
return m.opts.Name
|
|
|
|
}
|
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()
|
2024-09-20 18:50:08 +03:00
|
|
|
labels = append(m.opts.Labels, labels...)
|
|
|
|
vcd, ok := m.counter.Load(name)
|
2022-03-11 01:24:14 +03:00
|
|
|
h := newHash(labels)
|
|
|
|
if !ok {
|
2024-04-15 00:34:55 +03:00
|
|
|
cd := &counters{cs: &sync.Map{}}
|
2024-09-20 18:50:08 +03:00
|
|
|
c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
2024-04-15 00:27:15 +03:00
|
|
|
cd.cs.Store(h, c)
|
2024-09-20 18:50:08 +03:00
|
|
|
m.counter.Store(name, cd)
|
2022-03-11 01:24:14 +03:00
|
|
|
return c
|
|
|
|
}
|
2024-04-15 00:34:55 +03:00
|
|
|
cd := vcd.(*counters)
|
2024-04-15 00:27:15 +03:00
|
|
|
vc, ok := cd.cs.Load(h)
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2024-09-20 18:50:08 +03:00
|
|
|
c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
2024-04-15 00:27:15 +03:00
|
|
|
cd.cs.Store(h, c)
|
2024-09-20 18:50:08 +03:00
|
|
|
m.counter.Store(name, cd)
|
2024-04-15 00:34:55 +03:00
|
|
|
return c
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-04-15 00:34:55 +03:00
|
|
|
c := vc.(*prometheusCounter)
|
2022-03-11 01:24:14 +03:00
|
|
|
return c
|
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()
|
2024-09-20 18:50:08 +03:00
|
|
|
labels = append(m.opts.Labels, labels...)
|
|
|
|
vcd, ok := m.floatCounter.Load(name)
|
2022-03-11 01:24:14 +03:00
|
|
|
h := newHash(labels)
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2024-04-15 00:34:55 +03:00
|
|
|
cd := &floatCounters{cs: &sync.Map{}}
|
2024-09-20 18:50:08 +03:00
|
|
|
c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
2024-04-15 00:27:15 +03:00
|
|
|
cd.cs.Store(h, c)
|
2024-09-20 18:50:08 +03:00
|
|
|
m.floatCounter.Store(name, cd)
|
2022-03-11 01:24:14 +03:00
|
|
|
return c
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
2024-04-15 00:34:55 +03:00
|
|
|
cd := vcd.(*floatCounters)
|
2024-04-15 00:27:15 +03:00
|
|
|
vc, ok := cd.cs.Load(h)
|
2022-03-11 01:24:14 +03:00
|
|
|
if !ok {
|
2024-09-20 18:50:08 +03:00
|
|
|
c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
2024-04-15 00:27:15 +03:00
|
|
|
cd.cs.Store(h, c)
|
2024-09-20 18:50:08 +03:00
|
|
|
m.floatCounter.Store(name, cd)
|
2024-04-15 00:34:55 +03:00
|
|
|
return c
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-04-15 00:34:55 +03:00
|
|
|
c := vc.(*prometheusFloatCounter)
|
2022-03-11 01:24:14 +03:00
|
|
|
return c
|
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()
|
2024-09-20 18:50:08 +03:00
|
|
|
labels = append(m.opts.Labels, labels...)
|
|
|
|
vcd, ok := m.gauge.Load(name)
|
2022-03-11 01:24:14 +03:00
|
|
|
h := newHash(labels)
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2024-04-15 00:34:55 +03:00
|
|
|
cd := &gauges{cs: &sync.Map{}}
|
2024-09-20 18:50:08 +03:00
|
|
|
c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
2024-04-15 00:27:15 +03:00
|
|
|
cd.cs.Store(h, c)
|
2024-09-20 18:50:08 +03:00
|
|
|
m.gauge.Store(name, cd)
|
2022-03-11 01:24:14 +03:00
|
|
|
return c
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
2024-04-15 00:34:55 +03:00
|
|
|
cd := vcd.(*gauges)
|
2024-04-15 00:27:15 +03:00
|
|
|
vc, ok := cd.cs.Load(h)
|
2022-03-11 01:24:14 +03:00
|
|
|
if !ok {
|
2024-09-20 18:50:08 +03:00
|
|
|
c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
2024-04-15 00:27:15 +03:00
|
|
|
cd.cs.Store(h, c)
|
2024-09-20 18:50:08 +03:00
|
|
|
m.gauge.Store(name, cd)
|
2024-04-15 00:34:55 +03:00
|
|
|
return c
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-04-15 00:34:55 +03:00
|
|
|
c := vc.(*prometheusGauge)
|
2022-03-11 01:24:14 +03:00
|
|
|
return c
|
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()
|
2024-09-20 18:50:08 +03:00
|
|
|
labels = append(m.opts.Labels, labels...)
|
|
|
|
vcd, ok := m.histogram.Load(name)
|
2022-03-11 01:24:14 +03:00
|
|
|
h := newHash(labels)
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2024-04-15 00:34:55 +03:00
|
|
|
cd := &histograms{cs: &sync.Map{}}
|
2024-09-20 18:50:08 +03:00
|
|
|
c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: name}), labels: labels}
|
2024-04-15 00:27:15 +03:00
|
|
|
cd.cs.Store(h, c)
|
2024-09-20 18:50:08 +03:00
|
|
|
m.histogram.Store(name, cd)
|
2022-03-11 01:24:14 +03:00
|
|
|
return c
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
2024-04-15 00:34:55 +03:00
|
|
|
cd := vcd.(*histograms)
|
2024-04-15 00:27:15 +03:00
|
|
|
vc, ok := cd.cs.Load(h)
|
2022-03-11 01:24:14 +03:00
|
|
|
if !ok {
|
2024-09-20 18:50:08 +03:00
|
|
|
c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: name}), labels: labels}
|
2024-04-15 00:27:15 +03:00
|
|
|
cd.cs.Store(h, c)
|
2024-09-20 18:50:08 +03:00
|
|
|
m.histogram.Store(name, cd)
|
2024-04-15 00:34:55 +03:00
|
|
|
return c
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-04-15 00:34:55 +03:00
|
|
|
c := vc.(*prometheusHistogram)
|
2022-03-11 01:24:14 +03:00
|
|
|
return c
|
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()
|
2024-09-20 18:50:08 +03:00
|
|
|
labels = append(m.opts.Labels, labels...)
|
|
|
|
vcd, ok := m.summary.Load(name)
|
2022-03-11 01:24:14 +03:00
|
|
|
h := newHash(labels)
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2024-04-15 00:34:55 +03:00
|
|
|
cd := &summaries{cs: &sync.Map{}}
|
2024-09-20 18:50:08 +03:00
|
|
|
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: name}), labels: labels}
|
2024-04-15 00:27:15 +03:00
|
|
|
cd.cs.Store(h, c)
|
2024-09-20 18:50:08 +03:00
|
|
|
m.summary.Store(name, cd)
|
2022-03-11 01:24:14 +03:00
|
|
|
return c
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
2024-04-15 00:34:55 +03:00
|
|
|
cd := vcd.(*summaries)
|
2024-04-15 00:27:15 +03:00
|
|
|
vc, ok := cd.cs.Load(h)
|
2022-03-11 01:24:14 +03:00
|
|
|
if !ok {
|
2024-09-20 18:50:08 +03:00
|
|
|
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: name}), labels: labels}
|
2024-04-15 00:27:15 +03:00
|
|
|
cd.cs.Store(h, c)
|
2024-09-20 18:50:08 +03:00
|
|
|
m.summary.Store(name, cd)
|
2024-04-15 00:34:55 +03:00
|
|
|
return c
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-04-15 00:34:55 +03:00
|
|
|
c := vc.(*prometheusSummary)
|
2022-03-11 01:24:14 +03:00
|
|
|
return c
|
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()
|
2024-09-20 18:50:08 +03:00
|
|
|
labels = append(m.opts.Labels, labels...)
|
|
|
|
vcd, ok := m.summary.Load(name)
|
2022-03-11 01:24:14 +03:00
|
|
|
h := newHash(labels)
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2024-04-15 00:34:55 +03:00
|
|
|
cd := &summaries{cs: &sync.Map{}}
|
2022-03-11 01:24:14 +03:00
|
|
|
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
|
2024-09-20 18:50:08 +03:00
|
|
|
Name: name,
|
2022-03-05 19:09:31 +03:00
|
|
|
MaxAge: window,
|
|
|
|
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
|
2022-03-11 01:24:14 +03:00
|
|
|
}), labels: labels}
|
2024-04-15 00:27:15 +03:00
|
|
|
cd.cs.Store(h, c)
|
2024-09-20 18:50:08 +03:00
|
|
|
m.summary.Store(name, cd)
|
2022-03-11 01:24:14 +03:00
|
|
|
return c
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2024-04-15 00:34:55 +03:00
|
|
|
cd := vcd.(*summaries)
|
2024-04-15 00:27:15 +03:00
|
|
|
vc, ok := cd.cs.Load(h)
|
2022-03-11 01:24:14 +03:00
|
|
|
if !ok {
|
2024-04-15 00:34:55 +03:00
|
|
|
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
|
2024-09-20 18:50:08 +03:00
|
|
|
Name: name,
|
2022-03-11 01:24:14 +03:00
|
|
|
MaxAge: window,
|
|
|
|
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
|
|
|
|
}), labels: labels}
|
2024-04-15 00:27:15 +03:00
|
|
|
cd.cs.Store(h, c)
|
2024-09-20 18:50:08 +03:00
|
|
|
m.summary.Store(name, cd)
|
2024-04-15 00:34:55 +03:00
|
|
|
return c
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-04-15 00:34:55 +03:00
|
|
|
c := vc.(*prometheusSummary)
|
2022-03-11 01:24:14 +03:00
|
|
|
return c
|
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
|
|
|
}
|
2024-04-15 08:00:38 +03:00
|
|
|
|
|
|
|
if m.opts.WriteProcessMetrics || m.opts.WriteFDMetrics {
|
|
|
|
pc := collectors.NewProcessCollector(collectors.ProcessCollectorOpts{})
|
|
|
|
_ = m.set.Register(pc)
|
|
|
|
gc := collectors.NewGoCollector(collectors.WithGoCollectorRuntimeMetrics(collectors.GoRuntimeMetricsRule{Matcher: regexp.MustCompile("/.*")}))
|
|
|
|
_ = m.set.Register(gc)
|
|
|
|
}
|
|
|
|
|
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 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
|
|
|
|
}
|
|
|
|
|
2024-03-27 11:49:31 +03:00
|
|
|
enc := expfmt.NewEncoder(w, expfmt.NewFormat(expfmt.TypeTextPlain))
|
2022-03-11 01:24:14 +03:00
|
|
|
|
2024-04-15 00:27:15 +03:00
|
|
|
m.counter.Range(func(k, v any) bool {
|
|
|
|
name := k.(string)
|
2022-03-11 01:24:14 +03:00
|
|
|
mf := &dto.MetricFamily{
|
2024-09-20 18:50:08 +03:00
|
|
|
Name: &name,
|
2024-04-15 00:27:15 +03:00
|
|
|
Type: dto.MetricType_GAUGE.Enum(),
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-04-15 00:27:15 +03:00
|
|
|
v.(*counters).cs.Range(func(_, nv any) bool {
|
|
|
|
c := nv.(*prometheusCounter)
|
2022-03-13 11:24:11 +03:00
|
|
|
m := &dto.Metric{}
|
2022-03-13 11:18:38 +03:00
|
|
|
_ = c.c.Write(m)
|
2022-03-13 11:24:11 +03:00
|
|
|
fillMetric(m, c.labels)
|
2022-03-11 01:24:14 +03:00
|
|
|
mf.Metric = append(mf.Metric, m)
|
2024-04-15 00:27:15 +03:00
|
|
|
return true
|
|
|
|
})
|
2022-03-11 01:24:14 +03:00
|
|
|
mfs = append(mfs, mf)
|
2024-04-15 00:27:15 +03:00
|
|
|
return true
|
|
|
|
})
|
2022-03-11 01:24:14 +03:00
|
|
|
|
2024-04-15 00:27:15 +03:00
|
|
|
m.gauge.Range(func(k, v any) bool {
|
|
|
|
name := k.(string)
|
2022-03-11 01:24:14 +03:00
|
|
|
mf := &dto.MetricFamily{
|
2024-09-20 18:50:08 +03:00
|
|
|
Name: &name,
|
2024-04-15 00:27:15 +03:00
|
|
|
Type: dto.MetricType_GAUGE.Enum(),
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-04-15 00:27:15 +03:00
|
|
|
v.(*gauges).cs.Range(func(_, nv any) bool {
|
|
|
|
c := nv.(*prometheusGauge)
|
2022-03-13 11:24:11 +03:00
|
|
|
m := &dto.Metric{}
|
2022-03-13 11:18:38 +03:00
|
|
|
_ = c.c.Write(m)
|
2022-03-13 11:24:11 +03:00
|
|
|
fillMetric(m, c.labels)
|
2022-03-11 01:37:53 +03:00
|
|
|
mf.Metric = append(mf.Metric, m)
|
2024-04-15 00:27:15 +03:00
|
|
|
return true
|
|
|
|
})
|
2022-03-11 01:37:53 +03:00
|
|
|
mfs = append(mfs, mf)
|
2024-04-15 00:27:15 +03:00
|
|
|
return true
|
|
|
|
})
|
2022-03-11 01:37:53 +03:00
|
|
|
|
2024-04-15 00:27:15 +03:00
|
|
|
m.floatCounter.Range(func(k, v any) bool {
|
|
|
|
name := k.(string)
|
2022-03-11 01:37:53 +03:00
|
|
|
mf := &dto.MetricFamily{
|
2024-09-20 18:50:08 +03:00
|
|
|
Name: &name,
|
2024-04-15 00:27:15 +03:00
|
|
|
Type: dto.MetricType_GAUGE.Enum(),
|
2022-03-11 01:37:53 +03:00
|
|
|
}
|
2024-04-15 00:27:15 +03:00
|
|
|
v.(*floatCounters).cs.Range(func(_, nv any) bool {
|
2024-04-15 00:49:09 +03:00
|
|
|
c := nv.(*prometheusFloatCounter)
|
2022-03-13 11:24:11 +03:00
|
|
|
m := &dto.Metric{}
|
2022-03-13 11:18:38 +03:00
|
|
|
_ = c.c.Write(m)
|
2022-03-13 11:24:11 +03:00
|
|
|
fillMetric(m, c.labels)
|
2022-03-11 01:37:53 +03:00
|
|
|
mf.Metric = append(mf.Metric, m)
|
2024-04-15 00:27:15 +03:00
|
|
|
return true
|
|
|
|
})
|
2022-03-11 01:37:53 +03:00
|
|
|
mfs = append(mfs, mf)
|
2024-04-15 00:27:15 +03:00
|
|
|
return true
|
|
|
|
})
|
2022-03-11 01:37:53 +03:00
|
|
|
|
2024-04-15 00:27:15 +03:00
|
|
|
m.histogram.Range(func(k, v any) bool {
|
|
|
|
name := k.(string)
|
2022-03-11 01:37:53 +03:00
|
|
|
mf := &dto.MetricFamily{
|
2024-09-20 18:50:08 +03:00
|
|
|
Name: &name,
|
2024-04-15 00:27:15 +03:00
|
|
|
Type: dto.MetricType_HISTOGRAM.Enum(),
|
2022-03-11 01:37:53 +03:00
|
|
|
}
|
2024-04-15 00:27:15 +03:00
|
|
|
v.(*histograms).cs.Range(func(_, nv any) bool {
|
2024-04-15 00:34:55 +03:00
|
|
|
c := nv.(*prometheusHistogram)
|
2022-03-13 11:24:11 +03:00
|
|
|
m := &dto.Metric{}
|
2022-03-13 11:18:38 +03:00
|
|
|
_ = c.c.Write(m)
|
2022-03-13 11:24:11 +03:00
|
|
|
fillMetric(m, c.labels)
|
2022-03-11 01:37:53 +03:00
|
|
|
mf.Metric = append(mf.Metric, m)
|
2024-04-15 00:27:15 +03:00
|
|
|
return true
|
|
|
|
})
|
2022-03-11 01:37:53 +03:00
|
|
|
mfs = append(mfs, mf)
|
2024-04-15 00:27:15 +03:00
|
|
|
return true
|
|
|
|
})
|
2022-03-11 01:37:53 +03:00
|
|
|
|
2024-04-15 00:27:15 +03:00
|
|
|
m.summary.Range(func(k, v any) bool {
|
|
|
|
name := k.(string)
|
2022-03-11 01:37:53 +03:00
|
|
|
mf := &dto.MetricFamily{
|
2024-09-20 18:50:08 +03:00
|
|
|
Name: &name,
|
2024-04-15 00:27:15 +03:00
|
|
|
Type: dto.MetricType_SUMMARY.Enum(),
|
2022-03-11 01:37:53 +03:00
|
|
|
}
|
2024-04-15 00:27:15 +03:00
|
|
|
v.(*summaries).cs.Range(func(_, nv any) bool {
|
2024-04-15 00:34:55 +03:00
|
|
|
c := nv.(*prometheusSummary)
|
2022-03-13 11:24:11 +03:00
|
|
|
m := &dto.Metric{}
|
2022-03-13 11:18:38 +03:00
|
|
|
_ = c.c.Write(m)
|
2022-03-13 11:24:11 +03:00
|
|
|
fillMetric(m, c.labels)
|
2022-03-11 01:24:14 +03:00
|
|
|
mf.Metric = append(mf.Metric, m)
|
2024-04-15 00:27:15 +03:00
|
|
|
return true
|
|
|
|
})
|
2022-03-11 01:24:14 +03:00
|
|
|
mfs = append(mfs, mf)
|
2024-04-15 00:27:15 +03:00
|
|
|
return true
|
|
|
|
})
|
2022-03-11 01:24:14 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
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{
|
2023-04-13 20:01:15 +03:00
|
|
|
set: m.set,
|
|
|
|
opts: options,
|
|
|
|
floatCounter: m.floatCounter,
|
|
|
|
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-11 01:24:14 +03:00
|
|
|
c prometheus.Gauge
|
|
|
|
labels []string
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2022-03-11 01:24:14 +03:00
|
|
|
func (c *prometheusCounter) Add(n int) {
|
|
|
|
c.c.Add(float64(n))
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2022-03-11 01:24:14 +03:00
|
|
|
func (c *prometheusCounter) Dec() {
|
|
|
|
c.c.Dec()
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2022-03-11 01:24:14 +03:00
|
|
|
func (c *prometheusCounter) Inc() {
|
|
|
|
c.c.Inc()
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2022-03-11 01:24:14 +03:00
|
|
|
func (c *prometheusCounter) Get() uint64 {
|
2021-11-03 11:21:54 +03:00
|
|
|
m := &dto.Metric{}
|
2022-03-11 01:24:14 +03:00
|
|
|
if err := c.c.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
|
|
|
}
|
|
|
|
|
2022-03-11 01:24:14 +03:00
|
|
|
func (c *prometheusCounter) Set(n uint64) {
|
|
|
|
c.c.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-11 01:24:14 +03:00
|
|
|
c prometheus.Gauge
|
|
|
|
labels []string
|
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-11 01:24:14 +03:00
|
|
|
c.c.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-11 01:24:14 +03:00
|
|
|
if err := c.c.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-11 01:24:14 +03:00
|
|
|
c.c.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-11 01:24:14 +03:00
|
|
|
c.c.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-11 01:24:14 +03:00
|
|
|
c prometheus.Gauge
|
|
|
|
labels []string
|
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-11 01:24:14 +03:00
|
|
|
if err := c.c.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-11 01:24:14 +03:00
|
|
|
c prometheus.Histogram
|
|
|
|
labels []string
|
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-11 01:24:14 +03:00
|
|
|
c.c.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-11 01:24:14 +03:00
|
|
|
c.c.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-11 01:24:14 +03:00
|
|
|
c prometheus.Summary
|
|
|
|
labels []string
|
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-11 01:24:14 +03:00
|
|
|
c.c.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-11 01:24:14 +03:00
|
|
|
c.c.Observe(time.Since(n).Seconds())
|
|
|
|
}
|
|
|
|
|
|
|
|
func newHash(labels []string) uint64 {
|
|
|
|
labels = meter.BuildLabels(labels...)
|
|
|
|
h := fnv.New64a()
|
|
|
|
for _, l := range labels {
|
|
|
|
h.Write([]byte(l))
|
|
|
|
}
|
|
|
|
return h.Sum64()
|
2019-01-25 17:27:27 +03:00
|
|
|
}
|
2022-03-13 11:18:38 +03:00
|
|
|
|
2022-03-13 11:24:11 +03:00
|
|
|
func fillMetric(m *dto.Metric, labels []string) *dto.Metric {
|
2024-03-27 11:49:31 +03:00
|
|
|
var ok bool
|
|
|
|
seen := make(map[string]bool, len(labels)/2)
|
2022-03-13 11:24:11 +03:00
|
|
|
m.Label = make([]*dto.LabelPair, 0, len(labels)/2)
|
2022-03-13 11:18:38 +03:00
|
|
|
for idx := 0; idx < len(labels); idx += 2 {
|
2024-03-27 11:49:31 +03:00
|
|
|
if _, ok = seen[labels[idx]]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-03-13 11:18:38 +03:00
|
|
|
m.Label = append(m.Label, &dto.LabelPair{
|
2024-09-20 18:50:08 +03:00
|
|
|
Name: &(labels[idx]),
|
|
|
|
Value: &(labels[idx+1]),
|
2022-03-13 11:18:38 +03:00
|
|
|
})
|
2024-03-27 11:49:31 +03:00
|
|
|
seen[labels[idx]] = true
|
2022-03-13 11:18:38 +03:00
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|