slate 1.0.3 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -27,8 +27,9 @@ Or install it yourself as:
27
27
  Configure the Slate client
28
28
 
29
29
  ```ruby
30
- Slate.configure do |config|
30
+ client = Slate.configure do |config|
31
31
  config.endpoint = "http://your.graphite-server.com"
32
+ config.timeout = 30 # In seconds (default: 10)
32
33
  end
33
34
  ```
34
35
 
@@ -37,7 +38,7 @@ end
37
38
  To build a basic graph
38
39
 
39
40
  ```ruby
40
- graph = Slate::Graph.new
41
+ graph = Slate::Graph.new(client)
41
42
  graph << Slate::Target.build("stats.web01.load")
42
43
 
43
44
  puts graph.url
@@ -47,7 +48,7 @@ puts graph.download(:json)
47
48
  Adjust the timeframe of the graph
48
49
 
49
50
  ```ruby
50
- graph = Slate::Graph.new
51
+ graph = Slate::Graph.new(client)
51
52
  graph.from = "-1w"
52
53
  graph.until = "-1d"
53
54
  graph << Slate::Target.build("stats.web01.load")
@@ -56,7 +57,7 @@ graph << Slate::Target.build("stats.web01.load")
56
57
  Use functions
57
58
 
58
59
  ```ruby
59
- graph = Slate::Graph.new
60
+ graph = Slate::Graph.new(client)
60
61
 
61
62
  graph << Slate::Target.build("stats.web01.load") do |target|
62
63
  target.add_function :sum
@@ -72,7 +73,7 @@ Slate also provides a text interface for building targets, this can be useful if
72
73
  This text interface also support being able to pass targets as arguments to functions, like you need for the `asPercentOf` function.
73
74
 
74
75
  ```ruby
75
- graph = Slate::Graph.new
76
+ graph = Slate::Graph.new(client)
76
77
 
77
78
  target = <<-SLATE
