librato-metrics 0.4.3 → 0.5.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
2
 
3
+ require 'pry'
3
4
  require 'rspec'
4
5
  require 'rspec/mocks/standalone'
5
6
 
@@ -19,10 +20,11 @@ RSpec.configure do |config|
19
20
 
20
21
  # purge all metrics from test account
21
22
  def delete_all_metrics
22
- connection = Librato::Metrics.connection
23
+ connection = Librato::Metrics.client.connection
23
24
  Librato::Metrics.list.each do |metric|
24
25
  #puts "deleting #{metric['name']}..."
25
- connection.delete(:path => "v1/metrics/#{metric['name']}", :expects => 204)
26
+ # expects 204
27
+ connection.delete("metrics/#{metric['name']}")
26
28
  end
27
29
  end
28
30
 
@@ -0,0 +1,110 @@
1
+ require 'spec_helper'
2
+
3
+ module Librato
4
+ module Metrics
5
+
6
+ describe Client do
7
+
8
+ describe "#agent_identifier" do
9
+ context "when given a single string argument" do
10
+ it "should set agent_identifier" do
11
+ subject.agent_identifier 'mycollector/0.1 (dev_id:foo)'
12
+ subject.agent_identifier.should == 'mycollector/0.1 (dev_id:foo)'
13
+ end
14
+ end
15
+
16
+ context "when given three arguments" do
17
+ it "should compose an agent string" do
18
+ subject.agent_identifier('test_app', '0.5', 'foobar')
19
+ subject.agent_identifier.should == 'test_app/0.5 (dev_id:foobar)'
20
+ end
21
+
22
+ context "when given an empty string" do
23
+ it "should set to empty" do
24
+ subject.agent_identifier ''
25
+ subject.agent_identifier.should == ''
26
+ end
27
+ end
28
+ end
29
+
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)
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "#api_endpoint" do
38
+ it "should default to metrics" do
39
+ subject.api_endpoint.should == 'https://metrics-api.librato.com'
40
+ end
41
+ end
42
+
43
+ describe "#api_endpoint=" do
44
+ it "should set api_endpoint" do
45
+ subject.api_endpoint = 'http://test.com/'
46
+ subject.api_endpoint.should == 'http://test.com/'
47
+ end
48
+
49
+ # TODO:
50
+ # it "should ensure trailing slash"
51
+ # it "should ensure real URI"
52
+ end
53
+
54
+ describe "#authenticate" do
55
+ context "when given two arguments" do
56
+ it "should store them as email and api_key" do
57
+ subject.authenticate 'test@librato.com', 'api_key'
58
+ subject.email.should == 'test@librato.com'
59
+ subject.api_key.should == 'api_key'
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "#connection" do
65
+ it "should raise exception without authentication" do
66
+ subject.flush_authentication
67
+ lambda{ subject.connection }.should raise_error(Librato::Metrics::CredentialsMissing)
68
+ end
69
+ end
70
+
71
+ describe "#new_queue" do
72
+ it "should return a new queue with client set" do
73
+ queue = subject.new_queue
74
+ queue.client.should be subject
75
+ end
76
+ end
77
+
78
+ describe "#persistence" do
79
+ it "should default to direct" do
80
+ subject.send(:flush_persistence)
81
+ subject.persistence.should == :direct
82
+ end
83
+
84
+ it "should allow configuration of persistence method" do
85
+ subject.persistence = :fake
86
+ subject.persistence.should == :fake
87
+ end
88
+ end
89
+
90
+ describe "#submit" do
91
+ it "should persist metrics immediately" do
92
+ subject.authenticate 'me@librato.com', 'foo'
93
+ subject.persistence = :test
94
+ subject.submit(:foo => 123).should eql true
95
+ subject.persister.persisted.should eql({:gauges => [{:name => 'foo', :value => 123}]})
96
+ end
97
+
98
+ it "should tolerate muliple metrics" do
99
+ subject.authenticate 'me@librato.com', 'foo'
100
+ subject.persistence = :test
101
+ lambda{ subject.submit :foo => 123, :bar => 456 }.should_not raise_error
102
+ expected = {:gauges => [{:name => 'foo', :value => 123}, {:name => 'bar', :value => 456}]}
103
+ subject.persister.persisted.should eql expected
104
+ end
105
+ end
106
+
107
+ end
108
+
109
+ end
110
+ end
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ module Librato
4
+ module Metrics
5
+
6
+ describe Connection do
7
+
8
+ describe "network operations" do
9
+ context "when missing client" do
10
+ it "should raise exception" do
11
+ lambda { subject.get 'metrics' }#.should raise(NoClientProvided)
12
+ end
13
+ end
14
+ end
15
+
16
+ describe "#api_endpoint" do
17
+ context "when not provided" do
18
+ it "should be default" do
19
+ subject.api_endpoint.should == 'https://metrics-api.librato.com'
20
+ end
21
+ end
22
+
23
+ context "when provided" do
24
+ it "should be respected" do
25
+ connection = Connection.new(:api_endpoint => 'http://test.com/')
26
+ connection.api_endpoint.should == 'http://test.com/'
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "#user_agent" do
32
+ context "without an agent_identifier" do
33
+ it "should render standard string" do
34
+ connection = Connection.new(:client => Client.new)
35
+ connection.user_agent.should start_with('librato-metrics')
36
+ end
37
+ end
38
+
39
+ context "with an agent_identifier" do
40
+ it "should render agent_identifier first" do
41
+ client = Client.new
42
+ client.agent_identifier('foo', '0.5', 'bar')
43
+ connection = Connection.new(:client => client)
44
+ connection.user_agent.should start_with('foo/0.5')
45
+ end
46
+ end
47
+
48
+ # TODO: verify user agent is being sent with rackup test
49
+ end
50
+
51
+
52
+ end
53
+
54
+ end
55
+ end
@@ -7,7 +7,24 @@ module Librato
7
7
 
