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

frontend: Make PrometheusEmitter concurrency-safe #836

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Changes from all commits
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
8 changes: 7 additions & 1 deletion frontend/pkg/frontend/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package frontend
import (
"net/http"
"strconv"
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -22,6 +23,7 @@ type Emitter interface {
}

type PrometheusEmitter struct {
mutex sync.Mutex
gauges map[string]*prometheus.GaugeVec
counters map[string]*prometheus.CounterVec
registry prometheus.Registerer
Expand All @@ -36,6 +38,8 @@ func NewPrometheusEmitter() *PrometheusEmitter {
}

func (pe *PrometheusEmitter) EmitGauge(name string, value float64, labels map[string]string) {
pe.mutex.Lock()
defer pe.mutex.Unlock()
vec, exists := pe.gauges[name]
if !exists {
labelKeys := maps.Keys(labels)
Expand All @@ -47,11 +51,13 @@ func (pe *PrometheusEmitter) EmitGauge(name string, value float64, labels map[st
}

func (pe *PrometheusEmitter) EmitCounter(name string, value float64, labels map[string]string) {
pe.mutex.Lock()
defer pe.mutex.Unlock()
vec, exists := pe.counters[name]
if !exists {
labelKeys := maps.Keys(labels)
vec = prometheus.NewCounterVec(prometheus.CounterOpts{Name: name}, labelKeys)
prometheus.MustRegister(vec)
pe.registry.MustRegister(vec)
pe.counters[name] = vec
}
vec.With(labels).Add(value)
Expand Down
Loading