Skip to content

Commit

Permalink
fix(static/metrics/instance): fix duplicate metrics registration pani…
Browse files Browse the repository at this point in the history
…c when recreating the instance (grafana#6608)

Signed-off-by: hainenber <dotronghai96@gmail.com>
(cherry picked from commit 7a61067)
  • Loading branch information
hainenber authored and rfratto committed Mar 5, 2024
1 parent 27ba56a commit 2fec7fc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ internal API changes are not present.

- Fix an issue where Loki could reject a batch of logs when structured metadata feature is used. (@thampiotr)

- Fix a duplicate metrics registration panic when recreating static
mode metric instance's write handler. (@rfratto, @hainenber)

v0.40.1 (2024-02-27)
--------------------

Expand Down
2 changes: 1 addition & 1 deletion pkg/metrics/instance/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ func (i *Instance) initialize(ctx context.Context, reg prometheus.Registerer, cf
return fmt.Errorf("error creating WAL: %w", err)
}

i.writeHandler = remote.NewWriteHandler(i.logger, i.reg, i.wal)
i.writeHandler = remote.NewWriteHandler(i.logger, reg, i.wal)

i.discovery, err = i.newDiscoveryManager(ctx, cfg)
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions pkg/metrics/instance/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ func TestInstance_Recreate(t *testing.T) {
cfg.RemoteFlushDeadline = time.Hour

logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))
inst, err := New(prometheus.NewRegistry(), cfg, walDir, logger)
currentReg := prometheus.NewRegistry()
inst, err := New(currentReg, cfg, walDir, logger)
require.NoError(t, err)

ctx, cancel := context.WithCancel(context.Background())
Expand All @@ -278,7 +279,7 @@ func TestInstance_Recreate(t *testing.T) {

// Recreate the instance, no panic should happen.
require.NotPanics(t, func() {
inst, err := New(prometheus.NewRegistry(), cfg, walDir, logger)
inst, err := New(currentReg, cfg, walDir, logger)
require.NoError(t, err)
runInstance(t, inst)

Expand Down

0 comments on commit 2fec7fc

Please sign in to comment.