BasicSampler prints first message (fix #104)

This commit is contained in:
Olivier Poitrey 2018-09-16 15:53:13 -07:00
parent b6f076edc8
commit 1dde226d45
3 changed files with 12 additions and 5 deletions

View File

@ -398,7 +398,7 @@ func TestSampling(t *testing.T) {
log.Log().Int("i", 2).Msg("") log.Log().Int("i", 2).Msg("")
log.Log().Int("i", 3).Msg("") log.Log().Int("i", 3).Msg("")
log.Log().Int("i", 4).Msg("") log.Log().Int("i", 4).Msg("")
if got, want := decodeIfBinaryToString(out.Bytes()), "{\"i\":2}\n{\"i\":4}\n"; got != want { if got, want := decodeIfBinaryToString(out.Bytes()), "{\"i\":1}\n{\"i\":3}\n"; got != want {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want) t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
} }
} }
@ -527,7 +527,7 @@ type loggableError struct {
} }
func (l loggableError) MarshalZerologObject(e *Event) { func (l loggableError) MarshalZerologObject(e *Event) {
e.Str("message", l.error.Error() + ": loggableError") e.Str("message", l.error.Error()+": loggableError")
} }
func TestErrorMarshalFunc(t *testing.T) { func TestErrorMarshalFunc(t *testing.T) {
@ -549,7 +549,7 @@ func TestErrorMarshalFunc(t *testing.T) {
// test overriding the ErrorMarshalFunc // test overriding the ErrorMarshalFunc
originalErrorMarshalFunc := ErrorMarshalFunc originalErrorMarshalFunc := ErrorMarshalFunc
defer func(){ defer func() {
ErrorMarshalFunc = originalErrorMarshalFunc ErrorMarshalFunc = originalErrorMarshalFunc
}() }()

View File

@ -47,7 +47,7 @@ type BasicSampler struct {
// Sample implements the Sampler interface. // Sample implements the Sampler interface.
func (s *BasicSampler) Sample(lvl Level) bool { func (s *BasicSampler) Sample(lvl Level) bool {
c := atomic.AddUint32(&s.counter, 1) c := atomic.AddUint32(&s.counter, 1)
return c%s.N == 0 return c%s.N == s.N-1
} }
// BurstSampler lets Burst events pass per Period then pass the decision to // BurstSampler lets Burst events pass per Period then pass the decision to

View File

@ -15,7 +15,14 @@ var samplers = []struct {
wantMax int wantMax int
}{ }{
{ {
"BasicSampler", "BasicSampler_1",
func() Sampler {
return &BasicSampler{N: 1}
},
100, 100, 100,
},
{
"BasicSampler_5",
func() Sampler { func() Sampler {
return &BasicSampler{N: 5} return &BasicSampler{N: 5}
}, },