-
Notifications
You must be signed in to change notification settings - Fork 83
in_prometheus: extract the error log throttling into LogThrottle #264
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
Merged
Merged
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 @@ | ||
| require 'fluent/clock' | ||
|
|
||
| module Fluent | ||
| module Plugin | ||
| module Prometheus | ||
| # Suppresses the repeated log for the same key within the interval. | ||
| # in_prometheus uses it, with an instance of its own. The key decides | ||
| # what is throttled, an error scope for now. The fingerprint tells the | ||
| # logs of a key apart: one which differs from the last is not suppressed. | ||
| class LogThrottle | ||
| Entry = Struct.new(:time, :fingerprint, :suppressed) | ||
|
|
||
| def initialize(interval) | ||
| @interval = interval | ||
| @mutex = Mutex.new | ||
| # one entry per key, so this does not grow without a limit | ||
| @entries = {} | ||
| end | ||
|
|
||
| # Returns [emit, suppressed_count]. emit is true for the first log of a | ||
| # key, for a new fingerprint, and after the interval has passed. | ||
| # suppressed_count is how many logs were suppressed since the last one | ||
| # was emitted. | ||
| def check(key, fingerprint) | ||
| return [true, 0] if @interval <= 0 | ||
|
|
||
| @mutex.synchronize do | ||
| now = Fluent::Clock.now | ||
| last = @entries[key] | ||
| if last.nil? || last.fingerprint != fingerprint || (now - last.time) >= @interval | ||
| suppressed = (last && last.fingerprint == fingerprint) ? last.suppressed : 0 | ||
| @entries[key] = Entry.new(now, fingerprint, 0) | ||
| [true, suppressed] | ||
| else | ||
| last.suppressed += 1 | ||
| [false, 0] | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or 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 hidden or 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,108 @@ | ||
| require 'spec_helper' | ||
| require 'fluent/plugin/prometheus/log_throttle' | ||
|
|
||
| describe Fluent::Plugin::Prometheus::LogThrottle do | ||
| # Fluent::Clock.now is monotonic, so a Hash is enough to fake it | ||
| let(:clock) { { now: 1000.0 } } | ||
| let(:interval) { 3600 } | ||
| # in_prometheus builds it out of an error | ||
| let(:fingerprint) { [RuntimeError, 'a'] } | ||
| subject(:throttle) { described_class.new(interval) } | ||
|
|
||
| before do | ||
| allow(Fluent::Clock).to receive(:now) { clock[:now] } | ||
| end | ||
|
|
||
| describe '#check' do | ||
| it 'emits on the first occurrence of a key' do | ||
| emit, suppressed = throttle.check(:foo, fingerprint) | ||
| expect(emit).to be true | ||
| expect(suppressed).to eq(0) | ||
| end | ||
|
|
||
| it 'suppresses the same key within the interval' do | ||
| throttle.check(:foo, fingerprint) | ||
| clock[:now] += interval - 1 | ||
| emit, _ = throttle.check(:foo, fingerprint) | ||
| expect(emit).to be false | ||
| end | ||
|
|
||
| it 'emits again once the interval has elapsed' do | ||
| throttle.check(:foo, fingerprint) | ||
| clock[:now] += interval | ||
| emit, _ = throttle.check(:foo, fingerprint) | ||
| expect(emit).to be true | ||
| end | ||
|
|
||
| it 'reports how many occurrences were suppressed in the meantime' do | ||
| throttle.check(:foo, fingerprint) # emits, suppressed=0 | ||
| 2.times { throttle.check(:foo, fingerprint) } # suppressed 1, then 2 | ||
| clock[:now] += interval | ||
| emit, suppressed = throttle.check(:foo, fingerprint) | ||
| expect(emit).to be true | ||
| expect(suppressed).to eq(2) | ||
| end | ||
|
|
||
| it 'resets the suppressed count after emitting' do | ||
| throttle.check(:foo, fingerprint) | ||
| 2.times { throttle.check(:foo, fingerprint) } | ||
| clock[:now] += interval | ||
| throttle.check(:foo, fingerprint) # emits with suppressed=2 | ||
| clock[:now] += interval | ||
| _, suppressed = throttle.check(:foo, fingerprint) | ||
| expect(suppressed).to eq(0) | ||
| end | ||
|
|
||
| it 'keeps a separate slot per key' do | ||
| expect(throttle.check(:foo, fingerprint).first).to be true | ||
| expect(throttle.check(:bar, fingerprint).first).to be true | ||
| end | ||
|
|
||
| it 'emits immediately when the fingerprint changes within the interval' do | ||
| expect(throttle.check(:foo, [RuntimeError, 'a']).first).to be true | ||
| expect(throttle.check(:foo, [RuntimeError, 'b']).first).to be true | ||
| end | ||
|
|
||
| # the caller makes a new fingerprint for each event, so it has to be | ||
| # compared by value, not by object identity | ||
| it 'suppresses an equal fingerprint given as a different object' do | ||
| expect(throttle.check(:foo, [RuntimeError, 'a']).first).to be true | ||
| expect(throttle.check(:foo, [RuntimeError, 'a']).first).to be false | ||
| end | ||
|
|
||
| it 'does not carry the suppressed count across a fingerprint change' do | ||
| throttle.check(:foo, [RuntimeError, 'a']) | ||
| 2.times { throttle.check(:foo, [RuntimeError, 'a']) } | ||
| emit, suppressed = throttle.check(:foo, [RuntimeError, 'b']) | ||
| expect(emit).to be true | ||
| expect(suppressed).to eq(0) | ||
| end | ||
|
|
||
| context 'when interval is zero' do | ||
| let(:interval) { 0 } | ||
|
|
||
| it 'always emits without consulting the clock' do | ||
| expect(Fluent::Clock).not_to receive(:now) | ||
| 3.times do | ||
| emit, suppressed = throttle.check(:foo, fingerprint) | ||
| expect(emit).to be true | ||
| expect(suppressed).to eq(0) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| context 'when interval is negative' do | ||
| let(:interval) { -1 } | ||
|
|
||
| it 'always emits' do | ||
| expect(throttle.check(:foo, fingerprint).first).to be true | ||
| expect(throttle.check(:foo, fingerprint).first).to be true | ||
| end | ||
| end | ||
|
|
||
| it 'serializes concurrent checks for the same key into a single emission' do | ||
| results = 10.times.map { Thread.new { throttle.check(:foo, fingerprint).first } }.map(&:value) | ||
| expect(results.count(true)).to eq(1) | ||
| end | ||
| end | ||
| end |
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.
Uh oh!
There was an error while loading. Please reload this page.