8
8
  before(:all) do
9
9
  @time = Time.now.to_i
10
- Librato::Metrics::Queue.stub(:epoch_time).and_return(@time)
10
+ Queue.stub(:epoch_time).and_return(@time)
11
+ end
12
+
13
+ describe "initialization" do
14
+ context "with specified client" do
15
+ it "should set to client" do
16
+ barney = Client
17
+ queue = Queue.new(:client => barney)
18
+ queue.client.should be barney
19
+ end
20
+ end
21
+
22
+ context "without specified client" do
23
+ it "should use Librato::Metrics client" do
24
+ queue = Queue.new
25
+ queue.client.should be Librato::Metrics.client
26
+ end
27
+ end
11
28
  end
12
29
 
13
30
  describe "#add" do
@@ -115,7 +132,6 @@ module Librato
115
132
  Librato::Metrics.authenticate 'me@librato.com', 'foo'
116
133
  Librato::Metrics.persistence = :test
117
134
  end
118
- after(:all) { Librato::Metrics::Simple.flush_authentication }
119
135
 
120
136
  context "when successful" do
121
137
  it "should flush queued metrics and return true" do
@@ -9,8 +9,8 @@ module Librato
9
9
  context "when given two arguments" do
10
10
  it "should store them on simple" do
11
11
  Metrics.authenticate 'tester@librato.com', 'api_key'
12
- Metrics::Simple.email.should == 'tester@librato.com'
13
- Metrics::Simple.api_key.should == 'api_key'
12
+ Metrics.client.email.should == 'tester@librato.com'
13
+ Metrics.client.api_key.should == 'api_key'
14
14
  end
15
15
  end
16
16
 
@@ -30,7 +30,7 @@ module Librato
30
30
  Librato::Metrics.persistence = :test
31
31
  Librato::Metrics.authenticate 'me@librato.com', 'foo'
32
32
  end
33
- after(:all) { Librato::Metrics::Simple.flush_authentication }
33
+ after(:all) { Librato::Metrics.client.flush_authentication }
34
34
 
35
35
  it "should persist metrics immediately" do
36
36
  Metrics.persistence = :test
metadata CHANGED
@@ -1,30 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: librato-metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
5
- prerelease:
4
+ version: 0.5.0.pre1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Matt Sanders
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-23 00:00:00.000000000Z
12
+ date: 2012-03-26 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: excon
16
- requirement: &70348621518180 !ruby/object:Gem::Requirement
15
+ name: faraday
16
+ requirement: &70341863470180 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.13.0
21
+ version: 0.7.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70348621518180
24
+ version_requirements: *70341863470180
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: multi_json
27
- requirement: &70348621517780 !ruby/object:Gem::Requirement
27
+ requirement: &70341863469760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70348621517780
35
+ version_requirements: *70341863469760
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70348621517320 !ruby/object:Gem::Requirement
38
+ requirement: &70341863469300 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70348621517320
46
+ version_requirements: *70341863469300
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70348621505780 !ruby/object:Gem::Requirement
49
+ requirement: &70341863468800 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ~>
@@ -54,10 +54,21 @@ dependencies:
54
54
  version: 2.6.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70348621505780
