Skip to content

Commit

Permalink
loki/wal: fix panic for duplicate registered metrics (#5705)
Browse files Browse the repository at this point in the history
Signed-off-by: Paschalis Tsilias <[email protected]>
  • Loading branch information
tpaschalis authored Nov 3, 2023
1 parent 00cb90f commit 7817c22
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ Main (unreleased)

- Fixed a bug where UDP syslog messages were never processed (@joshuapare)

- Fix a bug where reloading the configuration of a `loki.write` component lead
to a panic. (@tpaschalis)

### Enhancements

- The `loki.write` WAL now has snappy compression enabled by default. (@thepalbi)
Expand Down
22 changes: 16 additions & 6 deletions component/common/loki/wal/watcher_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,23 @@ func NewWatcherMetrics(reg prometheus.Registerer) *WatcherMetrics {
}

if reg != nil {
reg.MustRegister(m.recordsRead)
reg.MustRegister(m.recordDecodeFails)
reg.MustRegister(m.droppedWriteNotifications)
reg.MustRegister(m.segmentRead)
reg.MustRegister(m.currentSegment)
reg.MustRegister(m.watchersRunning)
m.recordsRead = mustRegisterOrGet(reg, m.recordsRead).(*prometheus.CounterVec)
m.recordDecodeFails = mustRegisterOrGet(reg, m.recordDecodeFails).(*prometheus.CounterVec)
m.droppedWriteNotifications = mustRegisterOrGet(reg, m.droppedWriteNotifications).(*prometheus.CounterVec)
m.segmentRead = mustRegisterOrGet(reg, m.segmentRead).(*prometheus.CounterVec)
m.currentSegment = mustRegisterOrGet(reg, m.currentSegment).(*prometheus.GaugeVec)
m.watchersRunning = mustRegisterOrGet(reg, m.watchersRunning).(*prometheus.GaugeVec)
}

return m
}

func mustRegisterOrGet(reg prometheus.Registerer, c prometheus.Collector) prometheus.Collector {
if err := reg.Register(c); err != nil {
if are, ok := err.(prometheus.AlreadyRegisteredError); ok {
return are.ExistingCollector
}
panic(err)
}
return c
}

0 comments on commit 7817c22

Please sign in to comment.