librato-metrics 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## Changelog
2
2
 
3
+ ### Version 1.0.3
4
+ * Fix bug where retries of POST requests could 400
5
+ * Network related exceptions capture response state better
6
+
3
7
  ### Version 1.0.2
4
8
  * Fix bug with some versions of MultiJson (Thomas Dippel)
5
9
  * Use delegation for JSON handling
@@ -30,13 +30,10 @@ module Librato
30
30
  def transport
31
31
  raise(NoClientProvided, "No client provided.") unless @client
32
32
  @transport ||= Faraday::Connection.new(:url => api_endpoint + "/v1/") do |f|
33
- #f.use FaradayMiddleware::EncodeJson
34
33
  f.use Librato::Metrics::Middleware::RequestBody
35
34
  f.use Librato::Metrics::Middleware::Retry
36
35
  f.use Librato::Metrics::Middleware::CountRequests
37
-
38
36
  f.use Librato::Metrics::Middleware::ExpectsStatus
39
- #f.use FaradayMiddleware::ParseJson, :content_type => /\bjson$/
40
37
 
41
38
  f.adapter @adapter || Metrics.faraday_adapter
42
39
  end.tap do |transport|
@@ -10,7 +10,14 @@ module Librato
10
10
  class InvalidMeasureTime < MetricsError; end
11
11
  class NotMergeable < MetricsError; end
12
12
 
13
- class NetworkError < StandardError; end
13
+ class NetworkError < StandardError
14
+ attr_reader :response
15
+
16
+ def initialize(msg, response = nil)
17
+ super(msg)
18
+ @response = response
19
+ end
20
+ end
14
21
 
15
22
  class ClientError < NetworkError; end
16
23
  class Unauthorized < ClientError; end
@@ -8,17 +8,17 @@ module Librato
8
8
  # TODO: make exception output prettier
9
9
  case env[:status]
10
10
  when 401
11
- raise Unauthorized, env.to_s
11
+ raise Unauthorized.new(env.to_s, env)
12
12
  when 403
13
- raise Forbidden, env.to_s
13
+ raise Forbidden.new(env.to_s, env)
14
14
  when 404
15
- raise NotFound, env.to_s
15
+ raise NotFound.new(env.to_s, env)
16
16
  when 422
17
- raise EntityAlreadyExists, env.to_s
17
+ raise EntityAlreadyExists.new(env.to_s, env)
18
18
  when 400..499
19
- raise ClientError, env.to_s
19
+ raise ClientError.new(env.to_s, env)
20
20
  when 500..599
21
- raise ServerError, env.to_s
21
+ raise ServerError.new(env.to_s, env)
22
22
  end
23
23
  end
24
24
 
@@ -11,9 +11,11 @@ module Librato
11
11
 
12
12
  def call(env)
13
13
  retries = @retries
14
+ request_body = env[:body]
14
15
  begin
16
+ env[:body] = request_body # after failure is set to response body
15
17
  @app.call(env)
16
- rescue Librato::Metrics::ServerError, Timeout::Error,
18
+ rescue Librato::Metrics::ServerError, Timeout::Error,
17
19
  Faraday::Error::ConnectionFailed
18
20
  if retries > 0
19
21
  retries -= 1 and retry
@@ -1,5 +1,5 @@
1
1
  module Librato
2
2
  module Metrics
3
- VERSION = "1.0.2"
3
+ VERSION = "1.0.3"
4
4
  end
5
5
  end
@@ -60,8 +60,9 @@ module Librato
60
60
  subject.add :deployment, 'deployed v47'
61
61
  events = subject.fetch(:deployment, :start_time => Time.now.to_i-60)
62
62
  events = events['events']['unassigned']
63
- ids = events.each_with_object({}) do |event, hash|
63
+ ids = events.reduce({}) do |hash, event|
64
64
  hash[event['title']] = event['id']
65
+ hash
65
66
  end
66
67
  subject.delete_event :deployment, ids['deployed v47']
67
68
  events = subject.fetch(:deployment, :start_time => Time.now.to_i-60)
@@ -228,7 +228,7 @@ module Librato
228
228
  it "should create the metric if type specified" do
229
229
  delete_all_metrics
230
230
  Metrics.update :foo, :display_name => "Foo Metric",
231
- :type => :gauge,
231
+ :type => 'gauge',
232
232
  :period => 15,
233
233
  :attributes => {
234
234
  :display_max => 1000
@@ -16,6 +16,15 @@ class App < Sinatra::Base
16
16
  post('/v1/service_unavailable') do
17
17
  status 503
18
18
  end
19
+
20
+ post('/v1/retry_body') do
21
+ body = request.env["rack.input"].read
22
+ if body.empty?
23
+ status 504 # body not sent
24
+ else
25
+ status 502 # body sent
26
+ end
27
+ end
19
28
  end
20
29
 
21
30
  run App
@@ -56,12 +56,16 @@ module Librato
56
56
  end
57
57
  end
58
58
 
59
+ let(:client) do
60
+ client = Client.new
61
+ client.api_endpoint = 'http://127.0.0.1:9296'
62
+ client.authenticate 'foo', 'bar'
63
+ client
64
+ end
65
+
59
66
  context "with 400 class errors" do
60
67
  it "should not retry" do
61
68
  Middleware::CountRequests.reset
62
- client = Client.new
63
- client.api_endpoint = 'http://127.0.0.1:9296'
64
- client.authenticate 'foo', 'bar'
65
69
  with_rackup('status.ru') do
66
70
  lambda {
67
71
  client.connection.transport.post 'not_found'
@@ -70,26 +74,39 @@ module Librato
70
74
  client.connection.transport.post 'forbidden'
71
75
  }.should raise_error(ClientError)
72
76
  end
73
- Middleware::CountRequests.total_requests.should == 2
77
+ Middleware::CountRequests.total_requests.should == 2 # no retries
74
78
  end
75
79
  end
76
80
 
77
81
  context "with 500 class errors" do
78
82
  it "should retry" do
79
83
  Middleware::CountRequests.reset
80
- client = Client.new
81
- client.api_endpoint = 'http://127.0.0.1:9296'
82
- client.authenticate 'foo', 'bar'
83
84
  with_rackup('status.ru') do
84
85
  lambda {
85
86
  client.connection.transport.post 'service_unavailable'
86
87
  }.should raise_error(ServerError)
87
88
  end
88
- Middleware::CountRequests.total_requests.should == 4
89
+ Middleware::CountRequests.total_requests.should == 4 # did retries
90
+ end
91
+
92
+ it "should send consistent body with retries" do
93
+ Middleware::CountRequests.reset
94
+ status = 0
95
+ begin
96
+ with_rackup('status.ru') do
97
+ response = client.connection.transport.post do |req|
98
+ req.url 'retry_body'
99
+ req.body = '{"foo": "bar", "baz": "kaboom"}'
100
+ end
101
+ end
102
+ rescue Exception => error
103
+ status = error.response[:status].to_i
104
+ end
105
+ Middleware::CountRequests.total_requests.should == 4 # did retries
106
+ status.should be(502)#, 'body should be sent for retries'
89
107
  end
90
108
  end
91
109
  end
92
-
93
110
  end
94
111
 
95
112
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: librato-metrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-23 00:00:00.000000000 Z
12
+ date: 2013-01-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday
@@ -218,7 +218,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
218
218
  version: '0'
219
219
  segments:
220
220
  - 0
221
- hash: 2478461788804600344
221
+ hash: 3023872404930290893
222
222
  required_rubygems_version: !ruby/object:Gem::Requirement
223
223
  none: false
224
224
  requirements: