Skip to content

Commit

Permalink
frontend: Make PrometheusEmitter concurrency-safe
Browse files Browse the repository at this point in the history
http.Server.Serve creates a new goroutine for each incoming
connection so PrometheusEmitter needs to be concurrency-safe
when registering new collectors.
  • Loading branch information
Matthew Barnes authored and SudoBrendan committed Nov 14, 2024
1 parent f59cd06 commit 8221ab0
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 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,6 +51,8 @@ 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)
Expand Down

0 comments on commit 8221ab0

Please sign in to comment.