Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RWLock in prometheus metrics #331

Merged
merged 2 commits into from
Oct 6, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type PromMetrics struct {
// metrics keeps a record of all the registered metrics so we can increment
// them by name
metrics map[string]interface{}
lock sync.Mutex
lock sync.RWMutex

prefix string
}
Expand Down Expand Up @@ -57,30 +57,30 @@ func (p *PromMetrics) Register(name string, metricType string) {
switch metricType {
case "counter":
newmet = promauto.NewCounter(prometheus.CounterOpts{
Name: name,
Name: name,
Namespace: p.prefix,
Help: name,
Help: name,
})
case "gauge":
newmet = promauto.NewGauge(prometheus.GaugeOpts{
Name: name,
Name: name,
Namespace: p.prefix,
Help: name,
Help: name,
})
case "histogram":
newmet = promauto.NewHistogram(prometheus.HistogramOpts{
Name: name,
Name: name,
Namespace: p.prefix,
Help: name,
Help: name,
})
}

p.metrics[name] = newmet
}

func (p *PromMetrics) Increment(name string) {
p.lock.Lock()
defer p.lock.Unlock()
p.lock.RLock()
defer p.lock.RUnlock()

if counterIface, ok := p.metrics[name]; ok {
if counter, ok := counterIface.(prometheus.Counter); ok {
Expand All @@ -89,8 +89,8 @@ func (p *PromMetrics) Increment(name string) {
}
}
func (p *PromMetrics) Count(name string, n interface{}) {
p.lock.Lock()
defer p.lock.Unlock()
p.lock.RLock()
defer p.lock.RUnlock()

if counterIface, ok := p.metrics[name]; ok {
if counter, ok := counterIface.(prometheus.Counter); ok {
Expand All @@ -99,8 +99,8 @@ func (p *PromMetrics) Count(name string, n interface{}) {
}
}
func (p *PromMetrics) Gauge(name string, val interface{}) {
p.lock.Lock()
defer p.lock.Unlock()
p.lock.RLock()
defer p.lock.RUnlock()

if gaugeIface, ok := p.metrics[name]; ok {
if gauge, ok := gaugeIface.(prometheus.Gauge); ok {
Expand All @@ -109,8 +109,8 @@ func (p *PromMetrics) Gauge(name string, val interface{}) {
}
}
func (p *PromMetrics) Histogram(name string, obs interface{}) {
p.lock.Lock()
defer p.lock.Unlock()
p.lock.RLock()
defer p.lock.RUnlock()

if histIface, ok := p.metrics[name]; ok {
if hist, ok := histIface.(prometheus.Histogram); ok {
Expand Down