librato-metrics 1.6.2 → 2.0.0.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -11,6 +11,11 @@ require 'librato/metrics'
11
11
 
12
12
  RSpec.configure do |config|
13
13
 
14
+ # only accept expect syntax instead of should
15
+ config.expect_with :rspec do |c|
16
+ c.syntax = :expect
17
+ end
18
+
14
19
  # purge all metrics from test account
15
20
  def delete_all_metrics
16
21
  connection = Librato::Metrics.client.connection
@@ -65,20 +70,11 @@ RSpec.configure do |config|
65
70
 
66
71
  end
67
72
 
68
- # Ex: 'foobar'.should start_with('foo') #=> true
69
- #
70
- RSpec::Matchers.define :start_with do |start_string|
71
- match do |string|
72
- start_length = start_string.length
73
- string[0..start_length-1] == start_string
74
- end
75
- end
76
-
77
73
  # Compares hashes of arrays by converting the arrays to
78
74
  # sets before comparision
79
75
  #
80
76
  # @example
81
- # {:foo => [1,3,2]}.should equal_unordered({:foo => [1,2,3]})
77
+ # {foo: [1,3,2]}.should equal_unordered({foo: [1,2,3]})
82
78
  RSpec::Matchers.define :equal_unordered do |result|
83
79
  result.each do |key, value|
84
80
  result[key] = value.to_set if value.respond_to?(:to_set)
@@ -6,174 +6,174 @@ module Librato
6
6
 
7
7
  before(:all) do
8
8
  @time = 1354720160 #Time.now.to_i
9
- Aggregator.any_instance.stub(:epoch_time).and_return(@time)
9
+ allow_any_instance_of(Aggregator).to receive(:epoch_time).and_return(@time)
10
10
  end
11
11
 
12
12
  describe "initialization" do
13
13
  context "with specified client" do
14
- it "should set to client" do
14
+ it "sets to client" do
15
15
  barney = Client.new
16
- a = Aggregator.new(:client => barney)
17
- a.client.should be barney
16
+ a = Aggregator.new(client: barney)
17
+ expect(a.client).to eq(barney)
18
18
  end
19
19
  end
20
20
 
21
21
  context "without specified client" do
22
- it "should use Librato::Metrics client" do
22
+ it "uses Librato::Metrics client" do
23
23
  a = Aggregator.new
24
- a.client.should be Librato::Metrics.client
24
+ expect(a.client).to eq(Librato::Metrics.client)
25
25
  end
26
26
  end
27
27
 
28
28
  context "with specified source" do
29
- it "should set to source" do
30
- a = Aggregator.new(:source => 'rubble')
31
- a.source.should == 'rubble'
29
+ it "sets to source" do
30
+ a = Aggregator.new(source: 'rubble')
31
+ expect(a.source).to eq('rubble')
32
32
  end
33
33
  end
34
34
 
35
35
  context "without specified source" do
36
- it "should not have a source" do
36
+ it "does not have a source" do
37
37
  a = Aggregator.new
38
- a.source.should be_nil
38
+ expect(a.source).to be_nil
39
39
  end
40
40
  end
41
41
  end
42
42
 
43
43
  describe "#add" do
44
- it "should allow chaining" do
45
- subject.add(:foo => 1234).should == subject
44
+ it "allows chaining" do
45
+ expect(subject.add(foo: 1234)).to eq(subject)
46
46
  end
47
47
 
48
48
  context "with single hash argument" do