57
+ version_requirements: *70341863468800
58
+ - !ruby/object:Gem::Dependency
59
+ name: pry
60
+ requirement: &70341863468380 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70341863468380
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: yard
60
- requirement: &70348621505280 !ruby/object:Gem::Requirement
71
+ requirement: &70341863467920 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ! '>='
@@ -65,10 +76,10 @@ dependencies:
65
76
  version: '0'
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *70348621505280
79
+ version_requirements: *70341863467920
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: rdiscount
71
- requirement: &70348621504820 !ruby/object:Gem::Requirement
82
+ requirement: &70341863467500 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ! '>='
@@ -76,7 +87,7 @@ dependencies:
76
87
  version: '0'
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *70348621504820
90
+ version_requirements: *70341863467500
80
91
  description: An easy to use ruby wrapper for Librato's Metrics API
81
92
  email: matt@librato.com
82
93
  executables: []
@@ -92,19 +103,24 @@ files:
92
103
  - README.md
93
104
  - Rakefile
94
105
  - lib/librato/metrics.rb
95
- - lib/librato/metrics/collect.rb
106
+ - lib/librato/metrics/client.rb
107
+ - lib/librato/metrics/collection.rb
108
+ - lib/librato/metrics/connection.rb
96
109
  - lib/librato/metrics/errors.rb
110
+ - lib/librato/metrics/middleware/expects_status.rb
111
+ - lib/librato/metrics/middleware/retry.rb
97
112
  - lib/librato/metrics/persistence.rb
98
113
  - lib/librato/metrics/persistence/direct.rb
99
114
  - lib/librato/metrics/persistence/test.rb
100
115
  - lib/librato/metrics/queue.rb
101
- - lib/librato/metrics/simple.rb
102
116
  - lib/librato/metrics/version.rb
103
117
  - librato-metrics.gemspec
118
+ - spec/integration/metrics/connection_spec.rb
104
119
  - spec/integration/metrics_spec.rb
105
120
  - spec/spec_helper.rb
121
+ - spec/unit/metrics/client_spec.rb
122
+ - spec/unit/metrics/connection_spec.rb
106
123
  - spec/unit/metrics/queue_spec.rb
107
- - spec/unit/metrics/simple_spec.rb
108
124
  - spec/unit/metrics_spec.rb
109
125
  homepage: https://github.com/librato/librato-metrics
110
126
  licenses: []
@@ -122,19 +138,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
122
138
  required_rubygems_version: !ruby/object:Gem::Requirement
123
139
  none: false
124
140
  requirements:
125
- - - ! '>='
141
+ - - ! '>'
126
142
  - !ruby/object:Gem::Version
127
- version: '0'
143
+ version: 1.3.1
128
144
  requirements: []
129
145
  rubyforge_project:
130
- rubygems_version: 1.8.12
146
+ rubygems_version: 1.8.16
131
147
  signing_key:
132
148
  specification_version: 2
133
149
  summary: Ruby wrapper for Librato's Metrics API
134
150
  test_files:
151
+ - spec/integration/metrics/connection_spec.rb
135
152
  - spec/integration/metrics_spec.rb
136
153
  - spec/spec_helper.rb
154
+ - spec/unit/metrics/client_spec.rb
155
+ - spec/unit/metrics/connection_spec.rb
137
156
  - spec/unit/metrics/queue_spec.rb
138
- - spec/unit/metrics/simple_spec.rb
139
157
  - spec/unit/metrics_spec.rb
140
158
  has_rdoc:
@@ -1,148 +0,0 @@
1
- module Librato
2
- module Metrics
3
-
4
- # Class-level methods for quick one-off submission of metrics.
5
- #
6
- # @example Send a quick metric
7
- # Librato::Metrics::Simple.authenticate 'fred@foo.com', 'myapikey'
8
- # Librato::Metrics::Simple.save :total_vists => {:type => counter, :value => 2311}
9
- #
10
- # For more than quick one-off use, take a look at {Queue}. For
11
- # convenience, most of Simple's methods can be accessed directly from
12
- # the {Metrics} module.
13
- #
14
- class Simple
15
-
16
- class << self
17
- # class instance vars
18
- attr_accessor :email, :api_key
19
-
20
- # Provide agent identifier for the developer program. See:
21
- # http://support.metrics.librato.com/knowledgebase/articles/53548-developer-program
22
- #
23
- # @example Have the gem build your identifier string
24
- # Librato::Metrics.agent_identifier 'flintstone', '0.5', 'fred'
25
- #
26
- # @example Provide your own identifier string
27
- # Librato::Metrics.agent_identifier 'flintstone/0.5 (dev_id:fred)'
28
- #
29
- # @example Remove identifier string
30
- # Librato::Metrics.agent_identifier ''
31
- def agent_identifier(*args)
32
- if args.length == 1
33
- @agent_identifier = args.first
34
- elsif args.length == 3
35
- @agent_identifier = "#{args[0]}/#{args[1]} (dev_id:#{args[2]})"
36
- elsif ![0,1,3].include?(args.length)
37
- raise ArgumentError, 'invalid arguments, see method documentation'
38
- end
39
- @agent_identifier ||= ''
40
- end
41
-
42
- # API endpoint to use for queries and direct
43
- # persistence.
44
- #
45
- # @return [String] api_endpoint
46
- def api_endpoint
47
- @api_endpoint ||= 'https://metrics-api.librato.com/v1/'
48
- end
49
-
50
- # Set API endpoint for use with queries and direct
51
- # persistence. Generally you should not need to set this
52
- # as it will default to the current Librato Metrics
53
- # endpoint.
54
- #
55
- def api_endpoint=(endpoint)
56
- @api_endpoint = endpoint
57
- end
58
-
59
- # Authenticate for direct persistence
60
- #
61
- # @param [String] email
62
- # @param [String] api_key
63
- def authenticate(email, api_key)
64
- flush_authentication
65
- self.email, self.api_key = email, api_key
66
- end
67
-
68
- def connection
69
- # TODO: upate when excon connection recovery is improved.
70
- # @connection ||= Excon.new(self.api_endpoint, :headers => common_headers)
71
- Excon.defaults[:ssl_verify_peer] = false if RUBY_PLATFORM == "java"
72
- Excon.new(self.api_endpoint, :headers => common_headers)
73
- end
74
-
75
- # Purge current credentials and connection
76
- #
77
- def flush_authentication
78
- self.email = nil
79
- self.api_key = nil
80
- @connection = nil
81
- end
82
-
83
- # Persistence type to use when saving metrics.
84
- # Default is :direct.
85
- #
86
- def persistence
87
- @persistence ||= :direct
88
- end
89
-
90
- # Set persistence type to use when saving metrics.
91
- #
92
- # @param [Symbol] persistence_type
93
- def persistence=(persist_method)
94
- @persistence = persist_method
95
- end
96
-
97
- # Current persister object.
98
- def persister
99
- @queue ? @queue.persister : nil
100
- end
101
-
102
- # Submit all queued metrics.
103
- #
104
- def submit(args)
105
- @queue ||= Queue.new(:skip_measurement_times => true)
106
- @queue.add args
107
- @queue.submit
108
- end
109
-
110
- # User-agent used when making requests.
111
- #
112
- def user_agent
113
- ua_chunks = []
114
- if agent_identifier && !agent_identifier.empty?
115
- ua_chunks << agent_identifier
116
- end
117
- ua_chunks << "librato-metrics/#{Metrics::VERSION}"
118
- ua_chunks << "(#{ruby_engine}; #{RUBY_VERSION}p#{RUBY_PATCHLEVEL}; #{RUBY_PLATFORM})"
119
- ua_chunks << "direct-excon/#{Excon::VERSION}"
120
- ua_chunks.join(' ')
121
- end
122
-
123
- private
124
-
125
- def auth_header
126
- raise CredentialsMissing unless (self.email and self.api_key)
127
- encoded = Base64.encode64("#{email}:#{api_key}").gsub("\n", '')
128
- "Basic #{encoded}"
129
- end
130
-
131
- def common_headers
132
- {'Authorization' => auth_header, 'User-Agent' => user_agent}
133
- end
134
-
135
- def flush_persistence
136
- @persistence = nil
137
- end
138
-
139
- def ruby_engine
140
- return RUBY_ENGINE if Object.constants.include?(:RUBY_ENGINE)
141
- RUBY_DESCRIPTION.split[0]
142
- end
143
-
144
- end
145
-
146
- end
147
- end
148
- end
@@ -1,126 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Librato
4
- module Metrics
5
-
6
- describe Simple do
7
-
8
- describe "#agent_identifier" do
9
- context "when given a single string argument" do
10
- it "should set agent_identifier" do
11
- Simple.agent_identifier 'mycollector/0.1 (dev_id:foo)'
12
- Simple.agent_identifier.should == 'mycollector/0.1 (dev_id:foo)'
13
- end
14
- end
15
-
16
- context "when given three arguments" do
17
- it "should compose an agent string" do
18
- Simple.agent_identifier('test_app', '0.5', 'foobar')
19
- Simple.agent_identifier.should == 'test_app/0.5 (dev_id:foobar)'
20
- end
21
-
22
- context "when given an empty string" do
23
- it "should set to empty" do
24
- Simple.agent_identifier ''
25
- Simple.agent_identifier.should == ''
26
- end
27
- end
28
- end
29
-
30
- context "when given two arguments" do
31
- it "should raise error" do
32
- lambda { Simple.agent_identifier('test_app', '0.5') }.should raise_error(ArgumentError)
33
- end
34
- end
35
- end
36
-
37
- describe "#api_endpoint" do
38
- it "should default to metrics" do
39
- Simple.api_endpoint.should == 'https://metrics-api.librato.com/v1/'
40
- end
41
- end
42
-
43
- describe "#api_endpoint=" do
44
- it "should set api_endpoint" do
45
- @prior = Simple.api_endpoint
46
- Simple.api_endpoint = 'http://test.com/'
47
- Simple.api_endpoint.should == 'http://test.com/'
48
- Simple.api_endpoint = @prior
49
- end
50
-
51
- # TODO:
52
- # it "should ensure trailing slash"
53
- # it "should ensure real URI"
54
- end
55
-
56
- describe "#authenticate" do
57
- context "when given two arguments" do
58
- it "should store them as email and api_key" do
59
- Simple.authenticate 'test@librato.com', 'api_key'
60
- Simple.email.should == 'test@librato.com'
61
- Simple.api_key.should == 'api_key'
62
- end
63
- end
64
- end
65
-
66
- describe "#connection" do
67
- it "should raise exception without authentication" do
68
- Simple.flush_authentication
69
- lambda{ Simple.connection }.should raise_error(Librato::Metrics::CredentialsMissing)
70
- end
71
- end
72
-
73
- describe "#persistence" do
74
- it "should default to direct" do
75
- Simple.send(:flush_persistence)
76
- Simple.persistence.should == :direct
77
- end
78
-
79
- it "should allow configuration of persistence method" do
80
- current = Simple.persistence
81
- Simple.persistence = :fake
82
- Simple.persistence.should == :fake
83
- Simple.persistence = current
84
- end
85
- end
86
-
87
- describe "#submit" do
88
- before(:all) do
89
- Simple.persistence = :test
90
- Simple.authenticate 'me@librato.com', 'foo'
91
- end
92
- after(:all) { Simple.flush_authentication }
93
-
94
- it "should persist metrics immediately" do
95
- Simple.persistence = :test
96
- Simple.submit(:foo => 123).should eql true
97
- Simple.persister.persisted.should eql({:gauges => [{:name => 'foo', :value => 123}]})
98
- end
99
-
100
- it "should tolerate muliple metrics" do
101
- lambda{ Simple.submit :foo => 123, :bar => 456 }.should_not raise_error
102
- expected = {:gauges => [{:name => 'foo', :value => 123}, {:name => 'bar', :value => 456}]}
103
- Simple.persister.persisted.should eql expected
104
- end
105
- end
106
-
107
- describe "#user_agent" do
108
- context "without an agent_identifier" do
109
- it "should render standard string" do
110
- Simple.agent_identifier('')
111
- Simple.user_agent.should start_with('librato-metrics')
112
- end
113
- end
114
-
115
- context "with an agent_identifier" do
116
- it "should render agent_identifier first" do
117
- Simple.agent_identifier('foo', '0.5', 'bar')
118
- Simple.user_agent.should start_with('foo/0.5')
119
- end
120
- end
121
- end
122
-
123
- end
124
-
125
- end
126
- end