78
79
  "stats.web1.load" {
@@ -91,7 +92,7 @@ Full test cases for different things this syntax supports are here: [Parser Spec
91
92
  Slate supports things call Calculations, which take in graphite data and boil them down to single numbers, this can be useful if you wanted to calculate the average load over the week for all your servers.
92
93
 
93
94
  ```ruby
94
- graph = Slate::Graph.new
95
+ graph = Slate::Graph.new(client)
95
96
  graph << Slate::Target.build("stats.web01.load")
96
97
 
97
98
  p Slate::Calculation::Mean.new(graph).result
@@ -113,3 +114,5 @@ All the possible calculation classes are [here](https://github.com/trobrock/slat
113
114
  Trae Robrock (https://github.com/trobrock)
114
115
 
115
116
  Andrew Katz (https://github.com/andrewkatz)
117
+
118
+ David Sennerlöv (https://github.com/dsennerlov)
@@ -1,4 +1,5 @@
1
1
  require "slate/version"
2
+ require "slate/error"
2
3
  require "slate/client"
3
4
  require "slate/target"
4
5
  require "slate/graph"
@@ -1,6 +1,6 @@
1
1
  module Slate
2
2
  class Client
3
3
  # Public: Gets/Sets the URL endpoint of the Graphite server.
4
- attr_accessor :endpoint
4
+ attr_accessor :endpoint, :timeout
5
5
  end
6
6
  end
@@ -0,0 +1,5 @@
1
+ module Slate
2
+ module Error
3
+ class TimeoutError < StandardError ; end
4
+ end
5
+ end
@@ -1,5 +1,5 @@
1
1
  require 'cgi'
2
- require 'rest-client'
2
+ require 'faraday'
3
3
 
4
4
  module Slate
5
5
  class Graph
@@ -26,6 +26,14 @@ module Slate
26
26
  @targets = options[:targets] || []
27
27
  end
28
28
 
29
+ # Public: Adds a target to a graph.
30
+ #
31
+ # target - A Slate::Target instance.
32
+ #
33
+ # Examples
34
+ #
35
+ # graph = Slate::Graph.new(client)
36
+ # graph << Slate::Target.build("test.metric")
29
37
  def <<(target)
30
38
  @targets << target
31
39
  end
@@ -48,12 +56,29 @@ module Slate
48
56
  "#{@client.endpoint}/render?#{params(options)}"
49
57
  end
50
58
 
59
+ # Public: Retrieve the data from the graphite server in the requested format.
60
+ #
61
+ # format - The format of the data to return, as a Symbol (default: :png).
62
+ #
63
+ # Examples
64
+ #
65
+ # download(:json)
66
+ # # => '{"targets":[]}'
51
67
  def download(format=:png)
52
- RestClient.get url(format)
68
+ connection.get(url(format)).body
69
+ rescue Faraday::Error::TimeoutError
70
+ raise Slate::Error::TimeoutError
53
71
  end
54
72
 
55
73
  private
56
74
 
75
+ def connection
76
+ @connection ||= Faraday.new do |faraday|
77
+ faraday.options[:timeout] = @client.timeout || 10
78
+ faraday.adapter Faraday.default_adapter
79
+ end
80
+ end
81
+
57
82
  def url_options
58
83
  options = []
59
84
  options += @targets.map { |t| ["target", t.to_s] }
@@ -1,56 +1,33 @@
1
1
  module Slate
2
2
  module SlateTree
3
3
  class Target < Treetop::Runtime::SyntaxNode
4
- def type
5
- :target
6
- end
7
-
8
4
  def text_value
9
5
  elements.detect{ |e| e.is_a? String }.text_value
10
6
  end
11
7
  end
12
8
 
13
9
  class Function < Treetop::Runtime::SyntaxNode
14
- def type
15
- :function
16
- end
17
-
18
10
  def text_value
19
11
  elements.detect{ |e| e.is_a? Token }.text_value
20
12
  end
21
13
  end
22
14
 
23
15
  class Token < Treetop::Runtime::SyntaxNode
24
- def type
25
- :token
26
- end
27
16
  end
28
17
 
29
18
  class Argument < Treetop::Runtime::SyntaxNode
30
- def type
31
- :argument
32
- end
33
-
34
19
  def text_value
35
20
  elements.first.text_value
36
21
  end
37
22
  end
38
23
 
39
24
  class String < Treetop::Runtime::SyntaxNode
40
- def type
41
- :string
42
- end
43
-
44
25
  def text_value
45
26
  super.gsub(/"/,'')
46
27
  end
47
28
  end
48
29
 
49
30
  class Integer < Treetop::Runtime::SyntaxNode
50
- def type
51
- :integer
52
- end
53
-
54
31
  def text_value
55
32
  super.to_i
56
33
  end
@@ -43,8 +43,6 @@ module Slate
43
43
  args.map do |arg|
44
44
  if arg.is_a?(Numeric)
45
45
  arg.to_s
46
- elsif arg.is_a?(Slate::Graph)
47
- arg.send(:target)
48
46
  elsif arg.is_a? Slate::Target
49
47
  arg.to_s
50
48
  else
@@ -1,3 +1,3 @@
1
1
  module Slate
2
- VERSION = "1.0.3"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -17,10 +17,10 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_dependency "rest-client", "~> 1.6.7"
21
- gem.add_dependency "json", "~> 1.7.5"
20
+ gem.add_dependency "faraday", "~> 0.8"
21
+ gem.add_dependency "json", "~> 1.8"
22
22
  gem.add_dependency "jruby-openssl" if RUBY_PLATFORM == 'java'
23
- gem.add_dependency "treetop", "~> 1.4.12"
23
+ gem.add_dependency "treetop", "~> 1.4"
24
24
 
25
25
  gem.add_development_dependency "rake"
26
26
  gem.add_development_dependency "rspec"
@@ -124,6 +124,19 @@ describe Slate::Graph do
124
124
  graph.download(:json).should eq(@json_stub)
125
125
  graph.download(:svg).should eq(@svg_stub)
126
126
  end
127
+
128
+ it "should respect the configured timeout" do
129
+ stub_request(:get, "http://graphite/render?format=png&target=app.server01.timeout").
130
+ to_timeout
131
+
132
+ target = Slate::Target.build("app.server01.timeout")
133
+ graph = Slate::Graph.new(@client)
134
+ graph << target
135
+
136
+ expect {
137
+ graph.download(:png).should eq(@png_stub)
138
+ }.to raise_error(Slate::Error::TimeoutError)
139
+ end
127
140
  end
128
141
 
129
142
  def stub_download(format, body="")
@@ -0,0 +1,75 @@
1
+ require File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')
2
+
3
+ describe Slate::Target do
4
+ context "build" do
5
+ it "should return a target" do
6
+ Slate::Target.build("some.target").should be_a Slate::Target
7
+ end
8
+
9
+ it "should be able to configure the target" do
10
+ target = Slate::Target.build("some.target") do |target|
11
+ target.add_function :sum
12
+ end
13
+
14
+ target.to_s.should include("sum")
15
+ end
16
+ end
17
+
18
+ context "#to_s" do
19
+ let(:target) { Slate::Target.build("some.target") }
20
+
21
+ it "should return the target name" do
22
+ target.to_s.should == "some.target"
23
+ end
24
+
25
+ it "should wrap the target with functions that have no arguments" do
26
+ target.add_function :sum
27
+
28
+ target.to_s.should == "sum(some.target)"
29
+ end
30
+
31
+ context "with a string as a function argument" do
32
+ it "should add an argument" do
33
+ target.add_function :summarize, "1h"
34
+
35
+ target.to_s.should == "summarize(some.target,\"1h\")"
36
+ end
37
+
38
+ it "should add the arguments" do
39
+ target.add_function :summarize, "1h", "avg"
40
+
41
+ target.to_s.should == "summarize(some.target,\"1h\",\"avg\")"
42
+ end
43
+ end
44
+
45
+ context "with a number as a function argument" do
46
+ it "should add an argument" do
47
+ target.add_function :aliasByNode, 1
48
+
49
+ target.to_s.should == "aliasByNode(some.target,1)"
50
+ end
51
+
52
+ it "should add the arguments" do
53
+ target.add_function :aliasByNode, 1, 2
54
+
55
+ target.to_s.should == "aliasByNode(some.target,1,2)"
56
+ end
57
+ end
58
+
59
+ context "with a target as a function argument" do
60
+ let(:other_target) { Slate::Target.build "some.new.target" }
61
+
62
+ it "should add an argument" do
63
+ target.add_function :asPercentOf, other_target
64
+
65
+ target.to_s.should == "asPercentOf(some.target,some.new.target)"
66
+ end
67
+
68
+ it "should add the arguments" do
69
+ target.add_function :asPercentOf, other_target, other_target
70
+
71
+ target.to_s.should == "asPercentOf(some.target,some.new.target,some.new.target)"
72
+ end
73
+ end
74
+ end
75
+ end
@@ -8,4 +8,12 @@ describe Slate do
8
8
 
9
9
  client.endpoint.should == "http://graphite"
10
10
  end
11
+
12
+ it "should be able to configure the request timeout" do
13
+ client = Slate.configure do |c|
14
+ c.timeout = 30
15
+ end
16
+
17
+ client.timeout.should == 30
18
+ end
11
19
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slate
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,16 +9,16 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-14 00:00:00.000000000 Z
12
+ date: 2013-07-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: rest-client
15
+ name: faraday
16
16
  requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 1.6.7
21
+ version: '0.8'
22
22
  type: :runtime
23
23
  prerelease: false
24
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -26,7 +26,7 @@ dependencies:
26
26
  requirements:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
- version: 1.6.7
29
+ version: '0.8'
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: json
32
32
  requirement: !ruby/object:Gem::Requirement
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ~>
36
36
  - !ruby/object:Gem::Version
37
- version: 1.7.5
37
+ version: '1.8'
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ~>
44
44
  - !ruby/object:Gem::Version
45
- version: 1.7.5
45
+ version: '1.8'
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: treetop
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: 1.4.12
53
+ version: '1.4'
54
54
  type: :runtime
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 1.4.12
61
+ version: '1.4'
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rake
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -190,6 +190,7 @@ files:
190
190
  - lib/slate/calculation/last.rb
191
191
  - lib/slate/calculation/mean.rb
192
192
  - lib/slate/client.rb
193
+ - lib/slate/error.rb
193
194
  - lib/slate/graph.rb
194
195
  - lib/slate/parser.rb
195
196
  - lib/slate/parser/extensions.rb
@@ -202,6 +203,7 @@ files:
202
203
  - spec/lib/slate/calculation_spec.rb
203
204
  - spec/lib/slate/graph_spec.rb
204
205
  - spec/lib/slate/parser_spec.rb
206
+ - spec/lib/slate/target_spec.rb
205
207
  - spec/lib/slate_spec.rb
206
208
  - spec/spec_helper.rb
207
209
  homepage: https://github.com/trobrock/slate
@@ -218,7 +220,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
218
220
  version: '0'
219
221
  segments:
220
222
  - 0
221
- hash: -4420928061337880336
223
+ hash: 3246476924624244754
222
224
  required_rubygems_version: !ruby/object:Gem::Requirement
223
225
  none: false
224
226
  requirements:
@@ -227,7 +229,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
229
  version: '0'
228
230
  segments:
229
231
  - 0
230
- hash: -4420928061337880336
232
+ hash: 3246476924624244754
231
233
  requirements: []
232
234
  rubyforge_project:
233
235
  rubygems_version: 1.8.23
@@ -240,5 +242,6 @@ test_files:
240
242
  - spec/lib/slate/calculation_spec.rb
241
243
  - spec/lib/slate/graph_spec.rb
242
244
  - spec/lib/slate/parser_spec.rb
245
+ - spec/lib/slate/target_spec.rb
243
246
  - spec/lib/slate_spec.rb
244
247
  - spec/spec_helper.rb