librato-rack 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  librato-rack
2
2
  =======
3
3
 
4
- [![Build Status](https://secure.travis-ci.org/librato/librato-rack.png?branch=master)](http://travis-ci.org/librato/librato-rack)
4
+ [![Build Status](https://secure.travis-ci.org/librato/librato-rack.png?branch=master)](http://travis-ci.org/librato/librato-rack) [![Code Climate](https://codeclimate.com/github/librato/librato-rack.png)](https://codeclimate.com/github/librato/librato-rack)
5
5
 
6
6
  *Note: librato-rack is currently in beta and is currently recommended primarily for early-adopter use*
7
7
 
@@ -62,7 +62,7 @@ If you want to do more complex configuration, use your own environment variables
62
62
  config.token = 'mytoken'
63
63
  # …more configuration
64
64
 
65
- use Librato::Rack, config
65
+ use Librato::Rack, :config => config
66
66
 
67
67
  See the configuration class for all available options.
68
68
 
data/lib/librato/rack.rb CHANGED
@@ -24,13 +24,18 @@ module Librato
24
24
  #
25
25
  # app = Rack::Builder.app do
26
26
  # use Librato::Rack
27
- # run lambda { |env| [200, {"Content-Type" => 'text/html'}, ["Hello!"]]}
27
+ # run lambda { |env| [200, {"Content-Type" => 'text/html'}, ["Hello!"]] }
28
28
  # end
29
29
  #
30
30
  class Rack
31
31
  attr_reader :config, :tracker
32
32
 
33
- def initialize(app, config = Configuration.new)
33
+ def initialize(app, options={})
34
+ if options.respond_to?(:tracker) # old-style single argument
35
+ config = options
36
+ else
37
+ config = options.fetch(:config, Configuration.new)
38
+ end
34
39
  @app, @config = app, config
35
40
  @tracker = Tracker.new(@config)
36
41
  Librato.register_tracker(@tracker) # create global reference
@@ -49,7 +54,7 @@ module Librato
49
54
 
50
55
  def check_log_output(env)
51
56
  return if @log_target
52
- if env.keys.include?('HTTP_X_HEROKU_QUEUE_DEPTH') # on heroku
57
+ if in_heroku_env?
53
58
  tracker.on_heroku = true
54
59
  default = ::Logger.new($stdout)
55
60
  else
@@ -59,6 +64,11 @@ module Librato
59
64
  @log_target = config.log_target
60
65
  end
61
66
 
67
+ def in_heroku_env?
68
+ # don't have any custom http vars anymore, check if hostname is UUID
69
+ Socket.gethostname =~ /[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}/i
70
+ end
71
+
62
72
  def process_request(env)
63
73
  time = Time.now
64
74
  begin
@@ -72,18 +82,11 @@ module Librato
72
82
  end
73
83
 
74
84
  def record_header_metrics(env)
75
- return unless env.keys.include?('HTTP_X_HEROKU_QUEUE_DEPTH')
76
-
77
- tracker.group 'rack.heroku' do |group|
78
- group.group 'queue' do |q|
79
- q.measure 'depth', env['HTTP_X_HEROKU_QUEUE_DEPTH'].to_f
80
- q.timing 'wait_time', env['HTTP_X_HEROKU_QUEUE_WAIT_TIME'].to_f
81
- end
82
- group.measure 'dynos', env['HTTP_X_HEROKU_DYNOS_IN_USE'].to_f
83
- end
85
+ # TODO: track generalized queue wait
84
86
  end
85
87
 
86
88
  def record_request_metrics(status, duration)
89
+ return if config.disable_rack_metrics
87
90
  tracker.group 'rack.request' do |group|
88
91
  group.increment 'total'
89
92
  group.timing 'time', duration
@@ -100,6 +103,7 @@ module Librato
100
103
  end
101
104
 
102
105
  def record_exception(exception)
106
+ return if config.disable_rack_metrics
103
107
  tracker.increment 'rack.request.exceptions'
104
108
  end
105
109
 
@@ -11,7 +11,8 @@ module Librato
11
11
  #
12
12
  class Configuration
13
13
  attr_accessor :user, :token, :api_endpoint, :tracker, :source_pids,
14
- :log_level, :flush_interval, :log_target
14
+ :log_level, :flush_interval, :log_target,
15
+ :disable_rack_metrics
15
16
  attr_reader :prefix, :source
16
17
 
17
18
  def initialize
@@ -1,5 +1,5 @@
1
1
  module Librato
2
2
  class Rack
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -0,0 +1,14 @@
1
+ require 'bundler/setup'
2
+ require 'librato-rack'
3
+
4
+ config = Librato::Rack::Configuration.new
5
+ config.prefix = 'deprecated'
6
+
7
+ # old-style single-argument assignment
8
+ use Librato::Rack, config
9
+
10
+ def application(env)
11
+ [200, {"Content-Type" => 'text/html'}, ["Hello!"]]
12
+ end
13
+
14
+ run method(:application)
@@ -0,0 +1,18 @@
1
+ require 'bundler/setup'
2
+ require 'librato-rack'
3
+
4
+ config = Librato::Rack::Configuration.new
5
+ config.disable_rack_metrics = true
6
+
7
+ use Librato::Rack, :config => config
8
+
9
+ def application(env)
10
+ case env['PATH_INFO']
11
+ when '/exception'
12
+ raise 'exception raised!'
13
+ else
14
+ [200, {"Content-Type" => 'text/html'}, ["Hello!"]]
15
+ end
16
+ end
17
+
18
+ run method(:application)
@@ -0,0 +1,35 @@
1
+ require 'test_helper'
2
+ require 'rack/test'
3
+
4
+ # Tests for deprecated functionality
5
+ #
6
+ class DeprecatedTest < MiniTest::Unit::TestCase
7
+ include Rack::Test::Methods
8
+
9
+ def app
10
+ Rack::Builder.parse_file('test/apps/deprecated.ru').first
11
+ end
12
+
13
+ def teardown
14
+ # clear metrics before each run
15
+ aggregate.delete_all
16
+ counters.delete_all
17
+ end
18
+
19
+ def test_deprecated_config_form
20
+ get '/'
21
+ assert_equal 'deprecated', Librato.tracker.config.prefix
22
+ assert_equal 1, aggregate['deprecated.rack.request.time'][:count]
23
+ end
24
+
25
+ private
26
+
27
+ def aggregate
28
+ Librato.tracker.collector.aggregate
29
+ end
30
+
31
+ def counters
32
+ Librato.tracker.collector.counters
33
+ end
34
+
35
+ end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+ require 'rack/test'
3
+
4
+ # Tests for universal tracking for all request paths
5
+ #
6
+ class NoStatsTest < MiniTest::Unit::TestCase
7
+ include Rack::Test::Methods
8
+
9
+ def app
10
+ Rack::Builder.parse_file('test/apps/no_stats.ru').first
11
+ end
12
+
13
+ def teardown
14
+ # clear metrics before each run
15
+ aggregate.delete_all
16
+ counters.delete_all
17
+ end
18
+
19
+ def test_no_standard_counters
20
+ get '/'
21
+ assert last_response.ok?
22
+
23
+ assert_equal nil, counters["rack.request.total"]
24
+ assert_equal nil, counters["rack.request.status.200"]
25
+ assert_equal nil, counters["rack.request.status.2xx"]
26
+ end
27
+
28
+ def test_no_standard_measures
29
+ get '/'
30
+ assert last_response.ok?
31
+
32
+ assert_equal nil, aggregate["rack.request.time"]
33
+ end
34
+
35
+ def test_dont_track_exceptions
36
+ begin
37
+ get '/exception'
38
+ rescue RuntimeError => e
39
+ raise unless e.message == 'exception raised!'
40
+ end
41
+ assert_equal nil, counters["rack.request.exceptions"]
42
+ end
43
+
44
+ private
45
+
46
+ def aggregate
47
+ Librato.tracker.collector.aggregate
48
+ end
49
+
50
+ def counters
51
+ Librato.tracker.collector.counters
52
+ end
53
+
54
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: librato-rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.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: 2013-02-24 00:00:00.000000000 Z
12
+ date: 2013-03-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: librato-metrics
@@ -69,9 +69,11 @@ files:
69
69
  - README.md
70
70
  - test/apps/basic.ru
71
71
  - test/apps/custom.ru
72
- - test/apps/heroku.ru
72
+ - test/apps/deprecated.ru
73
+ - test/apps/no_stats.ru
73
74
  - test/integration/custom_test.rb
74
- - test/integration/heroku_test.rb
75
+ - test/integration/deprecated_test.rb
76
+ - test/integration/no_stats_test.rb
75
77
  - test/integration/request_test.rb
76
78
  - test/remote/tracker_test.rb
77
79
  - test/test_helper.rb
@@ -97,7 +99,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
97
99
  version: '0'
98
100
  segments:
99
101
  - 0
100
- hash: -1368002432416974153
102
+ hash: 2009831152017891350
101
103
  required_rubygems_version: !ruby/object:Gem::Requirement
102
104
  none: false
103
105
  requirements:
@@ -106,7 +108,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
106
108
  version: '0'
107
109
  segments:
108
110
  - 0
109
- hash: -1368002432416974153
111
+ hash: 2009831152017891350
110
112
  requirements: []
111
113
  rubyforge_project:
112
114
  rubygems_version: 1.8.25
@@ -116,9 +118,11 @@ summary: Use Librato Metrics with your rack application
116
118
  test_files:
117
119
  - test/apps/basic.ru
118
120
  - test/apps/custom.ru
119
- - test/apps/heroku.ru
121
+ - test/apps/deprecated.ru
122
+ - test/apps/no_stats.ru
120
123
  - test/integration/custom_test.rb
121
- - test/integration/heroku_test.rb
124
+ - test/integration/deprecated_test.rb
125
+ - test/integration/no_stats_test.rb
122
126
  - test/integration/request_test.rb
123
127
  - test/remote/tracker_test.rb
124
128
  - test/test_helper.rb
data/test/apps/heroku.ru DELETED
@@ -1,27 +0,0 @@
1
- require 'bundler/setup'
2
- require 'librato-rack'
3
-
4
- # Simulate the environment variables Heroku passes along
5
- # with each request
6
- #
7
- class FakeHeroku
8
- def initialize(app)
9
- @app = app
10
- end
11
-
12
- def call(env)
13
- env['HTTP_X_HEROKU_QUEUE_DEPTH'] = rand(4)
14
- env['HTTP_X_HEROKU_QUEUE_WAIT_TIME'] = rand(0.1)
15
- env['HTTP_X_HEROKU_DYNOS_IN_USE'] = 2
16
- @app.call(env)
17
- end
18
- end
19
-
20
- use FakeHeroku
21
- use Librato::Rack
22
-
23
- def application(env)
24
- [200, {"Content-Type" => 'text/html'}, ["Hello!"]]
25
- end
26
-
27
- run method(:application)
@@ -1,36 +0,0 @@
1
- require 'test_helper'
2
- require 'rack/test'
3
-
4
- # Tests for universal tracking for all request paths
5
- #
6
- class HerokuTest < MiniTest::Unit::TestCase
7
- include Rack::Test::Methods
8
-
9
- def app
10
- Rack::Builder.parse_file('test/apps/heroku.ru').first
11
- end
12
-
13
- def teardown
14
- # clear metrics before each run
15
- aggregate.delete_all
16
- counters.delete_all
17
- end
18
-
19
- def test_heroku_metrics
20
- get '/'
21
- assert_equal 1, aggregate['rack.heroku.queue.depth'][:count]
22
- assert_equal 1, aggregate['rack.heroku.queue.wait_time'][:count]
23
- assert_equal 1, aggregate['rack.heroku.dynos'][:count]
24
- end
25
-
26
- private
27
-
28
- def aggregate
29
- Librato.tracker.collector.aggregate
30
- end
31
-
32
- def counters
33
- Librato.tracker.collector.counters
34
- end
35
-
36
- end