Skip to content

Commit

Permalink
Merge pull request #2205 from Stephan14/fix_metrics_leak
Browse files Browse the repository at this point in the history
fix: prevent metrics leak when authenticate fails
  • Loading branch information
dnwe authored Apr 13, 2022
2 parents ed494ad + 08a8b06 commit 9904b37
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Broker struct {
responses chan *responsePromise
done chan bool

registeredMetrics []string
registeredMetrics map[string]struct{}

incomingByteRate metrics.Meter
requestRate metrics.Meter
Expand Down Expand Up @@ -1723,27 +1723,36 @@ func (b *Broker) registerMetrics() {
}

func (b *Broker) unregisterMetrics() {
for _, name := range b.registeredMetrics {
for name := range b.registeredMetrics {
b.conf.MetricRegistry.Unregister(name)
}
b.registeredMetrics = nil
}

func (b *Broker) registerMeter(name string) metrics.Meter {
nameForBroker := getMetricNameForBroker(name, b)
b.registeredMetrics = append(b.registeredMetrics, nameForBroker)
if b.registeredMetrics == nil {
b.registeredMetrics = map[string]struct{}{}
}
b.registeredMetrics[nameForBroker] = struct{}{}
return metrics.GetOrRegisterMeter(nameForBroker, b.conf.MetricRegistry)
}

func (b *Broker) registerHistogram(name string) metrics.Histogram {
nameForBroker := getMetricNameForBroker(name, b)
b.registeredMetrics = append(b.registeredMetrics, nameForBroker)
if b.registeredMetrics == nil {
b.registeredMetrics = map[string]struct{}{}
}
b.registeredMetrics[nameForBroker] = struct{}{}
return getOrRegisterHistogram(nameForBroker, b.conf.MetricRegistry)
}

func (b *Broker) registerCounter(name string) metrics.Counter {
nameForBroker := getMetricNameForBroker(name, b)
b.registeredMetrics = append(b.registeredMetrics, nameForBroker)
if b.registeredMetrics == nil {
b.registeredMetrics = map[string]struct{}{}
}
b.registeredMetrics[nameForBroker] = struct{}{}
return metrics.GetOrRegisterCounter(nameForBroker, b.conf.MetricRegistry)
}

Expand Down

0 comments on commit 9904b37

Please sign in to comment.