49
- it "should record a single aggregate" do
50
- subject.add :foo => 3000
51
- expected = { #:measure_time => @time, TODO: support specific time
52
- :gauges => [
53
- { :name => 'foo',
54
- :count => 1,
55
- :sum => 3000.0,
56
- :min => 3000.0,
57
- :max => 3000.0}
49
+ it "records a single aggregate" do
50
+ subject.add foo: 3000
51
+ expected = { #measure_time: @time, TODO: support specific time
52
+ gauges: [
53
+ { name: 'foo',
54
+ count: 1,
55
+ sum: 3000.0,
56
+ min: 3000.0,
57
+ max: 3000.0}
58
58
  ]
59
59
  }
60
- subject.queued.should equal_unordered(expected)
60
+ expect(subject.queued).to equal_unordered(expected)
61
61
  end
62
62
 
63
- it "should aggregate multiple measurements" do
64
- subject.add :foo => 1
65
- subject.add :foo => 2
66
- subject.add :foo => 3
67
- subject.add :foo => 4
68
- subject.add :foo => 5
69
- expected = { :gauges => [
70
- { :name => 'foo',
71
- :count => 5,
72
- :sum => 15.0,
73
- :min => 1.0,
74
- :max => 5.0}
63
+ it "aggregates multiple measurements" do
64
+ subject.add foo: 1
65
+ subject.add foo: 2
66
+ subject.add foo: 3
67
+ subject.add foo: 4
68
+ subject.add foo: 5
69
+ expected = { gauges: [
70
+ { name: 'foo',
71
+ count: 5,
72
+ sum: 15.0,
73
+ min: 1.0,
74
+ max: 5.0}
75
75
  ]
76
76
  }
77
- subject.queued.should equal_unordered(expected)
77
+ expect(subject.queued).to equal_unordered(expected)
78
78
  end
79
79
 
80
- it "should respect source argument" do
81
- subject.add :foo => {:source => 'alpha', :value => 1}
82
- subject.add :foo => 5
83
- subject.add :foo => {:source => :alpha, :value => 6}
84
- subject.add :foo => 10
85
- expected = { :gauges => [
86
- { :name => 'foo', :source => 'alpha', :count => 2,
87
- :sum => 7.0, :min => 1.0, :max => 6.0 },
88
- { :name => 'foo', :count => 2,
89
- :sum => 15.0, :min => 5.0, :max => 10.0 }
80
+ it "respects source argument" do
81
+ subject.add foo: {source: 'alpha', value: 1}
82
+ subject.add foo: 5
83
+ subject.add foo: {source: :alpha, value: 6}
84
+ subject.add foo: 10
85
+ expected = { gauges: [
86
+ { name: 'foo', source: 'alpha', count: 2,
87
+ sum: 7.0, min: 1.0, max: 6.0 },
88
+ { name: 'foo', count: 2,
89
+ sum: 15.0, min: 5.0, max: 10.0 }
90
90
  ]}
91
- subject.queued.should equal_unordered(expected)
91
+ expect(subject.queued).to equal_unordered(expected)
92
92
  end
93
93
 
94
94
  context "with a prefix set" do
95
- it "should auto-prepend names" do
96
- subject = Aggregator.new(:prefix => 'foo')
97
- subject.add :bar => 1
98
- subject.add :bar => 12
99
- expected = {:gauges => [
100
- { :name =>'foo.bar',
101
- :count => 2,
102
- :sum => 13.0,
103
- :min => 1.0,
104
- :max => 12.0
95
+ it "auto-prepends names" do
96
+ subject = Aggregator.new(prefix: 'foo')
97
+ subject.add bar: 1
98
+ subject.add bar: 12
99
+ expected = {gauges: [
100
+ { name:'foo.bar',
101
+ count: 2,
102
+ sum: 13.0,
103
+ min: 1.0,
104
+ max: 12.0
105
105
  }
106
106
  ]
107
107
  }
108
- subject.queued.should equal_unordered(expected)
108
+ expect(subject.queued).to equal_unordered(expected)
109
109
  end
110
110
  end
111
111
  end
112
112
 
113
113
  context "with multiple hash arguments" do
114
- it "should record a single aggregate" do
115
- subject.add :foo => 3000
116
- subject.add :bar => 30
114
+ it "records a single aggregate" do
115
+ subject.add foo: 3000
116
+ subject.add bar: 30
117
117
  expected = {
118
- #:measure_time => @time, TODO: support specific time
119
- :gauges => [
120
- { :name => 'foo',
121
- :count => 1,
122
- :sum => 3000.0,
123
- :min => 3000.0,
124
- :max => 3000.0},
125
- { :name => 'bar',
126
- :count => 1,
127
- :sum => 30.0,
128
- :min => 30.0,
129
- :max => 30.0},
118
+ #measure_time: @time, TODO: support specific time
119
+ gauges: [
120
+ { name: 'foo',
121
+ count: 1,
122
+ sum: 3000.0,
123
+ min: 3000.0,
124
+ max: 3000.0},
125
+ { name: 'bar',
126
+ count: 1,
127
+ sum: 30.0,
128
+ min: 30.0,
129
+ max: 30.0},
130
130
  ]
131
131
  }
132
- subject.queued.should equal_unordered(expected)
132
+ expect(subject.queued).to equal_unordered(expected)
133
133
  end
134
134
 
135
- it "should aggregate multiple measurements" do
136
- subject.add :foo => 1
137
- subject.add :foo => 2
138
- subject.add :foo => 3
139
- subject.add :foo => 4
140
- subject.add :foo => 5
141
-
142
- subject.add :bar => 6
143
- subject.add :bar => 7
144
- subject.add :bar => 8
145
- subject.add :bar => 9
146
- subject.add :bar => 10
147
- expected = { :gauges => [
148
- { :name => 'foo',
149
- :count => 5,
150
- :sum => 15.0,
151
- :min => 1.0,
152
- :max => 5.0},
153
- { :name => 'bar',
154
- :count => 5,
155
- :sum => 40.0,
156
- :min => 6.0,
157
- :max => 10.0}
135
+ it "aggregates multiple measurements" do
136
+ subject.add foo: 1
137
+ subject.add foo: 2
138
+ subject.add foo: 3
139
+ subject.add foo: 4
140
+ subject.add foo: 5
141
+
142
+ subject.add bar: 6
143
+ subject.add bar: 7
144
+ subject.add bar: 8
145
+ subject.add bar: 9
146
+ subject.add bar: 10
147
+ expected = { gauges: [
148
+ { name: 'foo',
149
+ count: 5,
150
+ sum: 15.0,
151
+ min: 1.0,
152
+ max: 5.0},
153
+ { name: 'bar',
154
+ count: 5,
155
+ sum: 40.0,
156
+ min: 6.0,
157
+ max: 10.0}
158
158
  ]
159
159
  }
160
- subject.queued.should equal_unordered(expected)
160
+ expect(subject.queued).to equal_unordered(expected)
161
161
  end
162
162
  end
163
163
  end
164
164
 
165
165
  describe "#queued" do
166
- it "should include global source if set" do
167
- a = Aggregator.new(:source => 'blah')
168
- a.add :foo => 12
169
- a.queued[:source].should == 'blah'
166
+ it "includes global source if set" do
167
+ a = Aggregator.new(source: 'blah')
168
+ a.add foo: 12
169
+ expect(a.queued[:source]).to eq('blah')
170
170
  end
171
171
 
172
- it "should include global measure_time if set" do
172
+ it "includes global measure_time if set" do
173
173
  measure_time = (Time.now-1000).to_i
174
- a = Aggregator.new(:measure_time => measure_time)
175
- a.add :foo => 12
176
- a.queued[:measure_time].should == measure_time
174
+ a = Aggregator.new(measure_time: measure_time)
175
+ a.add foo: 12
176
+ expect(a.queued[:measure_time]).to eq(measure_time)
177
177
  end
178
178
  end
179
179
 
@@ -184,44 +184,44 @@ module Librato
184
184
  end
185
185
 
186
186
  context "when successful" do
187
- it "should flush queued metrics and return true" do
188
- subject.add :steps => 2042, :distance => 1234
189
- subject.submit.should be_true
190
- subject.empty?.should be_true
187
+ it "flushes queued metrics and return true" do
188
+ subject.add steps: 2042, distance: 1234
189
+ expect(subject.submit).to be true
190
+ expect(subject.empty?).to be true
191
191
  end
192
192
  end
193
193
 
194
194
  context "when failed" do
195
- it "should preserve queue and return false" do
196
- subject.add :steps => 2042, :distance => 1234
195
+ it "preserves queue and return false" do
196
+ subject.add steps: 2042, distance: 1234
197
197
  subject.persister.return_value(false)
198
- subject.submit.should be_false
199
- subject.empty?.should be_false
198
+ expect(subject.submit).to be false
199
+ expect(subject.empty?).to be false
200
200
  end
201
201
  end
202
202
  end
203
203
 
204
204
  describe "#time" do
205
205
  context "with metric name only" do
206
- it "should queue metric with timed value" do
206
+ it "queues metric with timed value" do
207
207
  1.upto(5) do
208
208
  subject.time :sleeping do
209
209
  sleep 0.1
210
210
  end
211
211
  end
212
212
  queued = subject.queued[:gauges][0]
213
- queued[:name].should == 'sleeping'
214
- queued[:count].should be 5
215
- queued[:sum].should be >= 500.0
216
- queued[:sum].should be_within(150).of(500)
213
+ expect(queued[:name]).to eq('sleeping')
214
+ expect(queued[:count]).to eq(5)
215
+ expect(queued[:sum]).to be >= 500.0
216
+ expect(queued[:sum]).to be_within(150).of(500)
217
217
  end
218
218
 
219
- it "should return the result of the block" do
219
+ it "returns the result of the block" do
220
220
  result = subject.time :returning do
221
221
  :hi_there
222
222
  end
223
223
 
224
- result.should == :hi_there
224
+ expect(result).to eq(:hi_there)
225
225
  end
226
226
  end
227
227
  end
@@ -233,18 +233,18 @@ module Librato
233
233
  client
234
234
  end
235
235
 
236
- it "should not submit immediately" do
237
- timed_agg = Aggregator.new(:client => client, :autosubmit_interval => 1)
238
- timed_agg.add :foo => 1
239
- timed_agg.persister.persisted.should be_nil # nothing sent
236
+ it "does not submit immediately" do
237
+ timed_agg = Aggregator.new(client: client, autosubmit_interval: 1)
238
+ timed_agg.add foo: 1
239
+ expect(timed_agg.persister.persisted).to be_nil # nothing sent
240
240
  end
241
241
 
242
- it "should submit after interval" do
243
- timed_agg = Aggregator.new(:client => client, :autosubmit_interval => 1)
244
- timed_agg.add :foo => 1
242
+ it "submits after interval" do
243
+ timed_agg = Aggregator.new(client: client, autosubmit_interval: 1)
244
+ timed_agg.add foo: 1
245
245
  sleep 1
246
- timed_agg.add :foo => 2
247
- timed_agg.persister.persisted.should_not be_nil # sent
246
+ timed_agg.add foo: 2
247
+ expect(timed_agg.persister.persisted).not_to be_nil # sent
248
248
  end
249
249
  end
250
250
 
@@ -7,43 +7,43 @@ module Librato
7
7
 
8
8
  describe "#agent_identifier" do
9
9
  context "when given a single string argument" do
10
- it "should set agent_identifier" do
10
+ it "sets agent_identifier" do
11
11
  subject.agent_identifier 'mycollector/0.1 (dev_id:foo)'
12
- subject.agent_identifier.should == 'mycollector/0.1 (dev_id:foo)'
12
+ expect(subject.agent_identifier).to eq('mycollector/0.1 (dev_id:foo)')
13
13
  end
14
14
  end
15
15
 
16
16
  context "when given three arguments" do
17
- it "should compose an agent string" do
17
+ it "composes an agent string" do
18
18
  subject.agent_identifier('test_app', '0.5', 'foobar')
19
- subject.agent_identifier.should == 'test_app/0.5 (dev_id:foobar)'
19
+ expect(subject.agent_identifier).to eq('test_app/0.5 (dev_id:foobar)')
20
20
  end
21
21
 
22
22
  context "when given an empty string" do
23
- it "should set to empty" do
23
+ it "sets to empty" do
24
24
  subject.agent_identifier ''
25
- subject.agent_identifier.should == ''
25
+ expect(subject.agent_identifier).to be_empty
26
26
  end
27
27
  end
28
28
  end
29
29
 
30
30
  context "when given two arguments" do
31
- it "should raise error" do
32
- lambda { subject.agent_identifier('test_app', '0.5') }.should raise_error(ArgumentError)
31
+ it "raises error" do
32
+ expect { subject.agent_identifier('test_app', '0.5') }.to raise_error(ArgumentError)
33
33
  end
34
34
  end
35
35
  end
36
36
 
37
37
  describe "#api_endpoint" do
38
- it "should default to metrics" do
39
- subject.api_endpoint.should == 'https://metrics-api.librato.com'
38
+ it "defaults to metrics" do
39
+ expect(subject.api_endpoint).to eq('https://metrics-api.librato.com')
40
40
  end
41
41
  end
42
42
 
43
43
  describe "#api_endpoint=" do
44
- it "should set api_endpoint" do
44
+ it "sets api_endpoint" do
45
45
  subject.api_endpoint = 'http://test.com/'
46
- subject.api_endpoint.should == 'http://test.com/'
46
+ expect(subject.api_endpoint).to eq('http://test.com/')
47
47
  end
48
48
 
49
49
  # TODO:
@@ -53,71 +53,71 @@ module Librato
53
53
 
54
54
  describe "#authenticate" do
55
55
  context "when given two arguments" do
56
- it "should store them as email and api_key" do
56
+ it "stores them as email and api_key" do
57
57
  subject.authenticate 'test@librato.com', 'api_key'
58
- subject.email.should == 'test@librato.com'
59
- subject.api_key.should == 'api_key'
58
+ expect(subject.email).to eq('test@librato.com')
59
+ expect(subject.api_key).to eq('api_key')
60
60
  end
61
61
  end
62
62
  end
63
63
 
64
64
  describe "#connection" do
65
- it "should raise exception without authentication" do
65
+ it "raises exception without authentication" do
66
66
  subject.flush_authentication
67
- lambda{ subject.connection }.should raise_error(Librato::Metrics::CredentialsMissing)
67
+ expect { subject.connection }.to raise_error(Librato::Metrics::CredentialsMissing)
68
68
  end
69
69
  end
70
-
70
+
71
71
  describe "#faraday_adapter" do
72
- it "should default to Metrics default adapter" do
72
+ it "defaults to Metrics default adapter" do
73
73
  Metrics.faraday_adapter = :typhoeus
74
- Client.new.faraday_adapter.should == Metrics.faraday_adapter
74
+ expect(Client.new.faraday_adapter).to eq(Metrics.faraday_adapter)
75
75
  Metrics.faraday_adapter = nil
76
76
  end
77
77
  end
78
-
78
+
79
79
  describe "#faraday_adapter=" do
80
- it "should allow setting of faraday adapter" do
80
+ it "allows setting of faraday adapter" do
81
81
  subject.faraday_adapter = :excon
82
- subject.faraday_adapter.should == :excon
82
+ expect(subject.faraday_adapter).to eq(:excon)
83
83
  subject.faraday_adapter = :patron
84
- subject.faraday_adapter.should == :patron
84
+ expect(subject.faraday_adapter).to eq(:patron)
85
85
  end
86
86
  end
87
87
 
88
88
  describe "#new_queue" do
89
- it "should return a new queue with client set" do
89
+ it "returns a new queue with client set" do
90
90
  queue = subject.new_queue
91
- queue.client.should be subject
91
+ expect(queue.client).to eq(subject)
92
92
  end
93
93
  end
94
94
 
95
95
  describe "#persistence" do
96
- it "should default to direct" do
96
+ it "defaults to direct" do
97
97
  subject.send(:flush_persistence)
98
- subject.persistence.should == :direct
98
+ expect(subject.persistence).to eq(:direct)
99
99
  end
100
100
 
101
- it "should allow configuration of persistence method" do
101
+ it "allows configuration of persistence method" do
102
102
  subject.persistence = :fake
103
- subject.persistence.should == :fake
103
+ expect(subject.persistence).to eq(:fake)
104
104
  end
105
105
  end
106
106
 
107
107
  describe "#submit" do
108
- it "should persist metrics immediately" do
108
+ it "persists metrics immediately" do
109
109
  subject.authenticate 'me@librato.com', 'foo'
110
110
  subject.persistence = :test
111
- subject.submit(:foo => 123).should eql true
112
- subject.persister.persisted.should == {:gauges => [{:name => 'foo', :value => 123}]}
111
+ expect(subject.submit(foo: 123)).to be true
112
+ expect(subject.persister.persisted).to eq({gauges: [{name: 'foo', value: 123}]})
113
113
  end
114
114
 
115
- it "should tolerate muliple metrics" do
115
+ it "tolerates muliple metrics" do
116
116
  subject.authenticate 'me@librato.com', 'foo'
117
117
  subject.persistence = :test
118
- lambda{ subject.submit :foo => 123, :bar => 456 }.should_not raise_error
119
- expected = {:gauges => [{:name => 'foo', :value => 123}, {:name => 'bar', :value => 456}]}
120
- subject.persister.persisted.should equal_unordered(expected)
118
+ expect { subject.submit foo: 123, bar: 456 }.not_to raise_error
119
+ expected = {gauges: [{name: 'foo', value: 123}, {name: 'bar', value: 456}]}
120
+ expect(subject.persister.persisted).to equal_unordered(expected)
121
121
  end
122
122
  end
123
123