metriks-middleware 1.2.2 → 1.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,7 +2,7 @@ require 'metriks'
2
2
 
3
3
  module Metriks
4
4
  class Middleware
5
- VERSION = '1.2.2'
5
+ VERSION = '1.2.3'
6
6
 
7
7
  def initialize(app)
8
8
  @app = app
@@ -38,16 +38,19 @@ module Metriks
38
38
 
39
39
  def record_error_rate(env)
40
40
  original_callback = env['async.callback']
41
- env['async.callback'] = lambda do |env|
42
- record_error env.first
43
- original_callback.call env
41
+ env['async.callback'] = lambda do |(status, headers, body)|
42
+ record_error status
43
+ record_content_length headers
44
+ original_callback.call [status, headers, body]
44
45
  end
45
46
  end
46
47
 
47
48
  def call_downstream(env)
48
- response = @app.call env
49
- record_error response.first
50
- response
49
+ status, headers, body = @app.call env
50
+ record_error status
51
+ record_content_length headers
52
+
53
+ [status, headers, body]
51
54
  end
52
55
 
53
56
  def record_error(status)
@@ -60,6 +63,12 @@ module Metriks
60
63
  end
61
64
  end
62
65
 
66
+ def record_content_length(headers)
67
+ content_length = headers.fetch('Content-Length', 0).to_i
68
+ return unless content_length > 0
69
+ Metriks.histogram('responses.content_length').update(content_length)
70
+ end
71
+
63
72
  def response_timer
64
73
  Metriks.timer('app')
65
74
  end
@@ -13,8 +13,8 @@ Gem::Specification.new do |s|
13
13
  ## If your rubyforge_project name is different, then edit it and comment out
14
14
  ## the sub! line in the Rakefile
15
15
  s.name = 'metriks-middleware'
16
- s.version = '1.2.2'
17
- s.date = '2012-10-05'
16
+ s.version = '1.2.3'
17
+ s.date = '2012-10-24'
18
18
 
19
19
  ## Make sure your summary is short. The description may be as long
20
20
  ## as you like.
@@ -23,8 +23,8 @@ class AsyncAppTest < Test::Unit::TestCase
23
23
  end
24
24
 
25
25
  def test_calls_downstream
26
+ response = [200, { 'Header' => 'value' }, ['body']]
26
27
  downstream = mock
27
- response = stub first: 42
28
28
  downstream.expects(:call).with(@env).returns(response)
29
29
 
30
30
  actual_response = Metriks::Middleware.new(downstream).call(@env)
@@ -61,6 +61,23 @@ class AsyncAppTest < Test::Unit::TestCase
61
61
  assert_in_delta 0.1, time, 0.01
62
62
  end
63
63
 
64
+ def test_records_content_length
65
+ length_sync_app = lambda do |env| [200, {'Content-Length' => 42}, ['']] end
66
+ length_async_app = lambda do |env|
67
+ env['async.callback'].call [200, {'Content-Length' => 42}, ['']]
68
+ [-1, {}, ['']]
69
+ end
70
+
71
+ Metriks::Middleware.new(length_sync_app).call(@env.dup)
72
+ Metriks::Middleware.new(length_async_app).call(@env.dup)
73
+
74
+ count = Metriks.histogram('responses.content_length').count
75
+ size = Metriks.histogram('responses.content_length').mean
76
+
77
+ assert_equal 2, count
78
+ assert_equal 42, size
79
+ end
80
+
64
81
  def test_records_error_responses
65
82
  error_sync_app = lambda do |env| [500, {}, ['']] end
66
83
  error_async_app = lambda do |env|
@@ -20,8 +20,8 @@ class SyncAppTest < Test::Unit::TestCase
20
20
  end
21
21
 
22
22
  def test_calls_downstream
23
+ response = [200, {}, ['']]
23
24
  downstream = mock
24
- response = stub first: 200
25
25
  downstream.expects(:call).with(@env).returns(response)
26
26
 
27
27
  actual_response = Metriks::Middleware.new(downstream).call(@env)
@@ -45,6 +45,17 @@ class SyncAppTest < Test::Unit::TestCase
45
45
  assert_in_delta 0.1, time, 0.01
46
46
  end
47
47
 
48
+ def test_records_content_length
49
+ length_app = lambda do |env| [200, { 'Content-Length' => 42 }, ['']] end
50
+ Metriks::Middleware.new(length_app).call(@env)
51
+
52
+ count = Metriks.histogram('responses.content_length').count
53
+ size = Metriks.histogram('responses.content_length').mean
54
+
55
+ assert_equal 1, count
56
+ assert_equal 42, size
57
+ end
58
+
48
59
  def test_records_error_responses
49
60
  error_app = lambda do |env| [500, {}, ['']] end
50
61
  2.times { Metriks::Middleware.new(error_app).call(@env) }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metriks-middleware
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.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-10-05 00:00:00.000000000 Z
12
+ date: 2012-10-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: metriks