Skip to content

Commit

Permalink
Merge branch 'master' into query_region
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot[bot] authored Jan 27, 2025
2 parents 7d6da81 + 2a1f2dc commit ad46228
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
24 changes: 24 additions & 0 deletions client/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package metrics

import (
"sync"
"sync/atomic"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -26,6 +27,28 @@ var initialized int32
func init() {
initMetrics(prometheus.Labels{})
initCmdDurations()
initRegisteredConsumers()
}

var consumersInitializers = struct {
sync.Mutex
value []func()
}{}

// RegisterConsumer registers a consumer to be initialized when the metrics are (re)initialized
func RegisterConsumer(initConsumer func()) {
consumersInitializers.Lock()
defer consumersInitializers.Unlock()
consumersInitializers.value = append(consumersInitializers.value, initConsumer)
initConsumer()
}

func initRegisteredConsumers() {
consumersInitializers.Lock()
defer consumersInitializers.Unlock()
for _, initConsumer := range consumersInitializers.value {
initConsumer()
}
}

// InitAndRegisterMetrics initializes and registers the metrics manually.
Expand All @@ -34,6 +57,7 @@ func InitAndRegisterMetrics(constLabels prometheus.Labels) {
// init metrics with constLabels
initMetrics(constLabels)
initCmdDurations()
initRegisteredConsumers()
// register metrics
registerMetrics()
}
Expand Down
10 changes: 8 additions & 2 deletions client/pkg/circuitbreaker/circuit_breaker.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,18 @@ func NewCircuitBreaker(name string, st Settings) *CircuitBreaker {
cb.config = &st
cb.state = cb.newState(time.Now(), StateClosed)

metricName := replacer.Replace(name)
m.RegisterConsumer(func() {
registerMetrics(cb)
})
return cb
}

func registerMetrics(cb *CircuitBreaker) {
metricName := replacer.Replace(cb.name)
cb.successCounter = m.CircuitBreakerCounters.WithLabelValues(metricName, "success")
cb.errorCounter = m.CircuitBreakerCounters.WithLabelValues(metricName, "error")
cb.overloadCounter = m.CircuitBreakerCounters.WithLabelValues(metricName, "overload")
cb.fastFailCounter = m.CircuitBreakerCounters.WithLabelValues(metricName, "fast_fail")
return cb
}

// ChangeSettings changes the CircuitBreaker settings.
Expand Down

0 comments on commit ad46228

Please sign in to comment.