metriks-middleware 0.0.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- metriks-middleware (0.0.1)
4
+ metriks-middleware (1.0.0)
5
5
  metriks (~> 0.9.9)
6
6
 
7
7
  GEM
@@ -10,13 +10,17 @@ GEM
10
10
  atomic (1.0.1)
11
11
  avl_tree (1.1.3)
12
12
  hitimes (1.1.1)
13
+ metaclass (0.0.1)
13
14
  metriks (0.9.9)
14
15
  atomic (~> 1.0)
15
16
  avl_tree (~> 1.1.2)
16
17
  hitimes (~> 1.1)
18
+ mocha (0.11.4)
19
+ metaclass (~> 0.0.1)
17
20
 
18
21
  PLATFORMS
19
22
  ruby
20
23
 
21
24
  DEPENDENCIES
22
25
  metriks-middleware!
26
+ mocha (~> 0.11.4)
data/Rakefile CHANGED
@@ -52,7 +52,7 @@ task :default => :test
52
52
  require 'rake/testtask'
53
53
  Rake::TestTask.new(:test) do |test|
54
54
  test.libs << 'lib' << 'test'
55
- test.pattern = 'test/**/test_*.rb'
55
+ test.pattern = 'test/**/*_test.rb'
56
56
  test.verbose = true
57
57
  end
58
58
 
@@ -2,40 +2,38 @@ require 'metriks'
2
2
 
3
3
  module Metriks
4
4
  class Middleware
5
- VERSION = '0.0.1'
5
+ VERSION = '1.0.0'
6
6
 
7
- def initialize(app, options)
8
- @app = app
9
- @metric_prefix = options.fetch :metric_prefix, 'app'
7
+ def initialize(app, options = {})
8
+ @app = app
9
+ @name = options.fetch :name, 'app'
10
10
  end
11
11
 
12
12
  def call(env)
13
13
  time_response(env) do
14
- record_heroku_status env
14
+ record_heroku_queue_status env
15
15
  call_downstream env
16
16
  end
17
17
  end
18
18
 
19
19
  protected
20
20
 
21
- def time_response(env, &block)
21
+ def time_response(env, &handle_request)
22
22
  if env.has_key? 'async.close'
23
23
  context = response_timer.time
24
24
  env['async.close'].callback do context.stop end
25
- block.call
25
+ handle_request.call
26
26
  else
27
- response_timer.time &block
27
+ response_timer.time &handle_request
28
28
  end
29
29
  end
30
30
 
31
- def record_heroku_status(env)
31
+ def record_heroku_queue_status(env)
32
32
  queue_wait = env['HTTP_X_HEROKU_QUEUE_WAIT_TIME']
33
33
  queue_depth = env['HTTP_X_HEROKU_QUEUE_DEPTH']
34
- dynos_in_use = env['HTTP_X_HEROKU_DYNOS_IN_USE']
35
34
 
36
- Metriks.histogram("#{ @metric_prefix }.queue.wait") .update(queue_wait.to_i) if queue_wait
37
- Metriks.histogram("#{ @metric_prefix }.queue.depth").update(queue_depth.to_i) if queue_depth
38
- Metriks.histogram("#{ @metric_prefix }.dynos") .update(dynos_in_use.to_i) if dynos_in_use
35
+ Metriks.histogram("#{ @name }.queue.wait") .update(queue_wait.to_i) if queue_wait
36
+ Metriks.histogram("#{ @name }.queue.depth").update(queue_depth.to_i) if queue_depth
39
37
  end
40
38
 
41
39
  def call_downstream(env)
@@ -43,7 +41,7 @@ module Metriks
43
41
  end
44
42
 
45
43
  def response_timer
46
- Metriks.timer @metric_prefix
44
+ Metriks.timer(@name)
47
45
  end
48
46
  end
49
47
  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 = '0.0.1'
17
- s.date = '2012-06-30'
16
+ s.version = '1.0.0'
17
+ s.date = '2012-07-02'
18
18
 
19
19
  ## Make sure your summary is short. The description may be as long
20
20
  ## as you like.
@@ -43,6 +43,7 @@ Gem::Specification.new do |s|
43
43
 
44
44
  ## List your development dependencies here. Development dependencies are
45
45
  ## those that are only needed during development
46
+ s.add_development_dependency 'mocha', '~> 0.11.4'
46
47
 
47
48
  ## Leave this section as-is. It will be automatically generated from the
48
49
  ## contents of your Git repository via the gemspec task. DO NOT REMOVE
@@ -56,10 +57,12 @@ Gem::Specification.new do |s|
56
57
  Rakefile
57
58
  lib/metriks/middleware.rb
58
59
  metriks-middleware.gemspec
60
+ test/metriks-middleware_test.rb
61
+ test/test_helper.rb
59
62
  ]
60
63
  # = MANIFEST =
61
64
 
62
65
  ## Test files will be grabbed from the file list. Make sure the path glob
63
66
  ## matches what you actually use.
64
- # s.test_files = s.files.select { |path| path =~ /^test\/test_.*\.rb/ }
67
+ s.test_files = s.files.select { |path| path =~ /^test\/.*_test\.rb/ }
65
68
  end
