rewrite to use sync.Map
Some checks failed
build / test (push) Failing after 1m29s
codeql / analyze (go) (push) Failing after 1m38s
build / lint (push) Has been cancelled

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-04-15 00:27:15 +03:00
parent 439d5cf125
commit 8dfa97b54f

View File

@ -20,32 +20,32 @@ var _ meter.Meter = &prometheusMeter{}
type prometheusMeter struct { type prometheusMeter struct {
opts meter.Options opts meter.Options
set prometheus.Registerer set prometheus.Registerer
counter map[string]*counters counter *sync.Map
floatCounter map[string]*floatCounters floatCounter *sync.Map
gauge map[string]*gauges gauge *sync.Map
histogram map[string]*histograms histogram *sync.Map
summary map[string]*summaries summary *sync.Map
sync.Mutex sync.Mutex
} }
type counters struct { type counters struct {
cs map[uint64]*prometheusCounter cs *sync.Map
} }
type gauges struct { type gauges struct {
cs map[uint64]*prometheusGauge cs *sync.Map
} }
type histograms struct { type histograms struct {
cs map[uint64]*prometheusHistogram cs *sync.Map
} }
type summaries struct { type summaries struct {
cs map[uint64]*prometheusSummary cs *sync.Map
} }
type floatCounters struct { type floatCounters struct {
cs map[uint64]*prometheusFloatCounter cs *sync.Map
} }
func newFloat64(v float64) *float64 { func newFloat64(v float64) *float64 {
@ -62,11 +62,11 @@ func NewMeter(opts ...meter.Option) *prometheusMeter {
return &prometheusMeter{ return &prometheusMeter{
set: prometheus.NewRegistry(), // prometheus.DefaultRegisterer, set: prometheus.NewRegistry(), // prometheus.DefaultRegisterer,
opts: meter.NewOptions(opts...), opts: meter.NewOptions(opts...),
counter: make(map[string]*counters), counter: &sync.Map{},
floatCounter: make(map[string]*floatCounters), floatCounter: &sync.Map{},
gauge: make(map[string]*gauges), gauge: &sync.Map{},
histogram: make(map[string]*histograms), histogram: &sync.Map{},
summary: make(map[string]*summaries), summary: &sync.Map{},
} }
} }
@ -124,21 +124,23 @@ func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
m.Lock() m.Lock()
defer m.Unlock() defer m.Unlock()
nm := m.buildName(name) nm := m.buildName(name)
labels = m.buildLabels(append(m.opts.Labels, labels...)...) labels = m.buildLabels(append(m.opts.Labels, labels...)...) // TODO: Read prometheus.go:128
cd, ok := m.counter[nm] vcd, ok := m.counter.Load(nm)
h := newHash(labels) h := newHash(labels)
cd := vcd.(*counters)
if !ok { if !ok {
cd = &counters{cs: make(map[uint64]*prometheusCounter)} cd = &counters{cs: &sync.Map{}}
c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels} c := &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c cd.cs.Store(h, c)
m.counter[nm] = cd m.counter.Store(nm, cd)
return c return c
} }
c, ok := cd.cs[h] vc, ok := cd.cs.Load(h)
c := vc.(*prometheusCounter)
if !ok { if !ok {
c = &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels} c = &prometheusCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c cd.cs.Store(h, c)
m.counter[nm] = cd m.counter.Store(nm, cd)
} }
return c return c
} }
@ -148,20 +150,22 @@ func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.Floa
defer m.Unlock() defer m.Unlock()
nm := m.buildName(name) nm := m.buildName(name)
labels = m.buildLabels(append(m.opts.Labels, labels...)...) labels = m.buildLabels(append(m.opts.Labels, labels...)...)
cd, ok := m.floatCounter[nm] vcd, ok := m.floatCounter.Load(nm)
h := newHash(labels) h := newHash(labels)
cd := vcd.(*floatCounters)
if !ok { if !ok {
cd = &floatCounters{cs: make(map[uint64]*prometheusFloatCounter)} cd = &floatCounters{cs: &sync.Map{}}
c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels} c := &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c cd.cs.Store(h, c)
m.floatCounter[nm] = cd m.floatCounter.Store(nm, cd)
return c return c
} }
c, ok := cd.cs[h] vc, ok := cd.cs.Load(h)
c := vc.(*prometheusFloatCounter)
if !ok { if !ok {
c = &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels} c = &prometheusFloatCounter{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c cd.cs.Store(h, c)
m.floatCounter[nm] = cd m.floatCounter.Store(nm, cd)
} }
return c return c
} }
@ -171,20 +175,22 @@ func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string
defer m.Unlock() defer m.Unlock()
nm := m.buildName(name) nm := m.buildName(name)
labels = m.buildLabels(append(m.opts.Labels, labels...)...) labels = m.buildLabels(append(m.opts.Labels, labels...)...)
cd, ok := m.gauge[nm] vcd, ok := m.gauge.Load(nm)
h := newHash(labels) h := newHash(labels)
cd := vcd.(*gauges)
if !ok { if !ok {
cd = &gauges{cs: make(map[uint64]*prometheusGauge)} cd = &gauges{cs: &sync.Map{}}
c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels} c := &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c cd.cs.Store(h, c)
m.gauge[nm] = cd m.gauge.Store(nm, cd)
return c return c
} }
c, ok := cd.cs[h] vc, ok := cd.cs.Load(h)
c := vc.(*prometheusGauge)
if !ok { if !ok {
c = &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels} c = &prometheusGauge{c: prometheus.NewGauge(prometheus.GaugeOpts{Name: nm}), labels: labels}
cd.cs[h] = c cd.cs.Store(h, c)
m.gauge[nm] = cd m.gauge.Store(nm, cd)
} }
return c return c
} }
@ -194,20 +200,22 @@ func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogr
defer m.Unlock() defer m.Unlock()
nm := m.buildName(name) nm := m.buildName(name)
labels = m.buildLabels(append(m.opts.Labels, labels...)...) labels = m.buildLabels(append(m.opts.Labels, labels...)...)
cd, ok := m.histogram[nm] vcd, ok := m.histogram.Load(nm)
h := newHash(labels) h := newHash(labels)
cd := vcd.(*histograms)
if !ok { if !ok {
cd = &histograms{cs: make(map[uint64]*prometheusHistogram)} cd = &histograms{cs: &sync.Map{}}
c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels} c := &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels}
cd.cs[h] = c cd.cs.Store(h, c)
m.histogram[nm] = cd m.histogram.Store(nm, cd)
return c return c
} }
c, ok := cd.cs[h] vc, ok := cd.cs.Load(h)
c := vc.(*prometheusHistogram)
if !ok { if !ok {
c = &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels} c = &prometheusHistogram{c: prometheus.NewHistogram(prometheus.HistogramOpts{Name: nm}), labels: labels}
cd.cs[h] = c cd.cs.Store(h, c)
m.histogram[nm] = cd m.histogram.Store(nm, cd)
} }
return c return c
} }
@ -217,20 +225,22 @@ func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
defer m.Unlock() defer m.Unlock()
nm := m.buildName(name) nm := m.buildName(name)
labels = m.buildLabels(append(m.opts.Labels, labels...)...) labels = m.buildLabels(append(m.opts.Labels, labels...)...)
cd, ok := m.summary[nm] vcd, ok := m.summary.Load(nm)
h := newHash(labels) h := newHash(labels)
cd := vcd.(*summaries)
if !ok { if !ok {
cd = &summaries{cs: make(map[uint64]*prometheusSummary)} cd = &summaries{cs: &sync.Map{}}
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels} c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels}
cd.cs[h] = c cd.cs.Store(h, c)
m.summary[nm] = cd m.summary.Store(nm, cd)
return c return c
} }
c, ok := cd.cs[h] vc, ok := cd.cs.Load(h)
c := vc.(*prometheusSummary)
if !ok { if !ok {
c = &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels} c = &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{Name: nm}), labels: labels}
cd.cs[h] = c cd.cs.Store(h, c)
m.summary[nm] = cd m.summary.Store(nm, cd)
} }
return c return c
} }
@ -240,28 +250,30 @@ func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantile
defer m.Unlock() defer m.Unlock()
nm := m.buildName(name) nm := m.buildName(name)
labels = m.buildLabels(append(m.opts.Labels, labels...)...) labels = m.buildLabels(append(m.opts.Labels, labels...)...)
cd, ok := m.summary[nm] vcd, ok := m.summary.Load(nm)
h := newHash(labels) h := newHash(labels)
cd := vcd.(*summaries)
if !ok { if !ok {
cd = &summaries{cs: make(map[uint64]*prometheusSummary)} cd = &summaries{cs: &sync.Map{}}
c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{ c := &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
Name: nm, Name: nm,
MaxAge: window, MaxAge: window,
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}), labels: labels} }), labels: labels}
cd.cs[h] = c cd.cs.Store(h, c)
m.summary[nm] = cd m.summary.Store(nm, cd)
return c return c
} }
c, ok := cd.cs[h] vc, ok := cd.cs.Load(h)
c := vc.(*prometheusSummary)
if !ok { if !ok {
c = &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{ c = &prometheusSummary{c: prometheus.NewSummary(prometheus.SummaryOpts{
Name: nm, Name: nm,
MaxAge: window, MaxAge: window,
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001}, Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
}), labels: labels} }), labels: labels}
cd.cs[h] = c cd.cs.Store(h, c)
m.summary[nm] = cd m.summary.Store(nm, cd)
} }
return c return c
} }
@ -298,80 +310,95 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
enc := expfmt.NewEncoder(w, expfmt.NewFormat(expfmt.TypeTextPlain)) enc := expfmt.NewEncoder(w, expfmt.NewFormat(expfmt.TypeTextPlain))
for name, metrics := range m.counter { m.counter.Range(func(k, v any) bool {
name := k.(string)
mf := &dto.MetricFamily{ mf := &dto.MetricFamily{
Name: newString(name), Name: newString(name),
Type: dto.MetricType_GAUGE.Enum(), Type: dto.MetricType_GAUGE.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
} }
for _, c := range metrics.cs { v.(*counters).cs.Range(func(_, nv any) bool {
c := nv.(*prometheusCounter)
m := &dto.Metric{} m := &dto.Metric{}
_ = c.c.Write(m) _ = c.c.Write(m)
fillMetric(m, c.labels) fillMetric(m, c.labels)
mf.Metric = append(mf.Metric, m) mf.Metric = append(mf.Metric, m)
} return true
})
mfs = append(mfs, mf) mfs = append(mfs, mf)
} return true
})
for name, metrics := range m.gauge { m.gauge.Range(func(k, v any) bool {
name := k.(string)
mf := &dto.MetricFamily{ mf := &dto.MetricFamily{
Name: newString(name), Name: newString(name),
Type: dto.MetricType_GAUGE.Enum(), Type: dto.MetricType_GAUGE.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
} }
for _, c := range metrics.cs { v.(*gauges).cs.Range(func(_, nv any) bool {
c := nv.(*prometheusGauge)
m := &dto.Metric{} m := &dto.Metric{}
_ = c.c.Write(m) _ = c.c.Write(m)
fillMetric(m, c.labels) fillMetric(m, c.labels)
mf.Metric = append(mf.Metric, m) mf.Metric = append(mf.Metric, m)
} return true
})
mfs = append(mfs, mf) mfs = append(mfs, mf)
} return true
})
for name, metrics := range m.floatCounter { m.floatCounter.Range(func(k, v any) bool {
name := k.(string)
mf := &dto.MetricFamily{ mf := &dto.MetricFamily{
Name: newString(name), Name: newString(name),
Type: dto.MetricType_GAUGE.Enum(), Type: dto.MetricType_GAUGE.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
} }
for _, c := range metrics.cs { v.(*floatCounters).cs.Range(func(_, nv any) bool {
c := nv.(*prometheusGauge)
m := &dto.Metric{} m := &dto.Metric{}
_ = c.c.Write(m) _ = c.c.Write(m)
fillMetric(m, c.labels) fillMetric(m, c.labels)
mf.Metric = append(mf.Metric, m) mf.Metric = append(mf.Metric, m)
} return true
})
mfs = append(mfs, mf) mfs = append(mfs, mf)
} return true
})
for name, metrics := range m.histogram { m.histogram.Range(func(k, v any) bool {
name := k.(string)
mf := &dto.MetricFamily{ mf := &dto.MetricFamily{
Name: newString(name), Name: newString(name),
Type: dto.MetricType_HISTOGRAM.Enum(), Type: dto.MetricType_HISTOGRAM.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
} }
for _, c := range metrics.cs { v.(*histograms).cs.Range(func(_, nv any) bool {
c := nv.(*prometheusGauge)
m := &dto.Metric{} m := &dto.Metric{}
_ = c.c.Write(m) _ = c.c.Write(m)
fillMetric(m, c.labels) fillMetric(m, c.labels)
mf.Metric = append(mf.Metric, m) mf.Metric = append(mf.Metric, m)
} return true
})
mfs = append(mfs, mf) mfs = append(mfs, mf)
} return true
})
for name, metrics := range m.summary { m.summary.Range(func(k, v any) bool {
name := k.(string)
mf := &dto.MetricFamily{ mf := &dto.MetricFamily{
Name: newString(name), Name: newString(name),
Type: dto.MetricType_SUMMARY.Enum(), Type: dto.MetricType_SUMMARY.Enum(),
Metric: make([]*dto.Metric, 0, len(metrics.cs)),
} }
for _, c := range metrics.cs { v.(*summaries).cs.Range(func(_, nv any) bool {
c := nv.(*prometheusGauge)
m := &dto.Metric{} m := &dto.Metric{}
_ = c.c.Write(m) _ = c.c.Write(m)
fillMetric(m, c.labels) fillMetric(m, c.labels)
mf.Metric = append(mf.Metric, m) mf.Metric = append(mf.Metric, m)
} return true
})
mfs = append(mfs, mf) mfs = append(mfs, mf)
} return true
})
for _, mf := range mfs { for _, mf := range mfs {
_ = enc.Encode(mf) _ = enc.Encode(mf)