librato-rails 0.9.0 → 0.10.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +14 -0
- data/FAQ.md +25 -0
- data/README.md +11 -2
- data/lib/librato/rails/configuration.rb +26 -29
- data/lib/librato/rails/railtie.rb +30 -5
- data/lib/librato/rails/subscribers/cache.rb +22 -0
- data/lib/librato/rails/subscribers/controller.rb +56 -0
- data/lib/librato/rails/subscribers/mail.rb +18 -0
- data/lib/librato/rails/subscribers/render.rb +28 -0
- data/lib/librato/rails/subscribers/sql.rb +24 -0
- data/lib/librato/rails/subscribers.rb +14 -69
- data/lib/librato/rails/tracker.rb +13 -0
- data/lib/librato/rails/version.rb +1 -1
- data/lib/librato/rails.rb +9 -214
- data/test/dummy/app/assets/javascripts/application.js +0 -3
- data/test/dummy/app/controllers/cache_controller.rb +44 -0
- data/test/dummy/app/controllers/render_controller.rb +4 -0
- data/test/dummy/app/models/user.rb +7 -5
- data/test/dummy/app/views/render/_first.html.erb +1 -0
- data/test/dummy/app/views/render/_second.html.erb +1 -0
- data/test/dummy/app/views/render/partial.html.erb +2 -0
- data/test/dummy/app/views/render/template.html.erb +1 -0
- data/test/dummy/config/application.rb +8 -6
- data/test/dummy/config/environments/test.rb +1 -1
- data/test/dummy/config/routes.rb +12 -3
- data/test/integration/cache_test.rb +40 -0
- data/test/integration/mail_test.rb +2 -4
- data/test/integration/render_test.rb +27 -0
- data/test/integration/request_test.rb +15 -11
- data/test/integration/sql_test.rb +6 -6
- data/test/support/integration_case.rb +11 -7
- data/test/unit/configuration_test.rb +63 -73
- data/test/unit/tracker_test.rb +15 -0
- metadata +36 -53
- data/lib/librato/rack/middleware.rb +0 -47
- data/lib/librato/rack.rb +0 -4
- data/lib/librato/rails/aggregator.rb +0 -95
- data/lib/librato/rails/collector.rb +0 -45
- data/lib/librato/rails/counter_cache.rb +0 -122
- data/lib/librato/rails/group.rb +0 -27
- data/lib/librato/rails/logging.rb +0 -77
- data/lib/librato/rails/validating_queue.rb +0 -31
- data/lib/librato/rails/worker.rb +0 -54
- data/lib/tasks/metrics-rails_tasks.rake +0 -4
- data/test/librato-rails_test.rb +0 -44
- data/test/remote/rails_remote_test.rb +0 -193
- data/test/unit/aggregator_test.rb +0 -53
- data/test/unit/counter_cache_test.rb +0 -90
- data/test/unit/group_test.rb +0 -54
- data/test/unit/middleware_test.rb +0 -82
- data/test/unit/worker_test.rb +0 -31
@@ -1,85 +1,75 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
3
|
+
module Librato
|
4
|
+
module Rails
|
5
|
+
class ConfigTest < MiniTest::Unit::TestCase
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
7
|
+
def teardown
|
8
|
+
ENV.delete('LIBRATO_USER')
|
9
|
+
ENV.delete('LIBRATO_TOKEN')
|
10
|
+
ENV.delete('LIBRATO_SOURCE')
|
11
|
+
# legacy
|
12
|
+
ENV.delete('LIBRATO_METRICS_USER')
|
13
|
+
ENV.delete('LIBRATO_METRICS_TOKEN')
|
14
|
+
ENV.delete('LIBRATO_METRICS_SOURCE')
|
15
|
+
end
|
8
16
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
Librato::Rails.explicit_source = nil
|
20
|
-
end
|
17
|
+
def test_environmental_variable_config
|
18
|
+
ENV['LIBRATO_USER'] = 'foo@bar.com'
|
19
|
+
ENV['LIBRATO_TOKEN'] = 'api_key'
|
20
|
+
ENV['LIBRATO_SOURCE'] = 'source'
|
21
|
+
config = Configuration.new
|
22
|
+
assert_equal 'foo@bar.com', config.user
|
23
|
+
assert_equal 'api_key', config.token
|
24
|
+
assert_equal 'source', config.source
|
25
|
+
assert config.explicit_source?, 'source is explicit'
|
26
|
+
end
|
21
27
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
28
|
+
def test_legacy_env_variable_config
|
29
|
+
ENV['LIBRATO_METRICS_USER'] = 'foo@bar.com'
|
30
|
+
ENV['LIBRATO_METRICS_TOKEN'] = 'api_key'
|
31
|
+
ENV['LIBRATO_METRICS_SOURCE'] = 'source'
|
32
|
+
config = Configuration.new
|
33
|
+
assert_equal 'foo@bar.com', config.user
|
34
|
+
assert_equal 'api_key', config.token
|
35
|
+
assert_equal 'source', config.source
|
36
|
+
assert config.explicit_source?, 'source is explicit'
|
37
|
+
end
|
32
38
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
39
|
+
def test_config_file_config
|
40
|
+
config = fixture_config
|
41
|
+
assert_equal 'test@bar.com', config.user
|
42
|
+
assert_equal 'test api key', config.token
|
43
|
+
assert_equal 'rails-test', config.prefix
|
44
|
+
assert_equal 30, config.flush_interval
|
45
|
+
assert_equal 'custom-1', config.source
|
46
|
+
assert_equal false, config.source_pids
|
47
|
+
assert config.explicit_source?, 'source is explicit'
|
48
|
+
end
|
43
49
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
assert_equal 'custom-1', Librato::Rails.source
|
51
|
-
assert_equal false, Librato::Rails.source_pids
|
52
|
-
assert Librato::Rails.explicit_source, 'source is explicit'
|
53
|
-
end
|
54
|
-
end
|
50
|
+
def test_implicit_source
|
51
|
+
config = fixture_config('simple')
|
52
|
+
assert_equal 'test@bar.com', config.user
|
53
|
+
assert_equal 'test api key', config.token
|
54
|
+
assert !config.explicit_source?, 'source is not explicit'
|
55
|
+
end
|
55
56
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
57
|
+
def test_environmental_and_config_file_config
|
58
|
+
ENV['LIBRATO_METRICS_USER'] = 'foo@bar.com'
|
59
|
+
ENV['LIBRATO_METRICS_TOKEN'] = 'api_key'
|
60
|
+
ENV['LIBRATO_METRICS_SOURCE'] = 'source'
|
61
|
+
config = fixture_config
|
62
|
+
assert_equal 'test@bar.com', config.user # from config file
|
63
|
+
assert_equal 'test api key', config.token # from config file
|
64
|
+
assert_equal 'rails-test', config.prefix # from config file
|
65
|
+
assert_equal 30, config.flush_interval # from config file
|
66
|
+
end
|
63
67
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
with_fixture_config do
|
69
|
-
assert_equal 'test@bar.com', Librato::Rails.user # from config file
|
70
|
-
assert_equal 'test api key', Librato::Rails.token # from config file
|
71
|
-
assert_equal 'rails-test', Librato::Rails.prefix # from config file
|
72
|
-
assert_equal 30, Librato::Rails.flush_interval # from config file
|
73
|
-
end
|
74
|
-
end
|
68
|
+
def fixture_config(file='librato')
|
69
|
+
fixture_config = File.join(File.dirname(__FILE__), "../fixtures/config/#{file}.yml")
|
70
|
+
Configuration.new(:config_file => fixture_config)
|
71
|
+
end
|
75
72
|
|
76
|
-
|
77
|
-
fixture_config = File.join(File.dirname(__FILE__), "../fixtures/config/#{file}.yml")
|
78
|
-
previous, Librato::Rails.config_file = Librato::Rails.config_file, fixture_config
|
79
|
-
Librato::Rails.check_config
|
80
|
-
yield
|
81
|
-
ensure
|
82
|
-
Librato::Rails.config_file = previous
|
73
|
+
end
|
83
74
|
end
|
84
|
-
|
85
75
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Librato
|
4
|
+
module Rails
|
5
|
+
class TrackerTest < MiniTest::Unit::TestCase
|
6
|
+
|
7
|
+
def test_user_agent
|
8
|
+
config = Configuration.new
|
9
|
+
tracker = Tracker.new(config)
|
10
|
+
assert_match /librato\-rails/, tracker.send(:user_agent)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: librato-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.10.0.pre1
|
5
|
+
prerelease: 7
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Matt Sanders
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-08-
|
12
|
+
date: 2013-08-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -28,13 +28,13 @@ dependencies:
|
|
28
28
|
- !ruby/object:Gem::Version
|
29
29
|
version: '3.0'
|
30
30
|
- !ruby/object:Gem::Dependency
|
31
|
-
name: librato-
|
31
|
+
name: librato-rack
|
32
32
|
requirement: !ruby/object:Gem::Requirement
|
33
33
|
none: false
|
34
34
|
requirements:
|
35
35
|
- - ~>
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version:
|
37
|
+
version: 0.4.1
|
38
38
|
type: :runtime
|
39
39
|
prerelease: false
|
40
40
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -42,7 +42,7 @@ dependencies:
|
|
42
42
|
requirements:
|
43
43
|
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version:
|
45
|
+
version: 0.4.1
|
46
46
|
- !ruby/object:Gem::Dependency
|
47
47
|
name: sqlite3
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
@@ -75,22 +75,6 @@ dependencies:
|
|
75
75
|
- - ~>
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: 2.0.3
|
78
|
-
- !ruby/object:Gem::Dependency
|
79
|
-
name: minitest
|
80
|
-
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
|
-
requirements:
|
83
|
-
- - ~>
|
84
|
-
- !ruby/object:Gem::Version
|
85
|
-
version: 3.4.0
|
86
|
-
type: :development
|
87
|
-
prerelease: false
|
88
|
-
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
|
-
requirements:
|
91
|
-
- - ~>
|
92
|
-
- !ruby/object:Gem::Version
|
93
|
-
version: 3.4.0
|
94
78
|
description: Report key app statistics to the Librato Metrics service and easily track
|
95
79
|
your own custom metrics.
|
96
80
|
email:
|
@@ -99,26 +83,23 @@ executables: []
|
|
99
83
|
extensions: []
|
100
84
|
extra_rdoc_files: []
|
101
85
|
files:
|
102
|
-
- lib/librato/rack/middleware.rb
|
103
|
-
- lib/librato/rack.rb
|
104
|
-
- lib/librato/rails/aggregator.rb
|
105
|
-
- lib/librato/rails/collector.rb
|
106
86
|
- lib/librato/rails/configuration.rb
|
107
|
-
- lib/librato/rails/counter_cache.rb
|
108
|
-
- lib/librato/rails/group.rb
|
109
|
-
- lib/librato/rails/logging.rb
|
110
87
|
- lib/librato/rails/railtie.rb
|
88
|
+
- lib/librato/rails/subscribers/cache.rb
|
89
|
+
- lib/librato/rails/subscribers/controller.rb
|
90
|
+
- lib/librato/rails/subscribers/mail.rb
|
91
|
+
- lib/librato/rails/subscribers/render.rb
|
92
|
+
- lib/librato/rails/subscribers/sql.rb
|
111
93
|
- lib/librato/rails/subscribers.rb
|
112
|
-
- lib/librato/rails/
|
94
|
+
- lib/librato/rails/tracker.rb
|
113
95
|
- lib/librato/rails/version.rb
|
114
|
-
- lib/librato/rails/worker.rb
|
115
96
|
- lib/librato/rails.rb
|
116
97
|
- lib/librato-rails.rb
|
117
|
-
- lib/tasks/metrics-rails_tasks.rake
|
118
98
|
- LICENSE
|
119
99
|
- Rakefile
|
120
100
|
- README.md
|
121
101
|
- CHANGELOG.md
|
102
|
+
- FAQ.md
|
122
103
|
- test/dummy/app/assets/javascripts/application.js
|
123
104
|
- test/dummy/app/assets/javascripts/home.js
|
124
105
|
- test/dummy/app/assets/javascripts/status.js
|
@@ -126,7 +107,9 @@ files:
|
|
126
107
|
- test/dummy/app/assets/stylesheets/home.css
|
127
108
|
- test/dummy/app/assets/stylesheets/status.css
|
128
109
|
- test/dummy/app/controllers/application_controller.rb
|
110
|
+
- test/dummy/app/controllers/cache_controller.rb
|
129
111
|
- test/dummy/app/controllers/home_controller.rb
|
112
|
+
- test/dummy/app/controllers/render_controller.rb
|
130
113
|
- test/dummy/app/controllers/status_controller.rb
|
131
114
|
- test/dummy/app/controllers/user_controller.rb
|
132
115
|
- test/dummy/app/helpers/application_helper.rb
|
@@ -136,6 +119,10 @@ files:
|
|
136
119
|
- test/dummy/app/models/user.rb
|
137
120
|
- test/dummy/app/views/home/index.html.erb
|
138
121
|
- test/dummy/app/views/layouts/application.html.erb
|
122
|
+
- test/dummy/app/views/render/_first.html.erb
|
123
|
+
- test/dummy/app/views/render/_second.html.erb
|
124
|
+
- test/dummy/app/views/render/partial.html.erb
|
125
|
+
- test/dummy/app/views/render/template.html.erb
|
139
126
|
- test/dummy/app/views/status/code.html.erb
|
140
127
|
- test/dummy/app/views/user_mailer/welcome_email.text.erb
|
141
128
|
- test/dummy/config/application.rb
|
@@ -167,22 +154,19 @@ files:
|
|
167
154
|
- test/dummy/script/rails
|
168
155
|
- test/fixtures/config/librato.yml
|
169
156
|
- test/fixtures/config/simple.yml
|
157
|
+
- test/integration/cache_test.rb
|
170
158
|
- test/integration/custom_test.rb
|
171
159
|
- test/integration/mail_test.rb
|
160
|
+
- test/integration/render_test.rb
|
172
161
|
- test/integration/request_test.rb
|
173
162
|
- test/integration/sql_test.rb
|
174
|
-
- test/librato-rails_test.rb
|
175
|
-
- test/remote/rails_remote_test.rb
|
176
163
|
- test/support/integration_case.rb
|
177
164
|
- test/test_helper.rb
|
178
|
-
- test/unit/aggregator_test.rb
|
179
165
|
- test/unit/configuration_test.rb
|
180
|
-
- test/unit/
|
181
|
-
- test/unit/group_test.rb
|
182
|
-
- test/unit/middleware_test.rb
|
183
|
-
- test/unit/worker_test.rb
|
166
|
+
- test/unit/tracker_test.rb
|
184
167
|
homepage: https://github.com/librato/librato-rails
|
185
|
-
licenses:
|
168
|
+
licenses:
|
169
|
+
- BSD 3-clause
|
186
170
|
post_install_message:
|
187
171
|
rdoc_options: []
|
188
172
|
require_paths:
|
@@ -195,16 +179,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
195
179
|
version: '0'
|
196
180
|
segments:
|
197
181
|
- 0
|
198
|
-
hash:
|
182
|
+
hash: -1394071796353976924
|
199
183
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
200
184
|
none: false
|
201
185
|
requirements:
|
202
|
-
- - ! '
|
186
|
+
- - ! '>'
|
203
187
|
- !ruby/object:Gem::Version
|
204
|
-
version:
|
205
|
-
segments:
|
206
|
-
- 0
|
207
|
-
hash: 928415932724141645
|
188
|
+
version: 1.3.1
|
208
189
|
requirements: []
|
209
190
|
rubyforge_project:
|
210
191
|
rubygems_version: 1.8.23
|
@@ -219,7 +200,9 @@ test_files:
|
|
219
200
|
- test/dummy/app/assets/stylesheets/home.css
|
220
201
|
- test/dummy/app/assets/stylesheets/status.css
|
221
202
|
- test/dummy/app/controllers/application_controller.rb
|
203
|
+
- test/dummy/app/controllers/cache_controller.rb
|
222
204
|
- test/dummy/app/controllers/home_controller.rb
|
205
|
+
- test/dummy/app/controllers/render_controller.rb
|
223
206
|
- test/dummy/app/controllers/status_controller.rb
|
224
207
|
- test/dummy/app/controllers/user_controller.rb
|
225
208
|
- test/dummy/app/helpers/application_helper.rb
|
@@ -229,6 +212,10 @@ test_files:
|
|
229
212
|
- test/dummy/app/models/user.rb
|
230
213
|
- test/dummy/app/views/home/index.html.erb
|
231
214
|
- test/dummy/app/views/layouts/application.html.erb
|
215
|
+
- test/dummy/app/views/render/_first.html.erb
|
216
|
+
- test/dummy/app/views/render/_second.html.erb
|
217
|
+
- test/dummy/app/views/render/partial.html.erb
|
218
|
+
- test/dummy/app/views/render/template.html.erb
|
232
219
|
- test/dummy/app/views/status/code.html.erb
|
233
220
|
- test/dummy/app/views/user_mailer/welcome_email.text.erb
|
234
221
|
- test/dummy/config/application.rb
|
@@ -260,17 +247,13 @@ test_files:
|
|
260
247
|
- test/dummy/script/rails
|
261
248
|
- test/fixtures/config/librato.yml
|
262
249
|
- test/fixtures/config/simple.yml
|
250
|
+
- test/integration/cache_test.rb
|
263
251
|
- test/integration/custom_test.rb
|
264
252
|
- test/integration/mail_test.rb
|
253
|
+
- test/integration/render_test.rb
|
265
254
|
- test/integration/request_test.rb
|
266
255
|
- test/integration/sql_test.rb
|
267
|
-
- test/librato-rails_test.rb
|
268
|
-
- test/remote/rails_remote_test.rb
|
269
256
|
- test/support/integration_case.rb
|
270
257
|
- test/test_helper.rb
|
271
|
-
- test/unit/aggregator_test.rb
|
272
258
|
- test/unit/configuration_test.rb
|
273
|
-
- test/unit/
|
274
|
-
- test/unit/group_test.rb
|
275
|
-
- test/unit/middleware_test.rb
|
276
|
-
- test/unit/worker_test.rb
|
259
|
+
- test/unit/tracker_test.rb
|
@@ -1,47 +0,0 @@
|
|
1
|
-
class Librato::Rack::Middleware
|
2
|
-
def initialize(app, metrics = Librato::Rails)
|
3
|
-
@app, @metrics = app, metrics
|
4
|
-
end
|
5
|
-
|
6
|
-
def call(env)
|
7
|
-
@metrics.check_worker
|
8
|
-
|
9
|
-
header_metrics env
|
10
|
-
|
11
|
-
time = Time.now
|
12
|
-
response = @app.call(env)
|
13
|
-
duration = (Time.now - time) * 1000.0
|
14
|
-
|
15
|
-
request_metrics response.first, duration
|
16
|
-
|
17
|
-
response
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def header_metrics(env)
|
23
|
-
return unless env.keys.include?('HTTP_X_HEROKU_QUEUE_DEPTH')
|
24
|
-
|
25
|
-
@metrics.group 'rack.heroku' do |group|
|
26
|
-
group.measure 'queue.depth', env['HTTP_X_HEROKU_QUEUE_DEPTH'].to_f
|
27
|
-
group.timing 'queue.wait_time', env['HTTP_X_HEROKU_QUEUE_WAIT_TIME'].to_f
|
28
|
-
group.measure 'queue.dynos', env['HTTP_X_HEROKU_DYNOS_IN_USE'].to_f
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def request_metrics(status, duration)
|
33
|
-
@metrics.group 'rack.request' do |group|
|
34
|
-
group.increment 'total'
|
35
|
-
group.timing 'time', duration
|
36
|
-
group.increment 'slow' if duration > 200.0
|
37
|
-
|
38
|
-
group.group 'status' do |s|
|
39
|
-
s.increment status
|
40
|
-
s.increment "#{status.to_s[0]}xx"
|
41
|
-
|
42
|
-
s.timing "#{status}.time", duration
|
43
|
-
s.timing "#{status.to_s[0]}xx.time", duration
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
data/lib/librato/rack.rb
DELETED
@@ -1,95 +0,0 @@
|
|
1
|
-
module Librato
|
2
|
-
module Rails
|
3
|
-
class Aggregator
|
4
|
-
extend Forwardable
|
5
|
-
|
6
|
-
def_delegators :@cache, :empty?, :prefix, :prefix=
|
7
|
-
|
8
|
-
def initialize(options={})
|
9
|
-
@cache = Librato::Metrics::Aggregator.new(:prefix => options[:prefix])
|
10
|
-
@lock = Mutex.new
|
11
|
-
end
|
12
|
-
|
13
|
-
def [](key)
|
14
|
-
fetch(key)
|
15
|
-
end
|
16
|
-
|
17
|
-
def fetch(key, options={})
|
18
|
-
return nil if @cache.empty?
|
19
|
-
gauges = nil
|
20
|
-
source = options[:source]
|
21
|
-
@lock.synchronize { gauges = @cache.queued[:gauges] }
|
22
|
-
gauges.each do |metric|
|
23
|
-
if metric[:name] == key.to_s
|
24
|
-
return metric if !source && !metric[:source]
|
25
|
-
return metric if source.to_s == metric[:source]
|
26
|
-
end
|
27
|
-
end
|
28
|
-
nil
|
29
|
-
end
|
30
|
-
|
31
|
-
def delete_all
|
32
|
-
@lock.synchronize { @cache.clear }
|
33
|
-
end
|
34
|
-
|
35
|
-
# transfer all measurements to queue and reset internal status
|
36
|
-
def flush_to(queue, options={})
|
37
|
-
queued = nil
|
38
|
-
@lock.synchronize do
|
39
|
-
return if @cache.empty?
|
40
|
-
queued = @cache.queued
|
41
|
-
@cache.clear
|
42
|
-
end
|
43
|
-
queue.merge!(queued) if queued
|
44
|
-
end
|
45
|
-
|
46
|
-
# @example Simple measurement
|
47
|
-
# measure 'sources_returned', sources.length
|
48
|
-
#
|
49
|
-
# @example Simple timing in milliseconds
|
50
|
-
# timing 'twitter.lookup', 2.31
|
51
|
-
#
|
52
|
-
# @example Block-based timing
|
53
|
-
# timing 'db.query' do
|
54
|
-
# do_my_query
|
55
|
-
# end
|
56
|
-
#
|
57
|
-
# @example Custom source
|
58
|
-
# measure 'user.all_orders', user.order_count, :source => user.id
|
59
|
-
#
|
60
|
-
def measure(*args, &block)
|
61
|
-
options = {}
|
62
|
-
event = args[0].to_s
|
63
|
-
returned = nil
|
64
|
-
|
65
|
-
# handle block or specified argument
|
66
|
-
if block_given?
|
67
|
-
start = Time.now
|
68
|
-
returned = yield
|
69
|
-
value = ((Time.now - start) * 1000.0).to_i
|
70
|
-
elsif args[1]
|
71
|
-
value = args[1]
|
72
|
-
else
|
73
|
-
raise "no value provided"
|
74
|
-
end
|
75
|
-
|
76
|
-
# detect options hash if present
|
77
|
-
if args.length > 1 and args[-1].respond_to?(:each)
|
78
|
-
options = args[-1]
|
79
|
-
end
|
80
|
-
source = options[:source]
|
81
|
-
|
82
|
-
@lock.synchronize do
|
83
|
-
if source
|
84
|
-
@cache.add event => {:source => source, :value => value}
|
85
|
-
else
|
86
|
-
@cache.add event => value
|
87
|
-
end
|
88
|
-
end
|
89
|
-
returned
|
90
|
-
end
|
91
|
-
alias :timing :measure
|
92
|
-
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
@@ -1,45 +0,0 @@
|
|
1
|
-
|
2
|
-
# an abstract collector object which can be given measurement values
|
3
|
-
# and can periodically report those values back to the Metrics service
|
4
|
-
|
5
|
-
module Librato
|
6
|
-
module Rails
|
7
|
-
class Collector
|
8
|
-
extend Forwardable
|
9
|
-
|
10
|
-
def_delegators :counters, :increment
|
11
|
-
def_delegators :aggregate, :measure, :timing
|
12
|
-
|
13
|
-
# access to internal aggregator object
|
14
|
-
def aggregate
|
15
|
-
@aggregator_cache ||= Aggregator.new(:prefix => @prefix)
|
16
|
-
end
|
17
|
-
|
18
|
-
# access to internal counters object
|
19
|
-
def counters
|
20
|
-
@counter_cache ||= CounterCache.new
|
21
|
-
end
|
22
|
-
|
23
|
-
# remove any accumulated but unsent metrics
|
24
|
-
def delete_all
|
25
|
-
aggregate.delete_all
|
26
|
-
counters.delete_all
|
27
|
-
end
|
28
|
-
|
29
|
-
def group(prefix)
|
30
|
-
group = Group.new(prefix)
|
31
|
-
yield group
|
32
|
-
end
|
33
|
-
|
34
|
-
# update prefix
|
35
|
-
def prefix=(new_prefix)
|
36
|
-
@prefix = new_prefix
|
37
|
-
aggregate.prefix = @prefix
|
38
|
-
end
|
39
|
-
|
40
|
-
def prefix
|
41
|
-
@prefix
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
@@ -1,122 +0,0 @@
|
|
1
|
-
module Librato
|
2
|
-
module Rails
|
3
|
-
|
4
|
-
class CounterCache
|
5
|
-
DEFAULT_SOURCE = '%%'
|
6
|
-
|
7
|
-
extend Forwardable
|
8
|
-
|
9
|
-
def_delegators :@cache, :empty?
|
10
|
-
|
11
|
-
def initialize
|
12
|
-
@cache = {}
|
13
|
-
@lock = Mutex.new
|
14
|
-
@sporadics = {}
|
15
|
-
end
|
16
|
-
|
17
|
-
# Retrieve the current value for a given metric. This is a short
|
18
|
-
# form for convenience which only retrieves metrics with no custom
|
19
|
-
# source specified. For more options see #fetch.
|
20
|
-
#
|
21
|
-
# @param [String|Symbol] key metric name
|
22
|
-
# @return [Integer|Float] current value
|
23
|
-
def [](key)
|
24
|
-
@lock.synchronize do
|
25
|
-
@cache[key.to_s][DEFAULT_SOURCE]
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
# removes all tracked metrics. note this removes all measurement
|
30
|
-
# data AND metric names any continuously tracked metrics will not
|
31
|
-
# report until they get another measurement
|
32
|
-
def delete_all
|
33
|
-
@lock.synchronize { @cache.clear }
|
34
|
-
end
|
35
|
-
|
36
|
-
|
37
|
-
def fetch(key, options={})
|
38
|
-
source = DEFAULT_SOURCE
|
39
|
-
if options[:source]
|
40
|
-
source = options[:source].to_s
|
41
|
-
end
|
42
|
-
@lock.synchronize do
|
43
|
-
return nil unless @cache[key.to_s]
|
44
|
-
@cache[key.to_s][source]
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
# transfer all measurements to queue and reset internal status
|
49
|
-
def flush_to(queue)
|
50
|
-
counts = nil
|
51
|
-
@lock.synchronize do
|
52
|
-
# work off of a duplicate data set so we block for
|
53
|
-
# as little time as possible
|
54
|
-
counts = Marshal.load(Marshal.dump(@cache))
|
55
|
-
reset_cache
|
56
|
-
end
|
57
|
-
counts.each do |key, data|
|
58
|
-
data.each do |source, value|
|
59
|
-
if source == DEFAULT_SOURCE
|
60
|
-
queue.add key => value
|
61
|
-
else
|
62
|
-
queue.add key => {:value => value, :source => source}
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
# Increment a given metric
|
69
|
-
#
|
70
|
-
# @example Increment metric 'foo' by 1
|
71
|
-
# increment :foo
|
72
|
-
#
|
73
|
-
# @example Increment metric 'bar' by 2
|
74
|
-
# increment :bar, :by => 2
|
75
|
-
#
|
76
|
-
# @example Increment metric 'foo' by 1 with a custom source
|
77
|
-
# increment :foo, :source => user.id
|
78
|
-
#
|
79
|
-
def increment(counter, options={})
|
80
|
-
counter = counter.to_s
|
81
|
-
if options.is_a?(Fixnum)
|
82
|
-
# suppport legacy style
|
83
|
-
options = {:by => options}
|
84
|
-
end
|
85
|
-
by = options[:by] || 1
|
86
|
-
source = DEFAULT_SOURCE
|
87
|
-
if options[:source]
|
88
|
-
source = options[:source].to_s
|
89
|
-
end
|
90
|
-
if options[:sporadic]
|
91
|
-
make_sporadic(counter, source)
|
92
|
-
end
|
93
|
-
@lock.synchronize do
|
94
|
-
@cache[counter] ||= {}
|
95
|
-
@cache[counter][source] ||= 0
|
96
|
-
@cache[counter][source] += by
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
private
|
101
|
-
|
102
|
-
def make_sporadic(metric, source)
|
103
|
-
@sporadics[metric] ||= Set.new
|
104
|
-
@sporadics[metric] << source
|
105
|
-
end
|
106
|
-
|
107
|
-
def reset_cache
|
108
|
-
# remove any source/metric pairs that aren't continuous
|
109
|
-
@sporadics.each do |key, sources|
|
110
|
-
sources.each { |source| @cache[key].delete(source) }
|
111
|
-
end
|
112
|
-
@sporadics.clear
|
113
|
-
# reset all continuous source/metric pairs to 0
|
114
|
-
@cache.each_key do |key|
|
115
|
-
@cache[key].each_key { |source| @cache[key][source] = 0 }
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
end
|
120
|
-
|
121
|
-
end
|
122
|
-
end
|