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

Question: Rate Limiting ? #969

Closed
midnightexigent opened this issue Jun 22, 2021 · 2 comments
Closed

Question: Rate Limiting ? #969

midnightexigent opened this issue Jun 22, 2021 · 2 comments

Comments

@midnightexigent
Copy link

Consider the following use case

zap.RedirectStdLog(aZapLogger)

where the std logger will be highly verbose. Since levels don't apply, all the logs will be written. Sampling is no good either since the messages will be different.

Ideally I would like to write something like this

zap.RedirectStdLog(aZapLogger.WithOption(someRateLimitingOption))

Basically saying: "I don't want more than N logs per M seconds"

Does zap provide some sort of primitive to accomplish this, that I missed while reading the docs ?

@midnightexigent
Copy link
Author

answering my own question:

I ended up implementing a zapcore.Core using the rate package

func main(){
	zap.RedirectStdLog(logger.WithOptions(zap.WrapCore(func(c zapcore.Core) zapcore.Core {
		return &rateZapCore{
			Core:    c,
			limiter: rate.NewLimiter(rate.Every(time.Second), 5),
		}
	})))
}

type rateZapCore struct {
	zapcore.Core
	limiter *rate.Limiter
}

func (rzc *rateZapCore) Check(e zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
	if rzc.limiter.Allow() {
		return rzc.Core.Check(e, ce)
	}
	return ce
}

@prashantv
Copy link
Collaborator

The sampler is the closest related feature, but as you mentioned, it doesn't help with the messages are variable, as it's used to throttle specific message/level combinations.

The sampler could be extended to accept a custom hash function, but what you're using seems like a good simple option.

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

No branches or pull requests

2 participants