add locking
Some checks failed
codeql / analyze (go) (push) Failing after 44s
build / test (push) Failing after 4m56s
build / lint (push) Successful in 9m35s

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2024-11-06 00:00:17 +03:00
parent da9201efff
commit dd71d9ec59
2 changed files with 77 additions and 50 deletions

View File

@ -21,23 +21,24 @@ var _ meter.Meter = (*prometheusMeter)(nil)
type prometheusMeter struct { type prometheusMeter struct {
opts meter.Options opts meter.Options
set prometheus.Registerer set prometheus.Registerer
counter *sync.Map counter map[uint64]*prometheusCounter
floatCounter *sync.Map floatCounter map[uint64]*prometheusFloatCounter
gauge *sync.Map gauge map[uint64]*prometheusGauge
histogram *sync.Map histogram map[uint64]*prometheusHistogram
summary *sync.Map summary map[uint64]*prometheusSummary
mfPool xpool.Pool[*dto.MetricFamily] mfPool xpool.Pool[*dto.MetricFamily]
mu sync.Mutex
} }
func NewMeter(opts ...meter.Option) *prometheusMeter { 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: &sync.Map{}, counter: make(map[uint64]*prometheusCounter),
floatCounter: &sync.Map{}, floatCounter: make(map[uint64]*prometheusFloatCounter),
gauge: &sync.Map{}, gauge: make(map[uint64]*prometheusGauge),
histogram: &sync.Map{}, histogram: make(map[uint64]*prometheusHistogram),
summary: &sync.Map{}, summary: make(map[uint64]*prometheusSummary),
mfPool: xpool.NewPool[*dto.MetricFamily](func() *dto.MetricFamily { mfPool: xpool.NewPool[*dto.MetricFamily](func() *dto.MetricFamily {
return &dto.MetricFamily{} return &dto.MetricFamily{}
}), }),
@ -51,7 +52,9 @@ func (m *prometheusMeter) Name() string {
func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter { func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels) h := newHash(name, clabels)
mc, ok := m.counter.Load(h) m.mu.Lock()
mc, ok := m.counter[h]
m.mu.Unlock()
if !ok { if !ok {
var v float64 var v float64
mc = &prometheusCounter{ mc = &prometheusCounter{
@ -61,15 +64,19 @@ func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
Label: labelMetric(clabels), Label: labelMetric(clabels),
}, },
} }
m.counter.Store(h, mc) m.mu.Lock()
m.counter[h] = mc
m.mu.Unlock()
} }
return mc.(*prometheusCounter) return mc
} }
func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter { func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.FloatCounter {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels) h := newHash(name, clabels)
mc, ok := m.floatCounter.Load(h) m.mu.Lock()
mc, ok := m.floatCounter[h]
m.mu.Unlock()
if !ok { if !ok {
var v float64 var v float64
mc = &prometheusFloatCounter{ mc = &prometheusFloatCounter{
@ -79,15 +86,19 @@ func (m *prometheusMeter) FloatCounter(name string, labels ...string) meter.Floa
Label: labelMetric(clabels), Label: labelMetric(clabels),
}, },
} }
m.floatCounter.Store(h, mc) m.mu.Lock()
m.floatCounter[h] = mc
m.mu.Unlock()
} }
return mc.(*prometheusFloatCounter) return mc
} }
func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge { func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string) meter.Gauge {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels) h := newHash(name, clabels)
mc, ok := m.gauge.Load(h) m.mu.Lock()
mc, ok := m.gauge[h]
m.mu.Unlock()
if !ok { if !ok {
var v float64 var v float64
mc = &prometheusGauge{ mc = &prometheusGauge{
@ -97,15 +108,19 @@ func (m *prometheusMeter) Gauge(name string, fn func() float64, labels ...string
Label: labelMetric(clabels), Label: labelMetric(clabels),
}, },
} }
m.gauge.Store(h, mc) m.mu.Lock()
m.gauge[h] = mc
m.mu.Unlock()
} }
return mc.(*prometheusGauge) return mc
} }
func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram { func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogram {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels) h := newHash(name, clabels)
mc, ok := m.histogram.Load(h) m.mu.Lock()
mc, ok := m.histogram[h]
m.mu.Unlock()
if !ok { if !ok {
var c uint64 var c uint64
var s float64 var s float64
@ -128,16 +143,19 @@ func (m *prometheusMeter) Histogram(name string, labels ...string) meter.Histogr
name: name, name: name,
c: mdto, c: mdto,
} }
m.mu.Lock()
m.histogram.Store(h, mc) m.histogram[h] = mc
m.mu.Unlock()
} }
return mc.(*prometheusHistogram) return mc
} }
func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary { func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels) h := newHash(name, clabels)
mc, ok := m.summary.Load(h) m.mu.Lock()
mc, ok := m.summary[h]
m.mu.Unlock()
if !ok { if !ok {
var c uint64 var c uint64
var s float64 var s float64
@ -152,15 +170,19 @@ func (m *prometheusMeter) Summary(name string, labels ...string) meter.Summary {
Label: labelMetric(clabels), Label: labelMetric(clabels),
}, },
} }
m.summary.Store(h, mc) m.mu.Lock()
m.summary[h] = mc
m.mu.Unlock()
} }
return mc.(*prometheusSummary) return mc
} }
func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary { func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantiles []float64, labels ...string) meter.Summary {
clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...) clabels := meter.BuildLabels(append(m.opts.Labels, labels...)...)
h := newHash(name, clabels) h := newHash(name, clabels)
mc, ok := m.summary.Load(h) m.mu.Lock()
mc, ok := m.summary[h]
m.mu.Lock()
if !ok { if !ok {
var c uint64 var c uint64
var s float64 var s float64
@ -174,9 +196,11 @@ func (m *prometheusMeter) SummaryExt(name string, window time.Duration, quantile
Label: labelMetric(clabels), Label: labelMetric(clabels),
}, },
} }
m.summary.Store(h, mc) m.mu.Lock()
m.summary[h] = mc
m.mu.Unlock()
} }
return mc.(*prometheusSummary) return mc
} }
func (m *prometheusMeter) Init(opts ...meter.Option) error { func (m *prometheusMeter) Init(opts ...meter.Option) error {
@ -212,55 +236,49 @@ 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))
m.counter.Range(func(k, v any) bool { m.mu.Lock()
c := v.(*prometheusCounter)
for _, c := range m.counter {
mf := m.mfPool.Get() mf := m.mfPool.Get()
mf.Name = &c.name mf.Name = &c.name
mf.Type = dto.MetricType_GAUGE.Enum() mf.Type = dto.MetricType_GAUGE.Enum()
mf.Metric = append(mf.Metric, c.c) mf.Metric = append(mf.Metric, c.c)
mfs = append(mfs, mf) mfs = append(mfs, mf)
return true }
})
m.floatCounter.Range(func(k, v any) bool { for _, c := range m.floatCounter {
c := v.(*prometheusFloatCounter)
mf := m.mfPool.Get() mf := m.mfPool.Get()
mf.Name = &c.name mf.Name = &c.name
mf.Type = dto.MetricType_GAUGE.Enum() mf.Type = dto.MetricType_GAUGE.Enum()
mf.Metric = append(mf.Metric, c.c) mf.Metric = append(mf.Metric, c.c)
mfs = append(mfs, mf) mfs = append(mfs, mf)
return true }
})
m.gauge.Range(func(k, v any) bool { for _, c := range m.gauge {
c := v.(*prometheusGauge)
mf := m.mfPool.Get() mf := m.mfPool.Get()
mf.Name = &c.name mf.Name = &c.name
mf.Type = dto.MetricType_GAUGE.Enum() mf.Type = dto.MetricType_GAUGE.Enum()
mf.Metric = append(mf.Metric, c.c) mf.Metric = append(mf.Metric, c.c)
mfs = append(mfs, mf) mfs = append(mfs, mf)
return true }
})
m.histogram.Range(func(k, v any) bool { for _, c := range m.histogram {
c := v.(*prometheusHistogram)
mf := m.mfPool.Get() mf := m.mfPool.Get()
mf.Name = &c.name mf.Name = &c.name
mf.Type = dto.MetricType_HISTOGRAM.Enum() mf.Type = dto.MetricType_HISTOGRAM.Enum()
mf.Metric = append(mf.Metric, c.c) mf.Metric = append(mf.Metric, c.c)
mfs = append(mfs, mf) mfs = append(mfs, mf)
return true }
})
m.summary.Range(func(k, v any) bool { for _, c := range m.summary {
c := v.(*prometheusSummary)
mf := m.mfPool.Get() mf := m.mfPool.Get()
mf.Name = &c.name mf.Name = &c.name
mf.Type = dto.MetricType_SUMMARY.Enum() mf.Type = dto.MetricType_SUMMARY.Enum()
mf.Metric = append(mf.Metric, c.c) mf.Metric = append(mf.Metric, c.c)
mfs = append(mfs, mf) mfs = append(mfs, mf)
return true }
})
m.mu.Unlock()
for _, mf := range mfs { for _, mf := range mfs {
_ = enc.Encode(mf) _ = enc.Encode(mf)

View File

@ -14,6 +14,14 @@ import (
"go.unistack.org/micro/v3/meter" "go.unistack.org/micro/v3/meter"
) )
func TestHash(t *testing.T) {
t.Skip()
h1 := newHash("micro_server_request_total", []string{"code", "16", "endpoint", "/clientprofile.ClientProfileService/GetClientProfile", "status", "failure"})
h2 := newHash("micro_server_request_total", []string{"code", "16", "endpoint", "/clientproduct.ClientProductService/GetDepositProducts", "status", "failure"})
h3 := newHash("micro_server_request_total", []string{"code", "16", "endpoint", "/operationsinfo.OperationsInfoService/GetOperations", "status", "failure"})
t.Logf("h1: %v\nh2: %v\nh3: %v\n", h1, h2, h3)
}
func TestHistogram(t *testing.T) { func TestHistogram(t *testing.T) {
m := NewMeter() m := NewMeter()
name := "test" name := "test"
@ -56,6 +64,7 @@ func TestHistogram(t *testing.T) {
} }
func TestSummary(t *testing.T) { func TestSummary(t *testing.T) {
t.Skip()
name := "micro_server" name := "micro_server"
m := NewMeter() m := NewMeter()
m.Summary("micro_server").Update(1) m.Summary("micro_server").Update(1)
@ -87,7 +96,7 @@ func TestSummary(t *testing.T) {
p.Observe(10) p.Observe(10)
p.Observe(30) p.Observe(30)
mdto := &dto.Metric{} mdto := &dto.Metric{}
p.Write(mdto) _ = p.Write(mdto)
pbuf := bytes.NewBuffer(nil) pbuf := bytes.NewBuffer(nil)
enc := expfmt.NewEncoder(pbuf, expfmt.NewFormat(expfmt.TypeTextPlain)) enc := expfmt.NewEncoder(pbuf, expfmt.NewFormat(expfmt.TypeTextPlain))
mf := &dto.MetricFamily{Name: &name, Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{mdto}} mf := &dto.MetricFamily{Name: &name, Type: dto.MetricType_SUMMARY.Enum(), Metric: []*dto.Metric{mdto}}