Skip to content

Commit

Permalink
Discard counters for out-of-bounds levels
Browse files Browse the repository at this point in the history
Previously this just crashed.
  • Loading branch information
thockin committed Jun 30, 2021
1 parent 120b08c commit 605a517
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 8 additions & 6 deletions zapcore/sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,14 @@ func (s *sampler) Check(ent Entry, ce *CheckedEntry) *CheckedEntry {
return ce
}

counter := s.counts.get(ent.Level, ent.Message)
n := counter.IncCheckReset(ent.Time, s.tick)
if n > s.first && (n-s.first)%s.thereafter != 0 {
s.hook(ent, LogDropped)
return ce
if ent.Level >= _minLevel && ent.Level <= _maxLevel {
counter := s.counts.get(ent.Level, ent.Message)
n := counter.IncCheckReset(ent.Time, s.tick)
if n > s.first && (n-s.first)%s.thereafter != 0 {
s.hook(ent, LogDropped)
return ce
}
s.hook(ent, LogSampled)
}
s.hook(ent, LogSampled)
return s.Core.Check(ent, ce)
}
6 changes: 6 additions & 0 deletions zapcore/sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,9 @@ func TestSamplerRaces(t *testing.T) {
close(start)
wg.Wait()
}

func TestSamplerBounds(t *testing.T) {
// Prove that out-of-bounds levels don't crash.
sampler, _ := fakeSampler(DebugLevel-1, time.Minute, 2, 3)
writeSequence(sampler, 1, DebugLevel-1)
}

0 comments on commit 605a517

Please sign in to comment.