update deps, minimise memory allocations
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
150
prometheus.go
150
prometheus.go
@@ -15,7 +15,7 @@ import (
|
||||
"go.unistack.org/micro/v3/meter"
|
||||
)
|
||||
|
||||
var _ meter.Meter = &prometheusMeter{}
|
||||
var _ meter.Meter = (*prometheusMeter)(nil)
|
||||
|
||||
type prometheusMeter struct {
|
||||
opts meter.Options
|
||||
@@ -48,16 +48,6 @@ type floatCounters struct {
|
||||
cs *sync.Map
|
||||
}
|
||||
|
||||
func newFloat64(v float64) *float64 {
|
||||
nv := v
|
||||
return &nv
|
||||
}
|
||||
|
||||
func newString(v string) *string {
|
||||
nv := v
|
||||
return &nv
|
||||
}
|
||||
|
||||
func NewMeter(opts ...meter.Option) *prometheusMeter {
|
||||
return &prometheusMeter{
|
||||
set: prometheus.NewRegistry(), // prometheus.DefaultRegisterer,
|
||||
@@ -70,52 +60,6 @@ func NewMeter(opts ...meter.Option) *prometheusMeter {
|
||||
}
|
||||
}
|
||||
|
||||
func (m *prometheusMeter) buildMetric(name string, labels ...string) string {
|
||||
if len(m.opts.MetricPrefix) > 0 {
|
||||
name = m.opts.MetricPrefix + name
|
||||
}
|
||||
|
||||
nl := len(m.opts.Labels) + len(labels)
|
||||
if nl == 0 {
|
||||
return name
|
||||
}
|
||||
|
||||
nlabels := make([]string, 0, nl)
|
||||
nlabels = append(nlabels, m.opts.Labels...)
|
||||
nlabels = append(nlabels, labels...)
|
||||
|
||||
if len(m.opts.LabelPrefix) == 0 {
|
||||
return meter.BuildName(name, nlabels...)
|
||||
}
|
||||
|
||||
for idx := 0; idx < nl; idx += 2 {
|
||||
nlabels[idx] = m.opts.LabelPrefix + nlabels[idx]
|
||||
}
|
||||
return meter.BuildName(name, nlabels...)
|
||||
}
|
||||
|
||||
func (m *prometheusMeter) buildName(name string) string {
|
||||
if len(m.opts.MetricPrefix) > 0 {
|
||||
name = m.opts.MetricPrefix + name
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func (m *prometheusMeter) buildLabels(labels ...string) []string {
|
||||
nl := len(labels)
|
||||
if nl == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
nlabels := make([]string, 0, nl)
|
||||
|
||||
for idx := 0; idx < nl; idx += 2 {
|
||||
nlabels = append(nlabels, m.opts.LabelPrefix+labels[idx])
|
||||
nlabels = append(nlabels, labels[idx+1])
|
||||
}
|
||||
return nlabels
|
||||
}
|
||||
|
||||
func (m *prometheusMeter) Name() string {
|
||||
return m.opts.Name
|
||||
}
|
||||
@@ -123,23 +67,22 @@ func (m *prometheusMeter) Name() string {
|
||||
func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
nm := m.buildName(name)
|
||||
labels = m.buildLabels(append(m.opts.Labels, labels...)...) // TODO: Read prometheus.go:128
|
||||
vcd, ok := m.counter.Load(nm)
|
||||
labels = append(m.opts.Labels, labels...)
|
||||
vcd, ok := m.counter.Load(name)
|
||||
h := newHash(labels)
|
||||
if !ok {
|
||||
cd := &counters{cs: &sync.Map{}}
|
||||
c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
||||
c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.counter.Store(nm, cd)
|
||||
m.counter.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
cd := vcd.(*counters)
|
||||
vc, ok := cd.cs.Load(h)
|
||||
if !ok {
|
||||
c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
||||
c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.counter.Store(nm, cd)
|
||||
m.counter.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c := vc.(*prometheusCounter)
|
||||
@@ -149,23 +92,22 @@ func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
|
||||
func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
nm := m.buildName(name)
|
||||
labels = m.buildLabels(append(m.opts.Labels, labels...)...)
|
||||
vcd, ok := m.floatCounter.Load(nm)
|
||||
labels = append(m.opts.Labels, labels...)
|
||||
vcd, ok := m.floatCounter.Load(name)
|
||||
h := newHash(labels)
|
||||
if !ok {
|
||||
cd := &floatCounters{cs: &sync.Map{}}
|
||||
c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
||||
c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.floatCounter.Store(nm, cd)
|
||||
m.floatCounter.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
cd := vcd.(*floatCounters)
|
||||
vc, ok := cd.cs.Load(h)
|
||||
if !ok {
|
||||
c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
||||
c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.floatCounter.Store(nm, cd)
|
||||
m.floatCounter.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c := vc.(*prometheusFloatCounter)
|
||||
@@ -175,23 +117,22 @@ func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.Floa
|
||||
func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
nm := m.buildName(name)
|
||||
labels = m.buildLabels(append(m.opts.Labels, labels...)...)
|
||||
vcd, ok := m.gauge.Load(nm)
|
||||
labels = append(m.opts.Labels, labels...)
|
||||
vcd, ok := m.gauge.Load(name)
|
||||
h := newHash(labels)
|
||||
if !ok {
|
||||
cd := &gauges{cs: &sync.Map{}}
|
||||
c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
||||
c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.gauge.Store(nm, cd)
|
||||
m.gauge.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
cd := vcd.(*gauges)
|
||||
vc, ok := cd.cs.Load(h)
|
||||
if !ok {
|
||||
c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
|
||||
c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.gauge.Store(nm, cd)
|
||||
m.gauge.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c := vc.(*prometheusGauge)
|
||||
@@ -201,23 +142,22 @@ func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string
|
||||
func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
nm := m.buildName(name)
|
||||
labels = m.buildLabels(append(m.opts.Labels, labels...)...)
|
||||
vcd, ok := m.histogram.Load(nm)
|
||||
labels = append(m.opts.Labels, labels...)
|
||||
vcd, ok := m.histogram.Load(name)
|
||||
h := newHash(labels)
|
||||
if !ok {
|
||||
cd := &histograms{cs: &sync.Map{}}
|
||||
c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels}
|
||||
c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.histogram.Store(nm, cd)
|
||||
m.histogram.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
cd := vcd.(*histograms)
|
||||
vc, ok := cd.cs.Load(h)
|
||||
if !ok {
|
||||
c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels}
|
||||
c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.histogram.Store(nm, cd)
|
||||
m.histogram.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c := vc.(*prometheusHistogram)
|
||||
@@ -227,23 +167,22 @@ func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogr
|
||||
func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
nm := m.buildName(name)
|
||||
labels = m.buildLabels(append(m.opts.Labels, labels...)...)
|
||||
vcd, ok := m.summary.Load(nm)
|
||||
labels = append(m.opts.Labels, labels...)
|
||||
vcd, ok := m.summary.Load(name)
|
||||
h := newHash(labels)
|
||||
if !ok {
|
||||
cd := &summaries{cs: &sync.Map{}}
|
||||
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels}
|
||||
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.summary.Store(nm, cd)
|
||||
m.summary.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
cd := vcd.(*summaries)
|
||||
vc, ok := cd.cs.Load(h)
|
||||
if !ok {
|
||||
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels}
|
||||
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: name}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.summary.Store(nm, cd)
|
||||
m.summary.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c := vc.(*prometheusSummary)
|
||||
@@ -253,31 +192,30 @@ func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
|
||||
func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
nm := m.buildName(name)
|
||||
labels = m.buildLabels(append(m.opts.Labels, labels...)...)
|
||||
vcd, ok := m.summary.Load(nm)
|
||||
labels = append(m.opts.Labels, labels...)
|
||||
vcd, ok := m.summary.Load(name)
|
||||
h := newHash(labels)
|
||||
if !ok {
|
||||
cd := &summaries{cs: &sync.Map{}}
|
||||
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
|
||||
Name: nm,
|
||||
Name: name,
|
||||
MaxAge: window,
|
||||
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
|
||||
}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.summary.Store(nm, cd)
|
||||
m.summary.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
cd := vcd.(*summaries)
|
||||
vc, ok := cd.cs.Load(h)
|
||||
if !ok {
|
||||
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
|
||||
Name: nm,
|
||||
Name: name,
|
||||
MaxAge: window,
|
||||
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
|
||||
}), labels: labels}
|
||||
cd.cs.Store(h, c)
|
||||
m.summary.Store(nm, cd)
|
||||
m.summary.Store(name, cd)
|
||||
return c
|
||||
}
|
||||
c := vc.(*prometheusSummary)
|
||||
@@ -320,7 +258,7 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
|
||||
m.counter.Range(func(k, v any) bool {
|
||||
name := k.(string)
|
||||
mf := &dto.MetricFamily{
|
||||
Name: newString(name),
|
||||
Name: &name,
|
||||
Type: dto.MetricType_GAUGE.Enum(),
|
||||
}
|
||||
v.(*counters).cs.Range(func(_, nv any) bool {
|
||||
@@ -338,7 +276,7 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
|
||||
m.gauge.Range(func(k, v any) bool {
|
||||
name := k.(string)
|
||||
mf := &dto.MetricFamily{
|
||||
Name: newString(name),
|
||||
Name: &name,
|
||||
Type: dto.MetricType_GAUGE.Enum(),
|
||||
}
|
||||
v.(*gauges).cs.Range(func(_, nv any) bool {
|
||||
@@ -356,7 +294,7 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
|
||||
m.floatCounter.Range(func(k, v any) bool {
|
||||
name := k.(string)
|
||||
mf := &dto.MetricFamily{
|
||||
Name: newString(name),
|
||||
Name: &name,
|
||||
Type: dto.MetricType_GAUGE.Enum(),
|
||||
}
|
||||
v.(*floatCounters).cs.Range(func(_, nv any) bool {
|
||||
@@ -374,7 +312,7 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
|
||||
m.histogram.Range(func(k, v any) bool {
|
||||
name := k.(string)
|
||||
mf := &dto.MetricFamily{
|
||||
Name: newString(name),
|
||||
Name: &name,
|
||||
Type: dto.MetricType_HISTOGRAM.Enum(),
|
||||
}
|
||||
v.(*histograms).cs.Range(func(_, nv any) bool {
|
||||
@@ -392,7 +330,7 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
|
||||
m.summary.Range(func(k, v any) bool {
|
||||
name := k.(string)
|
||||
mf := &dto.MetricFamily{
|
||||
Name: newString(name),
|
||||
Name: &name,
|
||||
Type: dto.MetricType_SUMMARY.Enum(),
|
||||
}
|
||||
v.(*summaries).cs.Range(func(_, nv any) bool {
|
||||
@@ -566,8 +504,8 @@ func fillMetric(m *dto.Metric, labels []string) *dto.Metric {
|
||||
continue
|
||||
}
|
||||
m.Label = append(m.Label, &dto.LabelPair{
|
||||
Name: newString(labels[idx]),
|
||||
Value: newString(labels[idx+1]),
|
||||
Name: &(labels[idx]),
|
||||
Value: &(labels[idx+1]),
|
||||
})
|
||||
seen[labels[idx]] = true
|
||||
}
|
||||
|
Reference in New Issue
Block a user