-
Notifications
You must be signed in to change notification settings - Fork 92
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
Rate limited errors #195
Open
joe-elliott
wants to merge
6
commits into
weaveworks:master
Choose a base branch
from
joe-elliott:rate-limited-errors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Rate limited errors #195
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b0e42ef
Added rate limited logger
joe-elliott 6427096
Added rate limited logger and used it
joe-elliott 0fa1c6c
Merge branch 'master' into rate-limited-errors
joe-elliott bc9ae8d
For high volume errors log everything on the same line
joe-elliott db20483
Fixed burst size on With logger
joe-elliott 54c934b
Use the same limiter for derived loggers
joe-elliott File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package logging | ||
|
||
import "golang.org/x/time/rate" | ||
|
||
type rateLimitedLogger struct { | ||
next Interface | ||
limiter *rate.Limiter | ||
} | ||
|
||
// NewRateLimitedLogger returns a logger.Interface that is limited to a number | ||
// of logs per second | ||
func NewRateLimitedLogger(logger Interface, logsPerSecond rate.Limit) Interface { | ||
return &rateLimitedLogger{ | ||
next: logger, | ||
limiter: rate.NewLimiter(logsPerSecond, 1), | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Debugf(format string, args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Debugf(format, args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Debugln(args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Debugln(args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Infof(format string, args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Infof(format, args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Infoln(args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Infoln(args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Errorf(format string, args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Errorf(format, args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Errorln(args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Errorln(args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Warnf(format string, args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Warnf(format, args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Warnln(args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Warnln(args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) WithField(key string, value interface{}) Interface { | ||
return &rateLimitedLogger{ | ||
next: l.next.WithField(key, value), | ||
limiter: rate.NewLimiter(l.limiter.Limit(), 0), | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) WithFields(f Fields) Interface { | ||
return &rateLimitedLogger{ | ||
next: l.next.WithFields(f), | ||
limiter: rate.NewLimiter(l.limiter.Limit(), 0), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package logging | ||
|
||
import ( | ||
"testing" | ||
|
||
"gotest.tools/assert" | ||
) | ||
|
||
type counterLogger struct { | ||
count int | ||
} | ||
|
||
func (c *counterLogger) Debugf(format string, args ...interface{}) { c.count++ } | ||
func (c *counterLogger) Debugln(args ...interface{}) { c.count++ } | ||
func (c *counterLogger) Infof(format string, args ...interface{}) { c.count++ } | ||
func (c *counterLogger) Infoln(args ...interface{}) { c.count++ } | ||
func (c *counterLogger) Warnf(format string, args ...interface{}) { c.count++ } | ||
func (c *counterLogger) Warnln(args ...interface{}) { c.count++ } | ||
func (c *counterLogger) Errorf(format string, args ...interface{}) { c.count++ } | ||
func (c *counterLogger) Errorln(args ...interface{}) { c.count++ } | ||
func (c *counterLogger) WithField(key string, value interface{}) Interface { | ||
return c | ||
} | ||
func (c *counterLogger) WithFields(Fields) Interface { | ||
return c | ||
} | ||
|
||
func TestRateLimitedLoggerLogs(t *testing.T) { | ||
c := &counterLogger{} | ||
r := NewRateLimitedLogger(c, 1) | ||
|
||
r.Errorln("asdf") | ||
assert.Equal(t, 1, c.count) | ||
} | ||
|
||
func TestRateLimitedLoggerLimits(t *testing.T) { | ||
c := &counterLogger{} | ||
r := NewRateLimitedLogger(c, 1) | ||
|
||
r.Errorln("asdf") | ||
r.Infoln("asdf") | ||
assert.Equal(t, 1, c.count) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we get a new limiter here?
Also, does it work with a burst size of zero?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the burst limit was wrong. Good catch. Added a test and fixed.
As far as the new limiter I figured calling
WithFields()
created a new logger that should have an independent rate limit, but I can go either way on this.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thing is, code often calls
WithFields()
on every operation, e.g. the example you gave in the description, or this one:common/middleware/grpc_logging.go
Line 30 in 61ffdd4
I expect the case you're trying to hit is where a high volume of logs from from lots of operations, so you would want the same rate-limit to be applied across them all. Try it!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. Thanks man 👍
Fixed.