From 76d7bce1b177ee4e9c4f69cfe0f0e8ee5acdecb5 Mon Sep 17 00:00:00 2001 From: Trae Robrock Date: Sun, 7 Jul 2013 13:21:12 -0700 Subject: [PATCH 1/5] Convert from rest-client to faraday --- lib/slate/graph.rb | 11 +++++++++-- slate.gemspec | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/slate/graph.rb b/lib/slate/graph.rb index ae406c0..1357b37 100644 --- a/lib/slate/graph.rb +++ b/lib/slate/graph.rb @@ -1,5 +1,5 @@ require 'cgi' -require 'rest-client' +require 'faraday' module Slate class Graph @@ -65,11 +65,18 @@ def url(format=:png) # download(:json) # # => '{"targets":[]}' def download(format=:png) - RestClient.get url(format) + connection.get(url(format)).body end private + def connection + @connection ||= Faraday.new do |faraday| + faraday.options[:timeout] = 10 + faraday.adapter Faraday.default_adapter + end + end + def url_options options = [] options += @targets.map { |t| ["target", t.to_s] } diff --git a/slate.gemspec b/slate.gemspec index 2eda40a..7cbc156 100644 --- a/slate.gemspec +++ b/slate.gemspec @@ -17,7 +17,7 @@ Gem::Specification.new do |gem| gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.require_paths = ["lib"] - gem.add_dependency "rest-client", "~> 1.6.7" + gem.add_dependency "faraday", "~> 0.8" gem.add_dependency "json", "~> 1.7.5" gem.add_dependency "jruby-openssl" if RUBY_PLATFORM == 'java' gem.add_dependency "treetop", "~> 1.4.12" From b1fe7ebed7ef210dc56acefaf2ae36d82967deff Mon Sep 17 00:00:00 2001 From: Trae Robrock Date: Sun, 7 Jul 2013 13:23:26 -0700 Subject: [PATCH 2/5] Upgrade json --- slate.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slate.gemspec b/slate.gemspec index 7cbc156..72656b2 100644 --- a/slate.gemspec +++ b/slate.gemspec @@ -18,7 +18,7 @@ Gem::Specification.new do |gem| gem.require_paths = ["lib"] gem.add_dependency "faraday", "~> 0.8" - gem.add_dependency "json", "~> 1.7.5" + gem.add_dependency "json", "~> 1.8" gem.add_dependency "jruby-openssl" if RUBY_PLATFORM == 'java' gem.add_dependency "treetop", "~> 1.4.12" From 77d57262a24c9b7f14715da021d89cfc628b2a7c Mon Sep 17 00:00:00 2001 From: Trae Robrock Date: Sun, 7 Jul 2013 13:24:25 -0700 Subject: [PATCH 3/5] Loosen restriction on treetop gem --- slate.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/slate.gemspec b/slate.gemspec index 72656b2..2712da6 100644 --- a/slate.gemspec +++ b/slate.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |gem| gem.add_dependency "faraday", "~> 0.8" gem.add_dependency "json", "~> 1.8" gem.add_dependency "jruby-openssl" if RUBY_PLATFORM == 'java' - gem.add_dependency "treetop", "~> 1.4.12" + gem.add_dependency "treetop", "~> 1.4" gem.add_development_dependency "rake" gem.add_development_dependency "rspec" From 47437b745de87413058a0ac60e307fc19cafe234 Mon Sep 17 00:00:00 2001 From: Trae Robrock Date: Sun, 7 Jul 2013 13:39:35 -0700 Subject: [PATCH 4/5] Catch faraday timeout error and map it to a slate timeout error --- lib/slate.rb | 1 + lib/slate/error.rb | 5 +++++ lib/slate/graph.rb | 2 ++ spec/lib/slate/graph_spec.rb | 13 +++++++++++++ 4 files changed, 21 insertions(+) create mode 100644 lib/slate/error.rb diff --git a/lib/slate.rb b/lib/slate.rb index 469b6eb..d82020d 100644 --- a/lib/slate.rb +++ b/lib/slate.rb @@ -1,4 +1,5 @@ require "slate/version" +require "slate/error" require "slate/client" require "slate/target" require "slate/graph" diff --git a/lib/slate/error.rb b/lib/slate/error.rb new file mode 100644 index 0000000..d97becf --- /dev/null +++ b/lib/slate/error.rb @@ -0,0 +1,5 @@ +module Slate + module Error + class TimeoutError < StandardError ; end + end +end diff --git a/lib/slate/graph.rb b/lib/slate/graph.rb index 1357b37..14f46f8 100644 --- a/lib/slate/graph.rb +++ b/lib/slate/graph.rb @@ -66,6 +66,8 @@ def url(format=:png) # # => '{"targets":[]}' def download(format=:png) connection.get(url(format)).body + rescue Faraday::Error::TimeoutError + raise Slate::Error::TimeoutError end private diff --git a/spec/lib/slate/graph_spec.rb b/spec/lib/slate/graph_spec.rb index 2757a8e..157b446 100644 --- a/spec/lib/slate/graph_spec.rb +++ b/spec/lib/slate/graph_spec.rb @@ -124,6 +124,19 @@ graph.download(:json).should eq(@json_stub) graph.download(:svg).should eq(@svg_stub) end + + it "should respect the configured timeout" do + stub_request(:get, "http://graphite/render?format=png&target=app.server01.timeout"). + to_timeout + + target = Slate::Target.build("app.server01.timeout") + graph = Slate::Graph.new(@client) + graph << target + + expect { + graph.download(:png).should eq(@png_stub) + }.to raise_error(Slate::Error::TimeoutError) + end end def stub_download(format, body="") From 4302eb38d64fbe8dab2dcdfe47f86ead058d8b05 Mon Sep 17 00:00:00 2001 From: Trae Robrock Date: Sun, 7 Jul 2013 13:45:25 -0700 Subject: [PATCH 5/5] Configurable timeout for downloading graph data --- README.md | 1 + lib/slate/client.rb | 2 +- lib/slate/graph.rb | 2 +- spec/lib/slate_spec.rb | 8 ++++++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 69c121e..2451a3f 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ Configure the Slate client ```ruby client = Slate.configure do |config| config.endpoint = "http://your.graphite-server.com" + config.timeout = 30 # In seconds (default: 10) end ``` diff --git a/lib/slate/client.rb b/lib/slate/client.rb index 26a2f69..df40d29 100644 --- a/lib/slate/client.rb +++ b/lib/slate/client.rb @@ -1,6 +1,6 @@ module Slate class Client # Public: Gets/Sets the URL endpoint of the Graphite server. - attr_accessor :endpoint + attr_accessor :endpoint, :timeout end end diff --git a/lib/slate/graph.rb b/lib/slate/graph.rb index 14f46f8..bb15fdd 100644 --- a/lib/slate/graph.rb +++ b/lib/slate/graph.rb @@ -74,7 +74,7 @@ def download(format=:png) def connection @connection ||= Faraday.new do |faraday| - faraday.options[:timeout] = 10 + faraday.options[:timeout] = @client.timeout || 10 faraday.adapter Faraday.default_adapter end end diff --git a/spec/lib/slate_spec.rb b/spec/lib/slate_spec.rb index 990686d..3798fbb 100644 --- a/spec/lib/slate_spec.rb +++ b/spec/lib/slate_spec.rb @@ -8,4 +8,12 @@ client.endpoint.should == "http://graphite" end + + it "should be able to configure the request timeout" do + client = Slate.configure do |c| + c.timeout = 30 + end + + client.timeout.should == 30 + end end