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"
|
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"
|
2024-10-06 13:45:33 +03:00
|
|
|
xpool "go.unistack.org/micro/v3/util/xpool"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
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-11-06 00:00:17 +03:00
|
|
|
counter map[uint64]*prometheusCounter
|
|
|
|
floatCounter map[uint64]*prometheusFloatCounter
|
|
|
|
gauge map[uint64]*prometheusGauge
|
|
|
|
histogram map[uint64]*prometheusHistogram
|
|
|
|
summary map[uint64]*prometheusSummary
|
2024-10-06 13:45:33 +03:00
|
|
|
mfPool xpool.Pool[*dto.MetricFamily]
|
2024-11-06 00:00:17 +03:00
|
|
|
mu sync.Mutex
|
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-11-06 00:00:17 +03:00
|
|
|
counter: make(map[uint64]*prometheusCounter),
|
|
|
|
floatCounter: make(map[uint64]*prometheusFloatCounter),
|
|
|
|
gauge: make(map[uint64]*prometheusGauge),
|
|
|
|
histogram: make(map[uint64]*prometheusHistogram),
|
|
|
|
summary: make(map[uint64]*prometheusSummary),
|
2024-10-06 13:45:33 +03:00
|
|
|
mfPool: xpool.NewPool[*dto.MetricFamily](func() *dto.MetricFamily {
|
|
|
|
return &dto.MetricFamily{}
|
|
|
|
}),
|
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 {
|
2024-10-06 13:45:33 +03:00
|
|
|
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
|
|
|
|
h := newHash(name, clabels)
|
2024-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
mc, ok := m.counter[h]
|
|
|
|
m.mu.Unlock()
|
2021-11-03 11:21:54 +03:00
|
|
|
if !ok {
|
2024-10-06 13:45:33 +03:00
|
|
|
var v float64
|
|
|
|
mc = &prometheusCounter{
|
|
|
|
name: name,
|
|
|
|
c: &dto.Metric{
|
|
|
|
Gauge: &dto.Gauge{Value: &v},
|
|
|
|
Label: labelMetric(clabels),
|
|
|
|
},
|
|
|
|
}
|
2024-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
m.counter[h] = mc
|
|
|
|
m.mu.Unlock()
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-11-06 00:00:17 +03:00
|
|
|
return mc
|
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 {
|
2024-10-06 13:45:33 +03:00
|
|
|
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
|
|
|
|
h := newHash(name, clabels)
|
2024-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
mc, ok := m.floatCounter[h]
|
|
|
|
m.mu.Unlock()
|
2022-03-11 01:24:14 +03:00
|
|
|
if !ok {
|
2024-10-06 13:45:33 +03:00
|
|
|
var v float64
|
|
|
|
mc = &prometheusFloatCounter{
|
|
|
|
name: name,
|
|
|
|
c: &dto.Metric{
|
|
|
|
Gauge: &dto.Gauge{Value: &v},
|
|
|
|
Label: labelMetric(clabels),
|
|
|
|
},
|
|
|
|
}
|
2024-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
m.floatCounter[h] = mc
|
|
|
|
m.mu.Unlock()
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-11-06 00:00:17 +03:00
|
|
|
return mc
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge {
|
2024-10-06 13:45:33 +03:00
|
|
|
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
|
|
|
|
h := newHash(name, clabels)
|
2024-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
mc, ok := m.gauge[h]
|
|
|
|
m.mu.Unlock()
|
2022-03-11 01:24:14 +03:00
|
|
|
if !ok {
|
2024-10-06 13:45:33 +03:00
|
|
|
var v float64
|
|
|
|
mc = &prometheusGauge{
|
|
|
|
name: name,
|
|
|
|
c: &dto.Metric{
|
|
|
|
Gauge: &dto.Gauge{Value: &v},
|
|
|
|
Label: labelMetric(clabels),
|
|
|
|
},
|
|
|
|
}
|
2024-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
m.gauge[h] = mc
|
|
|
|
m.mu.Unlock()
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-11-06 00:00:17 +03:00
|
|
|
return mc
|
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 {
|
2024-10-06 13:45:33 +03:00
|
|
|
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
|
|
|
|
h := newHash(name, clabels)
|
2024-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
mc, ok := m.histogram[h]
|
|
|
|
m.mu.Unlock()
|
2022-03-11 01:24:14 +03:00
|
|
|
if !ok {
|
2024-10-06 13:45:33 +03:00
|
|
|
var c uint64
|
|
|
|
var s float64
|
|
|
|
buckets := make([]float64, len(prometheus.DefBuckets))
|
|
|
|
copy(buckets, prometheus.DefBuckets)
|
|
|
|
mdto := &dto.Metric{
|
|
|
|
Histogram: &dto.Histogram{
|
|
|
|
SampleCount: &c,
|
|
|
|
SampleSum: &s,
|
|
|
|
CreatedTimestamp: timestamppb.Now(),
|
|
|
|
Bucket: make([]*dto.Bucket, len(buckets)),
|
|
|
|
},
|
|
|
|
Label: labelMetric(clabels),
|
|
|
|
}
|
|
|
|
for idx, b := range buckets {
|
|
|
|
var cc uint64
|
|
|
|
mdto.Histogram.Bucket[idx] = &dto.Bucket{CumulativeCount: &cc, UpperBound: &b}
|
|
|
|
}
|
|
|
|
mc = &prometheusHistogram{
|
|
|
|
name: name,
|
|
|
|
c: mdto,
|
|
|
|
}
|
2024-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
m.histogram[h] = mc
|
|
|
|
m.mu.Unlock()
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-11-06 00:00:17 +03:00
|
|
|
return mc
|
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 {
|
2024-10-06 13:45:33 +03:00
|
|
|
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
|
|
|
|
h := newHash(name, clabels)
|
2024-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
mc, ok := m.summary[h]
|
|
|
|
m.mu.Unlock()
|
2022-03-11 01:24:14 +03:00
|
|
|
if !ok {
|
2024-10-06 13:45:33 +03:00
|
|
|
var c uint64
|
|
|
|
var s float64
|
|
|
|
mc = &prometheusSummary{
|
|
|
|
name: name,
|
|
|
|
c: &dto.Metric{
|
|
|
|
Summary: &dto.Summary{
|
|
|
|
SampleCount: &c,
|
|
|
|
SampleSum: &s,
|
|
|
|
CreatedTimestamp: timestamppb.Now(),
|
|
|
|
},
|
|
|
|
Label: labelMetric(clabels),
|
|
|
|
},
|
|
|
|
}
|
2024-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
m.summary[h] = mc
|
|
|
|
m.mu.Unlock()
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-11-06 00:00:17 +03:00
|
|
|
return mc
|
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 {
|
2024-10-06 13:45:33 +03:00
|
|
|
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
|
|
|
|
h := newHash(name, clabels)
|
2024-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
mc, ok := m.summary[h]
|
|
|
|
m.mu.Lock()
|
2022-03-11 01:24:14 +03:00
|
|
|
if !ok {
|
2024-10-06 13:45:33 +03:00
|
|
|
var c uint64
|
|
|
|
var s float64
|
|
|
|
mc = &prometheusSummary{
|
|
|
|
name: name,
|
|
|
|
c: &dto.Metric{
|
|
|
|
Summary: &dto.Summary{
|
|
|
|
SampleCount: &c,
|
|
|
|
SampleSum: &s,
|
|
|
|
},
|
|
|
|
Label: labelMetric(clabels),
|
|
|
|
},
|
|
|
|
}
|
2024-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
m.summary[h] = mc
|
|
|
|
m.mu.Unlock()
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-11-06 00:00:17 +03:00
|
|
|
return mc
|
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-11-06 00:00:17 +03:00
|
|
|
m.mu.Lock()
|
|
|
|
|
|
|
|
for _, c := range m.counter {
|
2024-10-06 13:45:33 +03:00
|
|
|
mf := m.mfPool.Get()
|
|
|
|
mf.Name = &c.name
|
|
|
|
mf.Type = dto.MetricType_GAUGE.Enum()
|
|
|
|
mf.Metric = append(mf.Metric, c.c)
|
2022-03-11 01:24:14 +03:00
|
|
|
mfs = append(mfs, mf)
|
2024-11-06 00:00:17 +03:00
|
|
|
}
|
2022-03-11 01:24:14 +03:00
|
|
|
|
2024-11-06 00:00:17 +03:00
|
|
|
for _, c := range m.floatCounter {
|
2024-10-06 13:45:33 +03:00
|
|
|
mf := m.mfPool.Get()
|
|
|
|
mf.Name = &c.name
|
|
|
|
mf.Type = dto.MetricType_GAUGE.Enum()
|
|
|
|
mf.Metric = append(mf.Metric, c.c)
|
2022-03-11 01:37:53 +03:00
|
|
|
mfs = append(mfs, mf)
|
2024-11-06 00:00:17 +03:00
|
|
|
}
|
2022-03-11 01:37:53 +03:00
|
|
|
|
2024-11-06 00:00:17 +03:00
|
|
|
for _, c := range m.gauge {
|
2024-10-06 13:45:33 +03:00
|
|
|
mf := m.mfPool.Get()
|
|
|
|
mf.Name = &c.name
|
|
|
|
mf.Type = dto.MetricType_GAUGE.Enum()
|
|
|
|
mf.Metric = append(mf.Metric, c.c)
|
2022-03-11 01:37:53 +03:00
|
|
|
mfs = append(mfs, mf)
|
2024-11-06 00:00:17 +03:00
|
|
|
}
|
2022-03-11 01:37:53 +03:00
|
|
|
|
2024-11-06 00:00:17 +03:00
|
|
|
for _, c := range m.histogram {
|
2024-10-06 13:45:33 +03:00
|
|
|
mf := m.mfPool.Get()
|
|
|
|
mf.Name = &c.name
|
|
|
|
mf.Type = dto.MetricType_HISTOGRAM.Enum()
|
|
|
|
mf.Metric = append(mf.Metric, c.c)
|
2022-03-11 01:37:53 +03:00
|
|
|
mfs = append(mfs, mf)
|
2024-11-06 00:00:17 +03:00
|
|
|
}
|
2022-03-11 01:37:53 +03:00
|
|
|
|
2024-11-06 00:00:17 +03:00
|
|
|
for _, c := range m.summary {
|
2024-10-06 13:45:33 +03:00
|
|
|
mf := m.mfPool.Get()
|
|
|
|
mf.Name = &c.name
|
|
|
|
mf.Type = dto.MetricType_SUMMARY.Enum()
|
|
|
|
mf.Metric = append(mf.Metric, c.c)
|
2022-03-11 01:24:14 +03:00
|
|
|
mfs = append(mfs, mf)
|
2024-11-06 00:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
m.mu.Unlock()
|
2022-03-11 01:24:14 +03:00
|
|
|
|
2021-11-03 11:21:54 +03:00
|
|
|
for _, mf := range mfs {
|
|
|
|
_ = enc.Encode(mf)
|
2024-10-06 13:45:33 +03:00
|
|
|
mf.Reset()
|
|
|
|
m.mfPool.Put(mf)
|
2021-11-03 11:21:54 +03:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2024-10-06 13:45:33 +03:00
|
|
|
func labelMetric(labels []string) []*dto.LabelPair {
|
|
|
|
dtoLabels := make([]*dto.LabelPair, 0, len(labels)/2)
|
|
|
|
for idx := 0; idx < len(labels); idx += 2 {
|
|
|
|
dtoLabels = append(dtoLabels, &dto.LabelPair{
|
|
|
|
Name: &(labels[idx]),
|
|
|
|
Value: &(labels[idx+1]),
|
|
|
|
})
|
2021-11-03 17:50:29 +03:00
|
|
|
}
|
2024-10-06 13:45:33 +03:00
|
|
|
return dtoLabels
|
2020-04-17 02:45:40 +03:00
|
|
|
}
|
|
|
|
|
2024-10-06 13:45:33 +03:00
|
|
|
func newHash(n string, l []string) uint64 {
|
|
|
|
h := uint64(14695981039346656037)
|
|
|
|
for i := 0; i < len(n); i++ {
|
|
|
|
h ^= uint64(n[i])
|
|
|
|
h *= 1099511628211
|
2022-03-11 01:24:14 +03:00
|
|
|
}
|
2024-10-06 13:45:33 +03:00
|
|
|
for _, s := range l {
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
h ^= uint64(s[i])
|
|
|
|
h *= 1099511628211
|
2024-03-27 11:49:31 +03:00
|
|
|
}
|
2022-03-13 11:18:38 +03:00
|
|
|
}
|
2024-10-06 13:45:33 +03:00
|
|
|
return h
|
2022-03-13 11:18:38 +03:00
|
|
|
}
|