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"
|
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"
|
2023-05-07 19:35:39 +03:00
|
|
|
"go.unistack.org/micro/v4/meter"
|
2023-08-16 15:41:33 +03:00
|
|
|
"go.unistack.org/micro/v4/options"
|
2019-01-25 17:27:27 +03:00
|
|
|
)
|
|
|
|
|
2022-03-11 01:24:14 +03:00
|
|
|
var _ meter.Meter = &prometheusMeter{}
|
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
type prometheusMeter struct {
|
|
|
|
opts meter.Options
|
|
|
|
set prometheus.Registerer
|
2022-03-11 01:24:14 +03:00
|
|
|
counter map[string]*counters
|
|
|
|
floatCounter map[string]*floatCounters
|
|
|
|
gauge map[string]*gauges
|
|
|
|
histogram map[string]*histograms
|
|
|
|
summary map[string]*summaries
|
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 {
|
|
|
|
cs map[uint64]*prometheusCounter
|
|
|
|
}
|
|
|
|
|
|
|
|
type gauges struct {
|
|
|
|
cs map[uint64]*prometheusGauge
|
|
|
|
}
|
|
|
|
|
|
|
|
type histograms struct {
|
|
|
|
cs map[uint64]*prometheusHistogram
|
|
|
|
}
|
|
|
|
|
|
|
|
type summaries struct {
|
|
|
|
cs map[uint64]*prometheusSummary
|
|
|
|
}
|
|
|
|
|
|
|
|
type floatCounters struct {
|
|
|
|
cs map[uint64]*prometheusFloatCounter
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFloat64(v float64) *float64 {
|
|
|
|
nv := v
|
|
|
|
return &nv
|
|
|
|
}
|
|
|
|
|
|
|
|
func newString(v string) *string {
|
|
|
|
nv := v
|
|
|
|
return &nv
|
|
|
|
}
|
|
|
|
|
2023-08-16 15:41:33 +03:00
|
|
|
func NewMeter(opts ...options.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...),
|
2022-03-11 01:24:14 +03:00
|
|
|
counter: make(map[string]*counters),
|
|
|
|
floatCounter: make(map[string]*floatCounters),
|
|
|
|
gauge: make(map[string]*gauges),
|
|
|
|
histogram: make(map[string]*histograms),
|
|
|
|
summary: make(map[string]*summaries),
|
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) 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
|
|
|
|
2022-03-13 11:18:38 +03:00
|
|
|
for idx := 0; idx < nl; idx += 2 {
|
2021-11-03 11:21:54 +03:00
|
|
|
nlabels[idx] = m.opts.LabelPrefix + nlabels[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 {
|
2022-03-11 01:24:14 +03:00
|
|
|
nl := len(labels)
|
2021-11-03 11:21:54 +03:00
|
|
|
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)
|
|
|
|
|
2022-03-13 11:18:38 +03:00
|
|
|
for idx := 0; idx < nl; idx += 2 {
|
2022-03-11 01:24:14 +03:00
|
|
|
nlabels = append(nlabels, m.opts.LabelPrefix+labels[idx])
|
|
|
|
nlabels = append(nlabels, labels[idx+1])
|
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
|
|
|
|
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)
|
2022-03-11 01:24:14 +03:00
|
|
|
labels = m.buildLabels(append(m.opts.Labels, labels...)...)
|
|
|
|
cd, ok := m.counter[nm]
|
|
|
|
h := newHash(labels)
|
|
|
|
if !ok {
|
|
|
|
cd = &counters{cs: make(map[uint64]*prometheusCounter)}
|
|
|
|
c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
|
|
|
cd.cs[h] = c
|
|
|
|
m.counter[nm] = cd
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
c, ok := cd.cs[h]
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2022-03-11 01:24:14 +03:00
|
|
|
c = &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
|
|
|
cd.cs[h] = c
|
|
|
|
m.counter[nm] = cd
|
|
|
|
}
|
|
|
|
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()
|
2022-01-23 16:13:58 +03:00
|
|
|
nm := m.buildName(name)
|
2022-03-11 01:24:14 +03:00
|
|
|
labels = m.buildLabels(append(m.opts.Labels, labels...)...)
|
|
|
|
cd, ok := m.floatCounter[nm]
|
|
|
|
h := newHash(labels)
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2022-03-11 01:24:14 +03:00
|
|
|
cd = &floatCounters{cs: make(map[uint64]*prometheusFloatCounter)}
|
|
|
|
c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
|
|
|
cd.cs[h] = c
|
|
|
|
m.floatCounter[nm] = cd
|
|
|
|
return c
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
2022-03-11 01:24:14 +03:00
|
|
|
c, ok := cd.cs[h]
|
|
|
|
if !ok {
|
|
|
|
c = &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
|
|
|
cd.cs[h] = c
|
|
|
|
m.floatCounter[nm] = cd
|
|
|
|
}
|
|
|
|
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()
|
2022-01-23 16:13:58 +03:00
|
|
|
nm := m.buildName(name)
|
2022-03-11 01:24:14 +03:00
|
|
|
labels = m.buildLabels(append(m.opts.Labels, labels...)...)
|
|
|
|
cd, ok := m.gauge[nm]
|
|
|
|
h := newHash(labels)
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2022-03-11 01:24:14 +03:00
|
|
|
cd = &gauges{cs: make(map[uint64]*prometheusGauge)}
|
|
|
|
c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
|
|
|
cd.cs[h] = c
|
|
|
|
m.gauge[nm] = cd
|
|
|
|
return c
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
2022-03-11 01:24:14 +03:00
|
|
|
c, ok := cd.cs[h]
|
|
|
|
if !ok {
|
|
|
|
c = &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
|
|
|
cd.cs[h] = c
|
|
|
|
m.gauge[nm] = cd
|
|
|
|
}
|
|
|
|
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()
|
2022-01-23 16:13:58 +03:00
|
|
|
nm := m.buildName(name)
|
2022-03-11 01:24:14 +03:00
|
|
|
labels = m.buildLabels(append(m.opts.Labels, labels...)...)
|
|
|
|
cd, ok := m.histogram[nm]
|
|
|
|
h := newHash(labels)
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2022-03-11 01:24:14 +03:00
|
|
|
cd = &histograms{cs: make(map[uint64]*prometheusHistogram)}
|
|
|
|
c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels}
|
|
|
|
cd.cs[h] = c
|
|
|
|
m.histogram[nm] = cd
|
|
|
|
return c
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
2022-03-11 01:24:14 +03:00
|
|
|
c, ok := cd.cs[h]
|
|
|
|
if !ok {
|
|
|
|
c = &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels}
|
|
|
|
cd.cs[h] = c
|
|
|
|
m.histogram[nm] = cd
|
|
|
|
}
|
|
|
|
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()
|
2022-01-23 16:13:58 +03:00
|
|
|
nm := m.buildName(name)
|
2022-03-11 01:24:14 +03:00
|
|
|
labels = m.buildLabels(append(m.opts.Labels, labels...)...)
|
|
|
|
cd, ok := m.summary[nm]
|
|
|
|
h := newHash(labels)
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2022-03-11 01:24:14 +03:00
|
|
|
cd = &summaries{cs: make(map[uint64]*prometheusSummary)}
|
|
|
|
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels}
|
|
|
|
cd.cs[h] = c
|
|
|
|
m.summary[nm] = cd
|
|
|
|
return c
|
2021-01-19 16:51:58 +03:00
|
|
|
}
|
2022-03-11 01:24:14 +03:00
|
|
|
c, ok := cd.cs[h]
|
|
|
|
if !ok {
|
|
|
|
c = &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels}
|
|
|
|
cd.cs[h] = c
|
|
|
|
m.summary[nm] = cd
|
|
|
|
}
|
|
|
|
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()
|
2022-01-23 16:13:58 +03:00
|
|
|
nm := m.buildName(name)
|
2022-03-11 01:24:14 +03:00
|
|
|
labels = m.buildLabels(append(m.opts.Labels, labels...)...)
|
|
|
|
cd, ok := m.summary[nm]
|
|
|
|
h := newHash(labels)
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2022-03-11 01:24:14 +03:00
|
|
|
cd = &summaries{cs: make(map[uint64]*prometheusSummary)}
|
|
|
|
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
|
2022-03-05 19:09:31 +03:00
|
|
|
Name: nm,
|
|
|
|
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}
|
|
|
|
cd.cs[h] = c
|
|
|
|
m.summary[nm] = cd
|
|
|
|
return c
|
2020-06-10 16:04:12 +03:00
|
|
|
}
|
2022-03-11 01:24:14 +03:00
|
|
|
c, ok := cd.cs[h]
|
|
|
|
if !ok {
|
|
|
|
c = &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
|
|
|
|
Name: nm,
|
|
|
|
MaxAge: window,
|
|
|
|
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
|
|
|
|
}), labels: labels}
|
|
|
|
cd.cs[h] = c
|
|
|
|
m.summary[nm] = cd
|
|
|
|
}
|
|
|
|
return c
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
2020-04-17 02:45:40 +03:00
|
|
|
|
2023-08-16 15:41:33 +03:00
|
|
|
func (m *prometheusMeter) Init(opts ...options.Option) error {
|
2021-11-03 11:21:54 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-08-16 15:41:33 +03:00
|
|
|
func (m *prometheusMeter) Write(w io.Writer, opts ...options.Option) error {
|
2021-11-03 11:21:54 +03:00
|
|
|
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)
|
2022-03-11 01:24:14 +03:00
|
|
|
|
|
|
|
for name, metrics := range m.counter {
|
|
|
|
mf := &dto.MetricFamily{
|
|
|
|
Name: newString(name),
|
|
|
|
Type: dto.MetricType_GAUGE.Enum(),
|
|
|
|
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
|
|
|
|
}
|
|
|
|
for _, c := range metrics.cs {
|
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)
|
|
|
|
}
|
|
|
|
mfs = append(mfs, mf)
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, metrics := range m.gauge {
|
|
|
|
mf := &dto.MetricFamily{
|
|
|
|
Name: newString(name),
|
|
|
|
Type: dto.MetricType_GAUGE.Enum(),
|
|
|
|
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
|
|
|
|
}
|
|
|
|
for _, c := range metrics.cs {
|
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)
|
|
|
|
}
|
|
|
|
mfs = append(mfs, mf)
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, metrics := range m.floatCounter {
|
|
|
|
mf := &dto.MetricFamily{
|
|
|
|
Name: newString(name),
|
|
|
|
Type: dto.MetricType_GAUGE.Enum(),
|
|
|
|
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
|
|
|
|
}
|
|
|
|
for _, c := range metrics.cs {
|
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)
|
|
|
|
}
|
|
|
|
mfs = append(mfs, mf)
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, metrics := range m.histogram {
|
|
|
|
mf := &dto.MetricFamily{
|
|
|
|
Name: newString(name),
|
|
|
|
Type: dto.MetricType_HISTOGRAM.Enum(),
|
|
|
|
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
|
|
|
|
}
|
|
|
|
for _, c := range metrics.cs {
|
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)
|
|
|
|
}
|
|
|
|
mfs = append(mfs, mf)
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, metrics := range m.summary {
|
|
|
|
mf := &dto.MetricFamily{
|
|
|
|
Name: newString(name),
|
|
|
|
Type: dto.MetricType_SUMMARY.Enum(),
|
|
|
|
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
|
|
|
|
}
|
|
|
|
for _, c := range metrics.cs {
|
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)
|
|
|
|
}
|
|
|
|
mfs = append(mfs, mf)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-08-16 15:41:33 +03:00
|
|
|
func (m *prometheusMeter) Clone(opts ...options.Option) meter.Meter {
|
2021-11-03 11:21:54 +03:00
|
|
|
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
|
|
|
|
2023-08-16 15:41:33 +03:00
|
|
|
func (m *prometheusMeter) Set(opts ...options.Option) meter.Meter {
|
2021-11-03 11:21:54 +03:00
|
|
|
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 {
|
|
|
|
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 {
|
|
|
|
m.Label = append(m.Label, &dto.LabelPair{
|
2022-03-13 11:24:11 +03:00
|
|
|
Name: newString(labels[idx]),
|
|
|
|
Value: newString(labels[idx+1]),
|
2022-03-13 11:18:38 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|