librato-rails 0.9.0 → 0.10.0.pre1
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.
- 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
data/lib/librato/rails/group.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
module Librato
|
2
|
-
module Rails
|
3
|
-
class Group
|
4
|
-
|
5
|
-
def initialize(prefix)
|
6
|
-
@prefix = "#{prefix}."
|
7
|
-
end
|
8
|
-
|
9
|
-
def group(prefix)
|
10
|
-
prefix = "#{@prefix}#{prefix}"
|
11
|
-
yield self.class.new(prefix)
|
12
|
-
end
|
13
|
-
|
14
|
-
def increment(counter, by=1)
|
15
|
-
counter = "#{@prefix}#{counter}"
|
16
|
-
Librato::Rails.increment counter, by
|
17
|
-
end
|
18
|
-
|
19
|
-
def measure(event, duration)
|
20
|
-
event = "#{@prefix}#{event}"
|
21
|
-
Librato::Rails.measure event, duration
|
22
|
-
end
|
23
|
-
alias :timing :measure
|
24
|
-
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,77 +0,0 @@
|
|
1
|
-
module Librato::Rails
|
2
|
-
module Logging
|
3
|
-
LOG_LEVELS = [:off, :error, :warn, :info, :debug, :trace]
|
4
|
-
|
5
|
-
attr_writer :logger
|
6
|
-
|
7
|
-
# ex: log :debug, 'this is a debug message'
|
8
|
-
def log(level, message)
|
9
|
-
return unless should_log?(level)
|
10
|
-
case level
|
11
|
-
when :error, :warn
|
12
|
-
method = level
|
13
|
-
else
|
14
|
-
method = :info
|
15
|
-
end
|
16
|
-
message = '[librato-rails] ' << message
|
17
|
-
logger.send(method, message)
|
18
|
-
end
|
19
|
-
|
20
|
-
# set log level to any of LOG_LEVELS
|
21
|
-
def log_level=(level)
|
22
|
-
level = level.to_sym
|
23
|
-
if LOG_LEVELS.index(level)
|
24
|
-
@log_level = level
|
25
|
-
require 'pp' if should_log?(:debug)
|
26
|
-
else
|
27
|
-
raise "Invalid log level '#{level}'"
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
def log_level
|
32
|
-
@log_level ||= :info
|
33
|
-
end
|
34
|
-
|
35
|
-
def logger
|
36
|
-
@logger ||= if on_heroku
|
37
|
-
logger = Logger.new(STDOUT)
|
38
|
-
logger.level = Logger::INFO
|
39
|
-
logger
|
40
|
-
else
|
41
|
-
::Rails.logger
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
private
|
46
|
-
|
47
|
-
def should_log?(level)
|
48
|
-
LOG_LEVELS.index(self.log_level) >= LOG_LEVELS.index(level)
|
49
|
-
end
|
50
|
-
|
51
|
-
# trace current environment
|
52
|
-
def trace_environment
|
53
|
-
log :info, "Environment: " + ENV.pretty_inspect
|
54
|
-
end
|
55
|
-
|
56
|
-
# trace metrics being sent
|
57
|
-
def trace_queued(queued)
|
58
|
-
log :trace, "Queued: " + queued.pretty_inspect
|
59
|
-
end
|
60
|
-
|
61
|
-
def trace_settings
|
62
|
-
settings = {
|
63
|
-
:user => self.user,
|
64
|
-
:token => self.token,
|
65
|
-
:source => source,
|
66
|
-
:explicit_source => self.explicit_source ? 'true' : 'false',
|
67
|
-
:source_pids => self.source_pids ? 'true' : 'false',
|
68
|
-
:qualified_source => qualified_source,
|
69
|
-
:log_level => log_level,
|
70
|
-
:prefix => prefix,
|
71
|
-
:flush_interval => self.flush_interval
|
72
|
-
}
|
73
|
-
log :info, 'Settings: ' + settings.pretty_inspect
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
77
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
# this functionality should probably be available in librato-metrics
|
2
|
-
# eventually, spiking here for now to work out the kinks
|
3
|
-
module Librato
|
4
|
-
module Rails
|
5
|
-
class ValidatingQueue < Librato::Metrics::Queue
|
6
|
-
LOGGER = Librato::Rails
|
7
|
-
METRIC_NAME_REGEX = /\A[-.:_\w]{1,255}\z/
|
8
|
-
SOURCE_NAME_REGEX = /\A[-:A-Za-z0-9_.]{1,255}\z/
|
9
|
-
|
10
|
-
# screen all measurements for validity before sending
|
11
|
-
def submit
|
12
|
-
@queued[:gauges].delete_if do |entry|
|
13
|
-
name = entry[:name].to_s
|
14
|
-
source = entry[:source] && entry[:source].to_s
|
15
|
-
if name !~ METRIC_NAME_REGEX
|
16
|
-
LOGGER.log :warn, "invalid metric name '#{name}', not sending."
|
17
|
-
true # delete
|
18
|
-
elsif source && source !~ SOURCE_NAME_REGEX
|
19
|
-
LOGGER.log :warn, "invalid source name '#{source}', not sending."
|
20
|
-
true # delete
|
21
|
-
else
|
22
|
-
false # preserve
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
super
|
27
|
-
end
|
28
|
-
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
data/lib/librato/rails/worker.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
module Librato
|
2
|
-
module Rails
|
3
|
-
|
4
|
-
# This class manages the background thread which submits all data
|
5
|
-
# to the Librato Metrics service.
|
6
|
-
class Worker
|
7
|
-
|
8
|
-
def initialize
|
9
|
-
@interrupt = false
|
10
|
-
end
|
11
|
-
|
12
|
-
# do the assigned work, catching some special cases
|
13
|
-
#
|
14
|
-
def execute(obj)
|
15
|
-
obj.call
|
16
|
-
end
|
17
|
-
|
18
|
-
# run the given block every <period> seconds, looping
|
19
|
-
# infinitely unless @interrupt becomes true.
|
20
|
-
#
|
21
|
-
def run_periodically(period, &block)
|
22
|
-
next_run = start_time(period)
|
23
|
-
until @interrupt do
|
24
|
-
now = Time.now
|
25
|
-
if now >= next_run
|
26
|
-
execute(block) # runs given block
|
27
|
-
while next_run <= now
|
28
|
-
next_run += period
|
29
|
-
end
|
30
|
-
else
|
31
|
-
sleep(next_run - now)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
# Give some structure to worker start times so when possible
|
37
|
-
# they will be in sync.
|
38
|
-
def start_time(period)
|
39
|
-
earliest = Time.now + period
|
40
|
-
# already on a whole minute
|
41
|
-
return earliest if earliest.sec == 0
|
42
|
-
if period > 30
|
43
|
-
# bump to whole minute
|
44
|
-
earliest + (60-earliest.sec)
|
45
|
-
else
|
46
|
-
# ensure sync to whole minute if minute is evenly divisible
|
47
|
-
earliest + (period-(earliest.sec%period))
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
54
|
-
end
|
data/test/librato-rails_test.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class LibratoRailsTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
test 'is a module' do
|
6
|
-
assert_kind_of Module, Librato::Rails
|
7
|
-
end
|
8
|
-
|
9
|
-
test 'client is available' do
|
10
|
-
assert_kind_of Librato::Metrics::Client, Librato::Rails.client
|
11
|
-
end
|
12
|
-
|
13
|
-
test '#increment exists' do
|
14
|
-
assert Librato::Rails.respond_to?(:increment)
|
15
|
-
Librato::Rails.increment :baz, 5
|
16
|
-
end
|
17
|
-
|
18
|
-
test '#measure exists' do
|
19
|
-
assert Librato::Rails.respond_to?(:measure)
|
20
|
-
Librato::Rails.measure 'queries', 10
|
21
|
-
end
|
22
|
-
|
23
|
-
test '#timing exists' do
|
24
|
-
assert Librato::Rails.respond_to?(:timing)
|
25
|
-
Librato::Rails.timing 'request.time.total', 121.2
|
26
|
-
end
|
27
|
-
|
28
|
-
test 'source is assignable' do
|
29
|
-
original = Librato::Rails.source
|
30
|
-
Librato::Rails.source = 'foobar'
|
31
|
-
assert_equal 'foobar', Librato::Rails.source
|
32
|
-
Librato::Rails.source = original
|
33
|
-
end
|
34
|
-
|
35
|
-
test 'qualified source includes pid' do
|
36
|
-
assert_match /\.\d{2,6}$/, Librato::Rails.qualified_source
|
37
|
-
end
|
38
|
-
|
39
|
-
test 'qualified source does not include pid when disabled' do
|
40
|
-
Librato::Rails.source_pids = false
|
41
|
-
assert_match Librato::Rails.source, Librato::Rails.qualified_source
|
42
|
-
Librato::Rails.source_pids = true
|
43
|
-
end
|
44
|
-
end
|
@@ -1,193 +0,0 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
require 'test_helper'
|
3
|
-
|
4
|
-
class LibratoRailsRemoteTest < ActiveSupport::TestCase
|
5
|
-
|
6
|
-
# These tests connect to the Metrics server with an account and verify remote
|
7
|
-
# functions. They will only run if the below environment variables are set.
|
8
|
-
#
|
9
|
-
# BE CAREFUL, running these tests will DELETE ALL metrics currently in the
|
10
|
-
# test account.
|
11
|
-
#
|
12
|
-
if ENV['LIBRATO_RAILS_TEST_EMAIL'] && ENV['LIBRATO_RAILS_TEST_API_KEY']
|
13
|
-
|
14
|
-
setup do
|
15
|
-
# delete any generated Librato::Rails
|
16
|
-
Librato::Rails.user = ENV['LIBRATO_RAILS_TEST_EMAIL']
|
17
|
-
Librato::Rails.token = ENV['LIBRATO_RAILS_TEST_API_KEY']
|
18
|
-
if ENV['LIBRATO_RAILS_TEST_API_ENDPOINT']
|
19
|
-
Librato::Rails.api_endpoint = ENV['LIBRATO_RAILS_TEST_API_ENDPOINT']
|
20
|
-
end
|
21
|
-
Librato::Rails.delete_all
|
22
|
-
delete_all_metrics
|
23
|
-
end
|
24
|
-
|
25
|
-
teardown do
|
26
|
-
Librato::Rails.prefix = nil
|
27
|
-
end
|
28
|
-
|
29
|
-
test 'flush sends counters' do
|
30
|
-
source = Librato::Rails.qualified_source
|
31
|
-
|
32
|
-
Librato::Rails.increment :foo
|
33
|
-
Librato::Rails.increment :bar, 2
|
34
|
-
Librato::Rails.increment :foo
|
35
|
-
Librato::Rails.increment :foo, :source => 'baz', :by => 3
|
36
|
-
Librato::Rails.flush
|
37
|
-
|
38
|
-
client = Librato::Rails.client
|
39
|
-
metric_names = client.list.map { |m| m['name'] }
|
40
|
-
assert metric_names.include?('foo'), 'foo should be present'
|
41
|
-
assert metric_names.include?('bar'), 'bar should be present'
|
42
|
-
|
43
|
-
foo = client.fetch 'foo', :count => 10
|
44
|
-
assert_equal 1, foo[source].length
|
45
|
-
assert_equal 2, foo[source][0]['value']
|
46
|
-
|
47
|
-
# custom source
|
48
|
-
assert_equal 1, foo['baz'].length
|
49
|
-
assert_equal 3, foo['baz'][0]['value']
|
50
|
-
|
51
|
-
bar = client.fetch 'bar', :count => 10
|
52
|
-
assert_equal 1, bar[source].length
|
53
|
-
assert_equal 2, bar[source][0]['value']
|
54
|
-
end
|
55
|
-
|
56
|
-
test 'counters should persist through flush' do
|
57
|
-
Librato::Rails.increment 'knightrider'
|
58
|
-
Librato::Rails.increment 'badguys', :sporadic => true
|
59
|
-
assert_equal 1, Librato::Rails.counters['knightrider']
|
60
|
-
assert_equal 1, Librato::Rails.counters['badguys']
|
61
|
-
|
62
|
-
Librato::Rails.flush
|
63
|
-
assert_equal 0, Librato::Rails.counters['knightrider']
|
64
|
-
assert_equal nil, Librato::Rails.counters['badguys']
|
65
|
-
end
|
66
|
-
|
67
|
-
test 'flush sends measures/timings' do
|
68
|
-
source = Librato::Rails.qualified_source
|
69
|
-
|
70
|
-
Librato::Rails.timing 'request.time.total', 122.1
|
71
|
-
Librato::Rails.measure 'items_bought', 20
|
72
|
-
Librato::Rails.timing 'request.time.total', 81.3
|
73
|
-
Librato::Rails.timing 'jobs.queued', 5, :source => 'worker.3'
|
74
|
-
Librato::Rails.flush
|
75
|
-
|
76
|
-
client = Librato::Rails.client
|
77
|
-
metric_names = client.list.map { |m| m['name'] }
|
78
|
-
assert metric_names.include?('request.time.total'), 'request.time.total should be present'
|
79
|
-
assert metric_names.include?('items_bought'), 'request.time.db should be present'
|
80
|
-
|
81
|
-
total = client.fetch 'request.time.total', :count => 10
|
82
|
-
assert_equal 2, total[source][0]['count']
|
83
|
-
assert_in_delta 203.4, total[source][0]['sum'], 0.1
|
84
|
-
|
85
|
-
items = client.fetch 'items_bought', :count => 10
|
86
|
-
assert_equal 1, items[source][0]['count']
|
87
|
-
assert_in_delta 20, items[source][0]['sum'], 0.1
|
88
|
-
|
89
|
-
jobs = client.fetch 'jobs.queued', :count => 10
|
90
|
-
assert_equal 1, jobs['worker.3'][0]['count']
|
91
|
-
assert_in_delta 5, jobs['worker.3'][0]['sum'], 0.1
|
92
|
-
end
|
93
|
-
|
94
|
-
test 'flush should purge measures/timings' do
|
95
|
-
Librato::Rails.timing 'request.time.total', 122.1
|
96
|
-
Librato::Rails.measure 'items_bought', 20
|
97
|
-
Librato::Rails.flush
|
98
|
-
|
99
|
-
assert Librato::Rails.aggregate.empty?, 'measures and timings should be cleared with flush'
|
100
|
-
end
|
101
|
-
|
102
|
-
test 'empty flush should not be sent' do
|
103
|
-
Librato::Rails.flush
|
104
|
-
assert_equal [], Librato::Rails.client.list
|
105
|
-
end
|
106
|
-
|
107
|
-
test 'flush respects prefix' do
|
108
|
-
source = Librato::Rails.qualified_source
|
109
|
-
Librato::Rails.prefix = 'testyprefix'
|
110
|
-
|
111
|
-
Librato::Rails.timing 'mytime', 221.1
|
112
|
-
Librato::Rails.increment 'mycount', 4
|
113
|
-
Librato::Rails.flush
|
114
|
-
|
115
|
-
client = Librato::Rails.client
|
116
|
-
metric_names = client.list.map { |m| m['name'] }
|
117
|
-
assert metric_names.include?('testyprefix.mytime'), 'testyprefix.mytime should be present'
|
118
|
-
assert metric_names.include?('testyprefix.mycount'), 'testyprefix.mycount should be present'
|
119
|
-
|
120
|
-
mytime = client.fetch 'testyprefix.mytime', :count => 10
|
121
|
-
assert_equal 1, mytime[source][0]['count']
|
122
|
-
|
123
|
-
mycount = client.fetch 'testyprefix.mycount', :count => 10
|
124
|
-
assert_equal 4, mycount[source][0]['value']
|
125
|
-
end
|
126
|
-
|
127
|
-
test 'flush recovers from failed flush' do
|
128
|
-
client = Librato::Rails.client
|
129
|
-
source = Librato::Rails.qualified_source
|
130
|
-
|
131
|
-
# create a metric foo of counter type
|
132
|
-
client.submit :foo => {:type => :counter, :value => 12}
|
133
|
-
|
134
|
-
# failing flush - submit a foo measurement as a gauge (type mismatch)
|
135
|
-
Librato::Rails.measure :foo, 2.12
|
136
|
-
Librato::Rails.flush
|
137
|
-
|
138
|
-
foo = client.fetch :foo, :count => 10
|
139
|
-
assert_equal 1, foo['unassigned'].length
|
140
|
-
assert_nil foo[source] # shouldn't have been accepted
|
141
|
-
|
142
|
-
Librato::Rails.measure :boo, 2.12
|
143
|
-
Librato::Rails.flush
|
144
|
-
|
145
|
-
boo = client.fetch :boo, :count => 10
|
146
|
-
assert_equal 2.12, boo[source][0]["value"]
|
147
|
-
end
|
148
|
-
|
149
|
-
test 'flush tolerates invalid metric names' do
|
150
|
-
client = Librato::Rails.client
|
151
|
-
source = Librato::Rails.qualified_source
|
152
|
-
|
153
|
-
Librato::Rails.increment :foo
|
154
|
-
Librato::Rails.increment 'fübar'
|
155
|
-
Librato::Rails.measure 'fu/bar/baz', 12.1
|
156
|
-
Librato::Rails.flush
|
157
|
-
|
158
|
-
metric_names = client.list.map { |m| m['name'] }
|
159
|
-
assert metric_names.include?('foo')
|
160
|
-
|
161
|
-
# should have saved values for foo even though
|
162
|
-
# other metrics had invalid names
|
163
|
-
foo = client.fetch :foo, :count => 5
|
164
|
-
assert_equal 1.0, foo[source][0]["value"]
|
165
|
-
end
|
166
|
-
|
167
|
-
test 'flush tolerates invalid source names' do
|
168
|
-
client = Librato::Rails.client
|
169
|
-
|
170
|
-
Librato::Rails.increment :foo, :source => 'atreides'
|
171
|
-
Librato::Rails.increment :bar, :source => 'glébnöst'
|
172
|
-
Librato::Rails.measure 'baz', 2.25, :source => 'b/l/ak/nok'
|
173
|
-
Librato::Rails.flush
|
174
|
-
|
175
|
-
# should have saved values for foo even though
|
176
|
-
# other metrics had invalid sources
|
177
|
-
foo = client.fetch :foo, :count => 5
|
178
|
-
assert_equal 1.0, foo['atreides'][0]["value"]
|
179
|
-
end
|
180
|
-
|
181
|
-
private
|
182
|
-
|
183
|
-
def delete_all_metrics
|
184
|
-
client = Librato::Rails.client
|
185
|
-
metric_names = client.list.map { |metric| metric['name'] }
|
186
|
-
client.delete(*metric_names) if !metric_names.empty?
|
187
|
-
end
|
188
|
-
|
189
|
-
else
|
190
|
-
puts "Skipping remote tests..."
|
191
|
-
end
|
192
|
-
|
193
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class LibratoRailsAggregatorTest < MiniTest::Unit::TestCase
|
4
|
-
|
5
|
-
def setup
|
6
|
-
@agg = Librato::Rails::Aggregator.new
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_adding_timings
|
10
|
-
@agg.timing 'request.time.total', 23.7
|
11
|
-
@agg.timing 'request.time.db', 5.3
|
12
|
-
@agg.timing 'request.time.total', 64.3
|
13
|
-
|
14
|
-
assert_equal 2, @agg['request.time.total'][:count]
|
15
|
-
assert_equal 88.0, @agg['request.time.total'][:sum]
|
16
|
-
end
|
17
|
-
|
18
|
-
def test_block_timing
|
19
|
-
@agg.timing 'my.task' do
|
20
|
-
sleep 0.2
|
21
|
-
end
|
22
|
-
assert_in_delta @agg['my.task'][:sum], 200, 50
|
23
|
-
|
24
|
-
@agg.timing('another.task') { sleep 0.1 }
|
25
|
-
assert_in_delta @agg['another.task'][:sum], 100, 50
|
26
|
-
end
|
27
|
-
|
28
|
-
def test_return_values
|
29
|
-
simple = @agg.timing 'simple', 20
|
30
|
-
assert_equal nil, simple
|
31
|
-
|
32
|
-
timing = @agg.timing 'foo' do
|
33
|
-
sleep 0.1
|
34
|
-
'bar'
|
35
|
-
end
|
36
|
-
assert_equal 'bar', timing
|
37
|
-
end
|
38
|
-
|
39
|
-
def test_custom_source
|
40
|
-
# sources are kept separate
|
41
|
-
@agg.measure 'meaning.of.life', 1
|
42
|
-
@agg.measure 'meaning.of.life', 42, :source => 'douglas_adams'
|
43
|
-
assert_equal 1.0, @agg.fetch('meaning.of.life')[:sum]
|
44
|
-
assert_equal 42.0, @agg.fetch('meaning.of.life', :source => 'douglas_adams')[:sum]
|
45
|
-
|
46
|
-
# sources work with time blocks
|
47
|
-
@agg.timing 'mytiming', :source => 'mine' do
|
48
|
-
sleep 0.02
|
49
|
-
end
|
50
|
-
assert_in_delta @agg.fetch('mytiming', :source => 'mine')[:sum], 20, 10
|
51
|
-
end
|
52
|
-
|
53
|
-
end
|
@@ -1,90 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class LibratoRailsCounterCacheTest < MiniTest::Unit::TestCase
|
4
|
-
|
5
|
-
def test_basic_operations
|
6
|
-
cc = Librato::Rails::CounterCache.new
|
7
|
-
cc.increment :foo
|
8
|
-
assert_equal 1, cc[:foo]
|
9
|
-
|
10
|
-
# accepts optional argument
|
11
|
-
cc.increment :foo, :by => 5
|
12
|
-
assert_equal 6, cc[:foo]
|
13
|
-
|
14
|
-
# legacy style
|
15
|
-
cc.increment :foo, 2
|
16
|
-
assert_equal 8, cc[:foo]
|
17
|
-
|
18
|
-
# strings or symbols work
|
19
|
-
cc.increment 'foo'
|
20
|
-
assert_equal 9, cc['foo']
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_custom_sources
|
24
|
-
cc = Librato::Rails::CounterCache.new
|
25
|
-
|
26
|
-
cc.increment :foo, :source => 'bar'
|
27
|
-
assert_equal 1, cc.fetch(:foo, :source => 'bar')
|
28
|
-
|
29
|
-
# symbols also work
|
30
|
-
cc.increment :foo, :source => :baz
|
31
|
-
assert_equal 1, cc.fetch(:foo, :source => :baz)
|
32
|
-
|
33
|
-
# strings and symbols are interchangable
|
34
|
-
cc.increment :foo, :source => :bar
|
35
|
-
assert_equal 2, cc.fetch(:foo, :source => 'bar')
|
36
|
-
|
37
|
-
# custom source and custom increment
|
38
|
-
cc.increment :foo, :source => 'boombah', :by => 10
|
39
|
-
assert_equal 10, cc.fetch(:foo, :source => 'boombah')
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_continuous
|
43
|
-
cc = Librato::Rails::CounterCache.new
|
44
|
-
|
45
|
-
cc.increment :foo
|
46
|
-
cc.increment :foo, :source => 'bar'
|
47
|
-
|
48
|
-
cc.increment :baz, :sporadic => true
|
49
|
-
cc.increment :baz, :source => 118, :sporadic => true
|
50
|
-
assert_equal 1, cc[:baz]
|
51
|
-
assert_equal 1, cc.fetch(:baz, :source => 118)
|
52
|
-
|
53
|
-
cc.flush_to(Librato::Metrics::Queue.new) # persist values once
|
54
|
-
|
55
|
-
# continous values persist
|
56
|
-
assert_equal 0, cc[:foo]
|
57
|
-
assert_equal 0, cc.fetch(:foo, :source => 'bar')
|
58
|
-
|
59
|
-
# non-continous do not
|
60
|
-
assert_equal nil, cc[:baz]
|
61
|
-
assert_equal nil, cc.fetch(:baz, :source => 118)
|
62
|
-
|
63
|
-
# add a different sporadic metric
|
64
|
-
cc.increment :bazoom, :sporadic => true
|
65
|
-
assert_equal 1, cc[:bazoom]
|
66
|
-
|
67
|
-
cc.flush_to(Librato::Metrics::Queue.new) # persist values again
|
68
|
-
assert_equal nil, cc[:bazoom]
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_flushing
|
72
|
-
cc = Librato::Rails::CounterCache.new
|
73
|
-
|
74
|
-
cc.increment :foo
|
75
|
-
cc.increment :bar, :by => 2
|
76
|
-
cc.increment :foo, :source => 'foobar'
|
77
|
-
cc.increment :foo, :source => 'foobar', :by => 3
|
78
|
-
|
79
|
-
q = Librato::Metrics::Queue.new
|
80
|
-
cc.flush_to(q)
|
81
|
-
|
82
|
-
expected = Set.new [{:name=>"foo", :value=>1},
|
83
|
-
{:name=>"foo", :value=>4, :source=>"foobar"},
|
84
|
-
{:name=>"bar", :value=>2}]
|
85
|
-
queued = Set.new q.gauges
|
86
|
-
queued.each { |hash| hash.delete(:measure_time) }
|
87
|
-
assert_equal queued, expected
|
88
|
-
end
|
89
|
-
|
90
|
-
end
|
data/test/unit/group_test.rb
DELETED
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class LibratoRailsGroupTest < MiniTest::Unit::TestCase
|
4
|
-
|
5
|
-
def test_basic_grouping
|
6
|
-
Librato::Rails.group 'fruit' do |g|
|
7
|
-
g.increment 'bites'
|
8
|
-
g.increment 'nibbles', :by => 2
|
9
|
-
g.increment 'nibbles', 5
|
10
|
-
|
11
|
-
g.increment 'chomps', :source => 'pacman'
|
12
|
-
g.increment 'chomps', :source => 'pacman', :by => 5
|
13
|
-
|
14
|
-
g.measure 'banana', 12
|
15
|
-
g.measure 'banana', 10
|
16
|
-
|
17
|
-
g.timing 'grow_time', 122.2
|
18
|
-
g.timing 'grow_time', 24.3
|
19
|
-
end
|
20
|
-
|
21
|
-
assert_equal 1, counters['fruit.bites']
|
22
|
-
assert_equal 7, counters['fruit.nibbles']
|
23
|
-
assert_equal 6, counters.fetch('fruit.chomps', :source => 'pacman')
|
24
|
-
|
25
|
-
assert_equal 2, aggregate['fruit.banana'][:count]
|
26
|
-
assert_equal 22, aggregate['fruit.banana'][:sum]
|
27
|
-
|
28
|
-
assert_equal 2, aggregate['fruit.grow_time'][:count]
|
29
|
-
assert_equal 146.5, aggregate['fruit.grow_time'][:sum]
|
30
|
-
end
|
31
|
-
|
32
|
-
def test_nesting
|
33
|
-
Librato::Rails.group 'street' do |s|
|
34
|
-
s.increment 'count'
|
35
|
-
s.group 'market' do |m|
|
36
|
-
m.increment 'tenants', 10
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
assert_equal 1, counters['street.count']
|
41
|
-
assert_equal 10, counters['street.market.tenants']
|
42
|
-
end
|
43
|
-
|
44
|
-
private
|
45
|
-
|
46
|
-
def aggregate
|
47
|
-
Librato::Rails.aggregate
|
48
|
-
end
|
49
|
-
|
50
|
-
def counters
|
51
|
-
Librato::Rails.counters
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|