Skip to content

Commit

Permalink
Merge pull request #48 from braintree/ssgelm_rescue-haproxy
Browse files Browse the repository at this point in the history
Rescue Metric::HaproxyBackendsHealth current_health when haproxy isn't running
  • Loading branch information
ssgelm authored Jan 17, 2020
2 parents afadc2a + 2aff33c commit 6f12be1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/litmus_paper/metric/haproxy_backends_health.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def current_health
.inject(0) { |sum, server| sum + server["weight"].to_f }

((up_weight / total_weight) * @weight).to_i

rescue Timeout::Error
LitmusPaper.logger.info("HAProxy available check timed out for #{@cluster}")
0
rescue => e
LitmusPaper.logger.info("HAProxy available check failed for #{@cluster} with #{e.message}")
0
end

def stats
Expand Down
10 changes: 10 additions & 0 deletions spec/litmus_paper/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ def app
last_response.body.should include('Forcing health')
last_response.body.should include('88')
end

it "returns successfully if a haproxy backend socket is unreachable" do
LitmusPaper.services['test'] = LitmusPaper::Service.new('test', [], [LitmusPaper::Metric::HaproxyBackendsHealth.new(100, "/tmp/non-existant-socketfile", "haproxy")])

get "/"

last_response.status.should == 200
last_response.body.should include('test')
last_response.body.should include('0')
end
end

describe "POST /up" do
Expand Down
5 changes: 5 additions & 0 deletions spec/litmus_paper/metric/haproxy_backends_health_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
health = LitmusPaper::Metric::HaproxyBackendsHealth.new(50, "/tmp/stub-haproxy-stats", "yellow_cluster")
health.current_health.should == 16
end

it "should return a health of 0 if haproxy is not running" do
health = LitmusPaper::Metric::HaproxyBackendsHealth.new(100, "/tmp/non-existant-socketfile", "red_cluster")
health.current_health.should == 0
end
end

describe "#to_s" do
Expand Down

0 comments on commit 6f12be1

Please sign in to comment.