Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use TimestampFunc for sampling #671

Open
crazy-pe opened this issue Apr 26, 2024 · 1 comment
Open

Use TimestampFunc for sampling #671

crazy-pe opened this issue Apr 26, 2024 · 1 comment

Comments

@crazy-pe
Copy link
Contributor

It is possible to set a custom timestamp function with the TimesampFunc global variable. This is very useful especially for unit tests or when using zerolog in accelerated simulations.

Unfortunately, the BurstSampler uses time.Now() and not the TimesampFunc function to sample logs. Is there a reason why you did so?

My suggestion is to use the function in the file sampler.go:

func (s *BurstSampler) inc() uint32 {
	now := TimestampFunc().UnixNano() // <- here instead of now := time.Now().UnixNano()
	resetAt := atomic.LoadInt64(&s.resetAt)
	var c uint32
	if now > resetAt {
		c = 1
		atomic.StoreUint32(&s.counter, c)
		newResetAt := now + s.Period.Nanoseconds()
		reset := atomic.CompareAndSwapInt64(&s.resetAt, resetAt, newResetAt)
		if !reset {
			// Lost the race with another goroutine trying to reset.
			c = atomic.AddUint32(&s.counter, 1)
		}
	} else {
		c = atomic.AddUint32(&s.counter, 1)
	}
	return c
}
@rs
Copy link
Owner

rs commented Apr 26, 2024

Feel free to submit a PR to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants