-
-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0874ca8
commit 7a6cc87
Showing
12 changed files
with
341 additions
and
19 deletions.
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
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,75 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sentry | ||
class BackpressureMonitor | ||
include LoggingHelper | ||
|
||
DEFAULT_INTERVAL = 10 | ||
MAX_DOWNSAMPLE_FACTOR = 10 | ||
|
||
def initialize(configuration, client, interval: DEFAULT_INTERVAL) | ||
@interval = interval | ||
@client = client | ||
@logger = configuration.logger | ||
|
||
@thread = nil | ||
@exited = false | ||
|
||
@healthy = true | ||
@downsample_factor = 0 | ||
end | ||
|
||
def healthy? | ||
ensure_thread | ||
@healthy | ||
end | ||
|
||
def downsample_factor | ||
ensure_thread | ||
@downsample_factor | ||
end | ||
|
||
def run | ||
check_health | ||
set_downsample_factor | ||
end | ||
|
||
def check_health | ||
@healthy = !(@client.transport.any_rate_limited? || Sentry.background_worker&.full?) | ||
end | ||
|
||
def set_downsample_factor | ||
if @healthy | ||
log_debug("[BackpressureMonitor] health check positive, reverting to normal sampling") if @downsample_factor.positive? | ||
@downsample_factor = 0 | ||
else | ||
@downsample_factor += 1 if @downsample_factor < MAX_DOWNSAMPLE_FACTOR | ||
log_debug("[BackpressureMonitor] health check negative, downsampling with a factor of #{@downsample_factor}") | ||
end | ||
end | ||
|
||
def kill | ||
log_debug("[BackpressureMonitor] killing monitor") | ||
|
||
@exited = true | ||
@thread&.kill | ||
end | ||
|
||
private | ||
|
||
def ensure_thread | ||
return if @exited | ||
return if @thread&.alive? | ||
|
||
@thread = Thread.new do | ||
loop do | ||
sleep(@interval) | ||
run | ||
end | ||
end | ||
rescue ThreadError | ||
log_debug("[BackpressureMonitor] Thread creation failed") | ||
@exited = true | ||
end | ||
end | ||
end |
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
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,117 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Sentry::BackpressureMonitor do | ||
let(:string_io) { StringIO.new } | ||
|
||
before do | ||
perform_basic_setup do |config| | ||
config.enable_backpressure_handling = true | ||
config.logger = Logger.new(string_io) | ||
end | ||
end | ||
|
||
let(:configuration) { Sentry.configuration } | ||
let(:client) { Sentry.get_current_client } | ||
let(:transport) { client.transport } | ||
let(:background_worker) { Sentry.background_worker } | ||
|
||
subject { described_class.new(configuration, client) } | ||
|
||
describe '#healthy?' do | ||
it 'returns true by default' do | ||
expect(subject.healthy?).to eq(true) | ||
end | ||
|
||
it 'returns false when unhealthy' do | ||
expect(transport).to receive(:any_rate_limited?).and_return(true) | ||
subject.run | ||
expect(subject.healthy?).to eq(false) | ||
end | ||
|
||
it 'spawns new thread' do | ||
expect { subject.healthy? }.to change { Thread.list.count }.by(1) | ||
expect(subject.instance_variable_get(:@thread)).to be_a(Thread) | ||
end | ||
|
||
it 'spawns only one thread' do | ||
expect { subject.healthy? }.to change { Thread.list.count }.by(1) | ||
thread = subject.instance_variable_get(:@thread) | ||
expect(thread).to receive(:alive?).and_return(true) | ||
expect { subject.healthy? }.to change { Thread.list.count }.by(0) | ||
end | ||
|
||
context 'when thread creation fails' do | ||
before do | ||
expect(Thread).to receive(:new).and_raise(ThreadError) | ||
end | ||
|
||
it 'does not create new thread' do | ||
expect { subject.healthy? }.to change { Thread.list.count }.by(0) | ||
end | ||
|
||
it 'returns true (the default)' do | ||
expect(subject.healthy?).to eq(true) | ||
end | ||
|
||
it 'logs error' do | ||
subject.healthy? | ||
expect(string_io.string).to match(/\[BackpressureMonitor\] Thread creation failed/) | ||
end | ||
end | ||
|
||
context 'when killed' do | ||
before { subject.kill } | ||
|
||
it 'returns true (the default)' do | ||
expect(subject.healthy?).to eq(true) | ||
end | ||
|
||
it 'does not create new thread' do | ||
expect(Thread).not_to receive(:new) | ||
expect { subject.healthy? }.to change { Thread.list.count }.by(0) | ||
end | ||
end | ||
end | ||
|
||
# thread behavior is tested above in healthy? | ||
describe '#downsample_factor' do | ||
it 'returns 0 by default' do | ||
expect(subject.downsample_factor).to eq(0) | ||
end | ||
|
||
it 'increases when unhealthy upto limit' do | ||
allow(transport).to receive(:any_rate_limited?).and_return(true) | ||
|
||
10.times do |i| | ||
subject.run | ||
expect(subject.downsample_factor).to eq(i + 1) | ||
end | ||
|
||
2.times do |i| | ||
subject.run | ||
expect(subject.downsample_factor).to eq(10) | ||
end | ||
end | ||
end | ||
|
||
describe '#run' do | ||
it 'logs behavior' do | ||
allow(background_worker).to receive(:full?).and_return(true) | ||
subject.run | ||
expect(string_io.string).to match(/\[BackpressureMonitor\] health check negative, downsampling with a factor of 1/) | ||
|
||
allow(background_worker).to receive(:full?).and_return(false) | ||
subject.run | ||
expect(string_io.string).to match(/\[BackpressureMonitor\] health check positive, reverting to normal sampling/) | ||
end | ||
end | ||
|
||
describe '#kill' do | ||
it 'kills the thread and logs a message' do | ||
subject.healthy? | ||
expect(subject.instance_variable_get(:@thread)).to receive(:kill) | ||
subject.kill | ||
expect(string_io.string).to match(/\[BackpressureMonitor\] killing monitor/) | ||
end | ||
end | ||
end |
Oops, something went wrong.