canvas_statsd 1.0.5 → 1.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/canvas_statsd/default_tracking.rb +18 -5
- data/lib/canvas_statsd/request_logger.rb +2 -1
- data/lib/canvas_statsd/request_stat.rb +2 -0
- data/lib/canvas_statsd.rb +16 -1
- data/spec/canvas_statsd/canvas_statsd_spec.rb +17 -5
- data/spec/canvas_statsd/default_tracking_spec.rb +6 -2
- data/spec/canvas_statsd/request_logger_spec.rb +8 -8
- data/spec/canvas_statsd/request_stat_spec.rb +12 -0
- data/spec/canvas_statsd/statsd_spec.rb +11 -10
- data/spec/spec_helper.rb +2 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 88c09ebe7c39746f6df19a58da9a5edf13097cd7
|
4
|
+
data.tar.gz: eb620ecca522585ccef830258632c8a42dd40040
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bd1688d68e7891ff38f7dbc4594c30715cdc823a03d98ccc3ad94134c6f2dc68e896548c15b0d1555dfade7a572eb54b110410415a4af338f90e6aa4867902aa
|
7
|
+
data.tar.gz: c29c8778a1299b4be63a2b95f5cad2ee5a69c3c8562c74175570d805698620f499ffb941f32bedc283af6eb244b04eb4a3010328252cee43f5b7361551271a60
|
@@ -2,14 +2,16 @@ module CanvasStatsd
|
|
2
2
|
class DefaultTracking
|
3
3
|
|
4
4
|
@ar_counter = CanvasStatsd::Counter.new('ar_counter')
|
5
|
+
@cache_read_counter = CanvasStatsd::Counter.new('cache_read_counter')
|
5
6
|
@sql_tracker = CanvasStatsd::SqlTracker.new(blocked_names: ['SCHEMA'])
|
6
7
|
|
7
8
|
def self.track_default_metrics(options={})
|
8
|
-
options = {sql: true, active_record: true, logger: nil}.merge(options)
|
9
|
+
options = {sql: true, active_record: true, cache: true, logger: nil}.merge(options)
|
9
10
|
@logger = RequestLogger.new(options[:logger])
|
10
11
|
track_timing
|
11
12
|
track_sql if !!options[:sql]
|
12
13
|
track_active_record if !!options[:active_record]
|
14
|
+
track_cache if !!options[:cache]
|
13
15
|
end
|
14
16
|
|
15
17
|
def self.subscribe type, &block
|
@@ -23,24 +25,30 @@ module CanvasStatsd
|
|
23
25
|
end
|
24
26
|
|
25
27
|
def self.track_timing
|
26
|
-
subscribe(/start_processing
|
27
|
-
subscribe(/process_action
|
28
|
+
subscribe(/start_processing\.action_controller/) {|*args| start_processing(*args)}
|
29
|
+
subscribe(/process_action\.action_controller/) {|*args| finalize_processing(*args)}
|
28
30
|
end
|
29
31
|
|
30
32
|
def self.track_sql
|
31
33
|
@tracking_sql = true
|
32
|
-
subscribe(/sql
|
34
|
+
subscribe(/sql\.active_record/) {|*args| update_sql_count(*args)}
|
33
35
|
end
|
34
36
|
|
35
37
|
def self.track_active_record
|
36
38
|
instrument_active_record_creation
|
37
39
|
@tracking_active_record = true
|
38
|
-
subscribe(/instance
|
40
|
+
subscribe(/instance\.active_record/) {|*args| update_active_record_count(*args)}
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.track_cache
|
44
|
+
@tracking_cache = true
|
45
|
+
subscribe(/cache_read\.active_support/) {|*args| update_cache_read_count(*args)}
|
39
46
|
end
|
40
47
|
|
41
48
|
def self.start_processing *args
|
42
49
|
@sql_tracker.start
|
43
50
|
@ar_counter.start
|
51
|
+
@cache_read_counter.start
|
44
52
|
end
|
45
53
|
|
46
54
|
def self.update_sql_count name, start, finish, id, payload
|
@@ -51,12 +59,17 @@ module CanvasStatsd
|
|
51
59
|
@ar_counter.track payload.fetch(:name, '')
|
52
60
|
end
|
53
61
|
|
62
|
+
def self.update_cache_read_count name, start, finish, id, payload
|
63
|
+
@cache_read_counter.track "read"
|
64
|
+
end
|
65
|
+
|
54
66
|
def self.finalize_processing *args
|
55
67
|
request_stat = CanvasStatsd::RequestStat.new(*args)
|
56
68
|
request_stat.ar_count = @ar_counter.finalize_count if @tracking_active_record
|
57
69
|
request_stat.sql_read_count = @sql_tracker.num_reads if @tracking_sql
|
58
70
|
request_stat.sql_write_count = @sql_tracker.num_writes if @tracking_sql
|
59
71
|
request_stat.sql_cache_count = @sql_tracker.num_caches if @tracking_sql
|
72
|
+
request_stat.cache_read_count = @cache_read_counter.finalize_count if @tracking_cache
|
60
73
|
request_stat.report
|
61
74
|
@logger.log(request_stat)
|
62
75
|
end
|
@@ -5,6 +5,7 @@ module CanvasStatsd
|
|
5
5
|
attr_accessor :sql_write_count
|
6
6
|
attr_accessor :sql_cache_count
|
7
7
|
attr_accessor :ar_count
|
8
|
+
attr_accessor :cache_read_count
|
8
9
|
|
9
10
|
def initialize(name, start, finish, id, payload, statsd=CanvasStatsd::Statsd)
|
10
11
|
@name = name
|
@@ -25,6 +26,7 @@ module CanvasStatsd
|
|
25
26
|
@statsd.timing("#{common_key}.sql.write", sql_write_count) if sql_write_count
|
26
27
|
@statsd.timing("#{common_key}.sql.cache", sql_cache_count) if sql_cache_count
|
27
28
|
@statsd.timing("#{common_key}.active_record", ar_count) if ar_count
|
29
|
+
@statsd.timing("#{common_key}.cache.read", cache_read_count) if cache_read_count
|
28
30
|
end
|
29
31
|
end
|
30
32
|
|
data/lib/canvas_statsd.rb
CHANGED
@@ -2,6 +2,7 @@ require 'statsd'
|
|
2
2
|
require "aroi" if defined?(ActiveRecord)
|
3
3
|
|
4
4
|
module CanvasStatsd
|
5
|
+
VALID_SETTINGS = [:host, :port, :namespace, :append_hostname]
|
5
6
|
|
6
7
|
class ConfigurationError < StandardError; end
|
7
8
|
|
@@ -18,7 +19,21 @@ module CanvasStatsd
|
|
18
19
|
end
|
19
20
|
|
20
21
|
def self.settings=(value)
|
21
|
-
@settings = value
|
22
|
+
@settings = validate_settings(value)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.validate_settings(value)
|
26
|
+
return nil if value.nil?
|
27
|
+
|
28
|
+
validated = {}
|
29
|
+
value.each do |k,v|
|
30
|
+
if !VALID_SETTINGS.include?(k.to_sym)
|
31
|
+
raise CanvasStatsd::ConfigurationError, "Invalid key: #{k}"
|
32
|
+
end
|
33
|
+
validated[k.to_sym] = v
|
34
|
+
end
|
35
|
+
|
36
|
+
env_settings.merge(validated)
|
22
37
|
end
|
23
38
|
|
24
39
|
def self.env_settings(env=ENV)
|
@@ -38,7 +38,7 @@ describe CanvasStatsd do
|
|
38
38
|
end
|
39
39
|
|
40
40
|
it "can be assigned a new value" do
|
41
|
-
settings = {
|
41
|
+
settings = { host: 'bar', port: 1234 }
|
42
42
|
CanvasStatsd.settings = settings
|
43
43
|
|
44
44
|
expect(CanvasStatsd.settings).to eq settings
|
@@ -53,16 +53,28 @@ describe CanvasStatsd do
|
|
53
53
|
namespace: 'canvas',
|
54
54
|
}
|
55
55
|
expect(CanvasStatsd.settings).to eq(expected)
|
56
|
-
|
57
56
|
end
|
58
57
|
|
59
|
-
it 'configured settings take precedence over ENV settings' do
|
58
|
+
it 'configured settings are merged into and take precedence over any existing ENV settings' do
|
60
59
|
ENV['CANVAS_STATSD_HOST'] = 'statsd.example.org'
|
61
60
|
ENV['CANVAS_STATSD_NAMESPACE'] = 'canvas'
|
62
61
|
|
63
|
-
settings = {
|
62
|
+
settings = { host: 'statsd.example-override.org' }
|
64
63
|
CanvasStatsd.settings = settings
|
65
|
-
|
64
|
+
|
65
|
+
expect(CanvasStatsd.settings).to eq(CanvasStatsd.env_settings.merge(settings))
|
66
|
+
expect(CanvasStatsd.settings[:host]).to eq(settings[:host])
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'validates settings' do
|
70
|
+
settings = { foo: 'blah' }
|
71
|
+
expect { CanvasStatsd.settings = settings }.to raise_error(CanvasStatsd::ConfigurationError)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'converts string keys to symbols' do
|
75
|
+
settings = { 'host' => 'bar', 'port' => 1234 }
|
76
|
+
CanvasStatsd.settings = settings
|
77
|
+
expect(CanvasStatsd.settings).to eq({ host: 'bar', port: 1234 })
|
66
78
|
end
|
67
79
|
end
|
68
80
|
|
@@ -21,6 +21,11 @@ describe CanvasStatsd::DefaultTracking do
|
|
21
21
|
CanvasStatsd::DefaultTracking.track_default_metrics active_record: false
|
22
22
|
end
|
23
23
|
|
24
|
+
it 'should not track cache when cache: false option' do
|
25
|
+
expect(CanvasStatsd::DefaultTracking).not_to receive(:track_cache)
|
26
|
+
CanvasStatsd::DefaultTracking.track_default_metrics cache: false
|
27
|
+
end
|
28
|
+
|
24
29
|
it 'should delegate log messages to the optional logger' do
|
25
30
|
log_double = double()
|
26
31
|
expect(log_double).to receive(:info)
|
@@ -39,11 +44,10 @@ describe CanvasStatsd::DefaultTracking do
|
|
39
44
|
describe '#subscribe' do
|
40
45
|
it 'should subscribe via ActiveSupport::Notifications' do
|
41
46
|
target = double()
|
42
|
-
CanvasStatsd::DefaultTracking.subscribe(/test
|
47
|
+
CanvasStatsd::DefaultTracking.subscribe(/test\.notification/) {|*args| target.callback(*args)}
|
43
48
|
expect(target).to receive(:callback)
|
44
49
|
ActiveSupport::Notifications.instrument('test.notification') {}
|
45
50
|
end
|
46
51
|
end
|
47
52
|
|
48
53
|
end
|
49
|
-
|
@@ -19,15 +19,15 @@ describe CanvasStatsd::RequestLogger do
|
|
19
19
|
end
|
20
20
|
it 'includes stats that are available' do
|
21
21
|
request_stat = double('request_stat')
|
22
|
-
request_stat.
|
23
|
-
request_stat.
|
22
|
+
allow(request_stat).to receive(:ms).and_return(100.21)
|
23
|
+
allow(request_stat).to receive(:ar_count).and_return(24)
|
24
24
|
results = @logger.build_log_message(request_stat)
|
25
25
|
expect(results).to eq("[STATSD] (total: 100.21) (active_record: 24.00)")
|
26
26
|
end
|
27
27
|
it 'doesnt include nil stats' do
|
28
28
|
request_stat = double('request_stat')
|
29
|
-
request_stat.
|
30
|
-
request_stat.
|
29
|
+
allow(request_stat).to receive(:ms).and_return(100.22)
|
30
|
+
allow(request_stat).to receive(:ar_count).and_return(nil)
|
31
31
|
results = @logger.build_log_message(request_stat)
|
32
32
|
expect(results).to eq("[STATSD] (total: 100.22)")
|
33
33
|
end
|
@@ -35,16 +35,16 @@ describe CanvasStatsd::RequestLogger do
|
|
35
35
|
describe 'decimal precision' do
|
36
36
|
it 'forces 2 decimal precision' do
|
37
37
|
request_stat = double('request_stat')
|
38
|
-
request_stat.
|
38
|
+
allow(request_stat).to receive(:ms).and_return(72.1)
|
39
39
|
results = @logger.build_log_message(request_stat)
|
40
40
|
expect(results).to eq("[STATSD] (total: 72.10)")
|
41
41
|
end
|
42
42
|
it 'rounds values to 2 decimals' do
|
43
43
|
request_stat = double('request_stat')
|
44
|
-
request_stat.
|
44
|
+
allow(request_stat).to receive(:ms).and_return(72.1382928)
|
45
45
|
results = @logger.build_log_message(request_stat)
|
46
46
|
expect(results).to eq("[STATSD] (total: 72.14)")
|
47
|
-
request_stat.
|
47
|
+
allow(request_stat).to receive(:ms).and_return(72.1348209)
|
48
48
|
results = @logger.build_log_message(request_stat)
|
49
49
|
expect(results).to eq("[STATSD] (total: 72.13)")
|
50
50
|
end
|
@@ -65,7 +65,7 @@ describe CanvasStatsd::RequestLogger do
|
|
65
65
|
logger = CanvasStatsd::RequestLogger.new(std_out_logger)
|
66
66
|
expect(std_out_logger).to receive(:info).with("[DEFAULT_METRICS] (total: 100.20)")
|
67
67
|
request_stat = double('request_stat')
|
68
|
-
request_stat.
|
68
|
+
allow(request_stat).to receive(:ms).and_return(100.2)
|
69
69
|
logger.log(request_stat, "DEFAULT_METRICS")
|
70
70
|
end
|
71
71
|
end
|
@@ -121,6 +121,16 @@ describe CanvasStatsd::RequestStat do
|
|
121
121
|
rs.report
|
122
122
|
end
|
123
123
|
|
124
|
+
it 'sends cache_read_count when present' do
|
125
|
+
statsd = double
|
126
|
+
payload = {
|
127
|
+
params: {
|
128
|
+
'controller' => 'foo',
|
129
|
+
'action' => 'index'
|
130
|
+
}
|
131
|
+
}
|
132
|
+
end
|
133
|
+
|
124
134
|
describe 'sql stats' do
|
125
135
|
|
126
136
|
before :each do
|
@@ -132,6 +142,8 @@ describe CanvasStatsd::RequestStat do
|
|
132
142
|
}
|
133
143
|
}
|
134
144
|
@rs = create_subject(payload, @statsd)
|
145
|
+
@rs.cache_read_count = 25
|
146
|
+
expect(@statsd).to receive(:timing).with('request.foo.index.cache.read', 25)
|
135
147
|
end
|
136
148
|
|
137
149
|
it 'doesnt send sql stats when they dont exist' do
|
@@ -21,11 +21,16 @@ require 'spec_helper'
|
|
21
21
|
describe CanvasStatsd::Statsd do
|
22
22
|
METHODS = %w(increment decrement count gauge timing)
|
23
23
|
|
24
|
+
after do
|
25
|
+
CanvasStatsd.settings = {}
|
26
|
+
CanvasStatsd::Statsd.reset_instance
|
27
|
+
end
|
28
|
+
|
24
29
|
it "appends the hostname to stat names by default" do
|
25
|
-
CanvasStatsd::Statsd.
|
30
|
+
allow(CanvasStatsd::Statsd).to receive(:hostname).and_return("testhost")
|
26
31
|
statsd = double
|
27
|
-
CanvasStatsd::Statsd.
|
28
|
-
CanvasStatsd::Statsd.
|
32
|
+
allow(CanvasStatsd::Statsd).to receive(:instance).and_return(statsd)
|
33
|
+
allow(CanvasStatsd::Statsd).to receive(:append_hostname?).and_return(true)
|
29
34
|
METHODS.each do |method|
|
30
35
|
expect(statsd).to receive(method).with("test.name.testhost", "test")
|
31
36
|
CanvasStatsd::Statsd.send(method, "test.name", "test")
|
@@ -37,8 +42,8 @@ describe CanvasStatsd::Statsd do
|
|
37
42
|
it "omits hostname if specified in config" do
|
38
43
|
expect(CanvasStatsd::Statsd).to receive(:hostname).never
|
39
44
|
statsd = double
|
40
|
-
CanvasStatsd::Statsd.
|
41
|
-
CanvasStatsd::Statsd.
|
45
|
+
allow(CanvasStatsd::Statsd).to receive(:instance).and_return(statsd)
|
46
|
+
allow(CanvasStatsd::Statsd).to receive(:append_hostname?).and_return(false)
|
42
47
|
METHODS.each do |method|
|
43
48
|
expect(statsd).to receive(method).with("test.name", "test")
|
44
49
|
CanvasStatsd::Statsd.send(method, "test.name", "test")
|
@@ -48,7 +53,7 @@ describe CanvasStatsd::Statsd do
|
|
48
53
|
end
|
49
54
|
|
50
55
|
it "ignores all calls if statsd isn't enabled" do
|
51
|
-
CanvasStatsd::Statsd.
|
56
|
+
allow(CanvasStatsd::Statsd).to receive(:instance).and_return(nil)
|
52
57
|
METHODS.each do |method|
|
53
58
|
expect(CanvasStatsd::Statsd.send(method, "test.name")).to be_nil
|
54
59
|
end
|
@@ -87,8 +92,4 @@ describe CanvasStatsd::Statsd do
|
|
87
92
|
expect(result).to eq hash
|
88
93
|
end
|
89
94
|
end
|
90
|
-
|
91
|
-
after do
|
92
|
-
CanvasStatsd::Statsd.reset_instance
|
93
|
-
end
|
94
95
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
require 'canvas_statsd'
|
2
2
|
|
3
3
|
RSpec.configure do |config|
|
4
|
-
config.treat_symbols_as_metadata_keys_with_true_values = true
|
5
4
|
config.run_all_when_everything_filtered = true
|
6
5
|
config.filter_run :focus
|
7
6
|
|
7
|
+
config.raise_errors_for_deprecations!
|
8
|
+
|
8
9
|
config.order = 'random'
|
9
10
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: canvas_statsd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Cloward
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2016-11-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: statsd-ruby
|
@@ -128,7 +128,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
128
128
|
version: '0'
|
129
129
|
requirements: []
|
130
130
|
rubyforge_project:
|
131
|
-
rubygems_version: 2.
|
131
|
+
rubygems_version: 2.5.1
|
132
132
|
signing_key:
|
133
133
|
specification_version: 4
|
134
134
|
summary: Statsd for Canvas
|
@@ -143,4 +143,3 @@ test_files:
|
|
143
143
|
- spec/canvas_statsd/statsd_spec.rb
|
144
144
|
- spec/spec_helper.rb
|
145
145
|
- spec/support/test.log
|
146
|
-
has_rdoc:
|