@@ -0,0 +1,123 @@
1
+ require 'test_helper'
2
+
3
+ require 'metriks/middleware'
4
+
5
+ class MeterTest < Test::Unit::TestCase
6
+ def setup
7
+ @downstream = lambda do |env| end
8
+ @env = {}
9
+ end
10
+
11
+ def teardown
12
+ Metriks::Registry.default.each do |_, metric| metric.clear end
13
+ end
14
+
15
+ def sleepy_app
16
+ lambda do |env| sleep(0.1) end
17
+ end
18
+
19
+ def async_env(deferrable)
20
+ { 'async.close' => deferrable }
21
+ end
22
+
23
+ def test_calls_downstream
24
+ downstream = mock
25
+ response = stub
26
+ downstream.expects(:call).with(@env).returns(response)
27
+ actual_response = Metriks::Middleware.new(downstream).call(@env)
28
+
29
+ assert_equal response, actual_response
30
+ end
31
+
32
+ def test_async_app_calls_downstream
33
+ downstream = mock
34
+ response = stub
35
+
36
+ deferrable = Object.new
37
+ def deferrable.callback() yield end
38
+ env = { 'async.close' => deferrable }
39
+
40
+ downstream.expects(:call).with(env).returns(response)
41
+ actual_response = Metriks::Middleware.new(downstream).call(env)
42
+
43
+ assert_equal response, actual_response
44
+ end
45
+
46
+ def test_counts_throughput
47
+ Metriks::Middleware.new(@downstream).call(@env)
48
+ count = Metriks.timer('app').count
49
+
50
+ assert_equal 1, count
51
+ end
52
+
53
+ def test_times_downstream_response
54
+ Metriks::Middleware.new(sleepy_app).call(@env)
55
+ time = Metriks.timer('app').mean
56
+
57
+ assert_in_delta 0.1, time, 0.01
58
+ end
59
+
60
+ def test_async_app_stops_timer_on_close
61
+ deferrable = Object.new
62
+ def deferrable.callback(&block) @callback = block end
63
+ def deferrable.call_callback() @callback.call end
64
+ env = { 'async.close' => deferrable }
65
+
66
+ Metriks::Middleware.new(sleepy_app).call(env)
67
+ deferrable.call_callback
68
+ count = Metriks.timer('app').count
69
+ time = Metriks.timer('app').mean
70
+
71
+ assert_equal 1, count
72
+ assert_in_delta 0.1, time, 0.01
73
+ end
74
+
75
+ def test_omits_queue_metrics
76
+ Metriks::Middleware.new(@downstream).call(@env)
77
+ wait = Metriks.histogram('app.queue.wait').mean
78
+ depth = Metriks.histogram('app.queue.depth').mean
79
+
80
+ assert_equal 0, wait
81
+ assert_equal 0, depth
82
+ end
83
+
84
+ def test_records_heroku_queue
85
+ env = { 'HTTP_X_HEROKU_QUEUE_WAIT_TIME' => '42',
86
+ 'HTTP_X_HEROKU_QUEUE_DEPTH' => '24' }
87
+
88
+ Metriks::Middleware.new(@downstream).call(env)
89
+ wait = Metriks.histogram('app.queue.wait').mean
90
+ depth = Metriks.histogram('app.queue.depth').mean
91
+
92
+ assert_equal 42, wait
93
+ assert_equal 24, depth
94
+ end
95
+
96
+ def test_async_records_heroku_queue
97
+ deferrable = Object.new
98
+ def deferrable.callback() yield end
99
+ env = { 'async.close' => deferrable,
100
+ 'HTTP_X_HEROKU_QUEUE_WAIT_TIME' => '42',
101
+ 'HTTP_X_HEROKU_QUEUE_DEPTH' => '24' }
102
+
103
+ Metriks::Middleware.new(@downstream).call(env)
104
+ wait = Metriks.histogram('app.queue.wait').mean
105
+ depth = Metriks.histogram('app.queue.depth').mean
106
+
107
+ assert_equal 42, wait
108
+ assert_equal 24, depth
109
+ end
110
+
111
+ def test_name_merics
112
+ env = { 'HTTP_X_HEROKU_QUEUE_WAIT_TIME' => '42',
113
+ 'HTTP_X_HEROKU_QUEUE_DEPTH' => '24' }
114
+ Metriks::Middleware.new(@downstream, name: 'metric-name').call(env)
115
+ count = Metriks.timer('metric-name').count
116
+ wait = Metriks.histogram('metric-name.queue.wait').mean
117
+ depth = Metriks.histogram('metric-name.queue.depth').mean
118
+
119
+ assert_not_nil count
120
+ assert_not_nil wait
121
+ assert_not_nil depth
122
+ end
123
+ end
@@ -0,0 +1,4 @@
1
+ require 'test/unit'
2
+ require 'mocha'
3
+
4
+ Thread.abort_on_exception = true
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: 0.0.1
4
+ version: 1.0.0
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-06-30 00:00:00.000000000 Z
12
+ date: 2012-07-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: metriks
@@ -27,6 +27,22 @@ dependencies:
27
27
  - - ~>
28
28
  - !ruby/object:Gem::Version
29
29
  version: 0.9.9
30
+ - !ruby/object:Gem::Dependency
31
+ name: mocha
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 0.11.4
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 0.11.4
30
46
  description: Rack middleware to track throughput and response time with metriks.
31
47
  email: larry@marburger.cc
32
48
  executables: []
@@ -42,6 +58,8 @@ files:
42
58
  - Rakefile
43
59
  - lib/metriks/middleware.rb
44
60
  - metriks-middleware.gemspec
61
+ - test/metriks-middleware_test.rb
62
+ - test/test_helper.rb
45
63
  homepage: https://github.com/lmarburger/metriks-middleware
46
64
  licenses: []
47
65
  post_install_message:
@@ -67,4 +85,5 @@ rubygems_version: 1.8.23
67
85
  signing_key:
68
86
  specification_version: 2
69
87
  summary: Rack middleware for metriks
70
- test_files: []
88
+ test_files:
89
+ - test/metriks-middleware_test.rb