Compare commits

...

1 Commits
v3.8.21 ... v3

Author SHA1 Message Date
591c427815 protect meter by mutex
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2025-03-07 16:53:47 +03:00

@ -26,7 +26,7 @@ type prometheusMeter struct {
gauge map[string]*gauges
histogram map[string]*histograms
summary map[string]*summaries
sync.Mutex
mu *sync.Mutex
}
type counters struct {
@ -79,6 +79,7 @@ func NewMeter(opts ...meter.Option) *prometheusMeter {
gauge: make(map[string]*gauges),
histogram: make(map[string]*histograms),
summary: make(map[string]*summaries),
mu: &sync.Mutex{},
}
}
@ -100,8 +101,8 @@ func (m *prometheusMeter) Name() string {
}
func (m *prometheusMeter) Counter(name string, labels ...string) meter.Counter {
m.Lock()
defer m.Unlock()
m.mu.Lock()
defer m.mu.Unlock()
nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...)
cd, ok := m.counter[nm]
@ -123,8 +124,8 @@ 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()
m.mu.Lock()
defer m.mu.Unlock()
nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...)
cd, ok := m.floatCounter[nm]
@ -146,8 +147,8 @@ 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()
m.mu.Lock()
defer m.mu.Unlock()
nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...)
cd, ok := m.gauge[nm]
@ -169,8 +170,8 @@ 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()
m.mu.Lock()
defer m.mu.Unlock()
nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...)
cd, ok := m.histogram[nm]
@ -192,8 +193,8 @@ 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()
m.mu.Lock()
defer m.mu.Unlock()
nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...)
cd, ok := m.summary[nm]
@ -215,8 +216,8 @@ 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()
m.mu.Lock()
defer m.mu.Unlock()
nm := m.buildMetric(name)
labels = append(m.opts.Labels, labels...)
cd, ok := m.summary[nm]
@ -277,6 +278,8 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
enc := expfmt.NewEncoder(w, expfmt.NewFormat(expfmt.TypeTextPlain))
m.mu.Lock()
for name, metrics := range m.counter {
mf := &dto.MetricFamily{
Name: newName(name),
@ -352,6 +355,8 @@ func (m *prometheusMeter) Write(w io.Writer, opts ...meter.Option) error {
mfs = append(mfs, mf)
}
m.mu.Unlock()
for _, mf := range mfs {
_ = enc.Encode(mf)
}
@ -370,6 +375,7 @@ func (m *prometheusMeter) Clone(opts ...meter.Option) meter.Meter {
}
return &prometheusMeter{
mu: m.mu,
set: m.set,
opts: options,
floatCounter: m.floatCounter,