mirror of https://github.com/prometheus/prometheus
Merge pull request #15562 from tjhop/fix/dedupe-logger-concurrent-map-panic
fix(deduper): use ptr to sync.RWMutex, fix panic during concurrent usepull/13423/merge
commit
a70cba84a6
|
@ -33,7 +33,7 @@ type Deduper struct {
|
||||||
next *slog.Logger
|
next *slog.Logger
|
||||||
repeat time.Duration
|
repeat time.Duration
|
||||||
quit chan struct{}
|
quit chan struct{}
|
||||||
mtx sync.RWMutex
|
mtx *sync.RWMutex
|
||||||
seen map[string]time.Time
|
seen map[string]time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,6 +43,7 @@ func Dedupe(next *slog.Logger, repeat time.Duration) *Deduper {
|
||||||
next: next,
|
next: next,
|
||||||
repeat: repeat,
|
repeat: repeat,
|
||||||
quit: make(chan struct{}),
|
quit: make(chan struct{}),
|
||||||
|
mtx: new(sync.RWMutex),
|
||||||
seen: map[string]time.Time{},
|
seen: map[string]time.Time{},
|
||||||
}
|
}
|
||||||
go d.run()
|
go d.run()
|
||||||
|
@ -88,6 +89,7 @@ func (d *Deduper) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||||
repeat: d.repeat,
|
repeat: d.repeat,
|
||||||
quit: d.quit,
|
quit: d.quit,
|
||||||
seen: d.seen,
|
seen: d.seen,
|
||||||
|
mtx: d.mtx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,6 +105,7 @@ func (d *Deduper) WithGroup(name string) slog.Handler {
|
||||||
repeat: d.repeat,
|
repeat: d.repeat,
|
||||||
quit: d.quit,
|
quit: d.quit,
|
||||||
seen: d.seen,
|
seen: d.seen,
|
||||||
|
mtx: d.mtx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,3 +56,27 @@ func TestDedupe(t *testing.T) {
|
||||||
}
|
}
|
||||||
require.Len(t, lines, 2)
|
require.Len(t, lines, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDedupeConcurrent(t *testing.T) {
|
||||||
|
d := Dedupe(promslog.New(&promslog.Config{}), 250*time.Millisecond)
|
||||||
|
dlog := slog.New(d)
|
||||||
|
defer d.Stop()
|
||||||
|
|
||||||
|
concurrentWriteFunc := func() {
|
||||||
|
go func() {
|
||||||
|
dlog1 := dlog.With("writer", 1)
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
dlog1.With("foo", "bar").Info("test", "hello", "world")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
dlog2 := dlog.With("writer", 2)
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
dlog2.With("foo", "bar").Info("test", "hello", "world")
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
require.NotPanics(t, func() { concurrentWriteFunc() })
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue