Class: InstStatsd::SqlTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/inst_statsd/sql_tracker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = nil) ⇒ SqlTracker

Returns a new instance of SqlTracker.



8
9
10
11
12
13
14
# File 'lib/inst_statsd/sql_tracker.rb', line 8

def initialize(opts=nil)
  opts ||= {}
  @blocked_names = opts.fetch(:blocked_names, [])
  @read_counts = opts.fetch(:read_counter, InstStatsd::Counter.new('sql.read'))
  @write_counts = opts.fetch(:write_counter, InstStatsd::Counter.new('sql.write'))
  @cache_counts = opts.fetch(:cache_counter, InstStatsd::Counter.new('sql.cache'))
end

Instance Attribute Details

#blocked_names ⇒ Object (readonly)

Returns the value of attribute blocked_names.



6
7
8
# File 'lib/inst_statsd/sql_tracker.rb', line 6

def blocked_names
  @blocked_names
end

#cache_counts ⇒ Object (readonly)

Returns the value of attribute cache_counts.



6
7
8
# File 'lib/inst_statsd/sql_tracker.rb', line 6

def cache_counts
  @cache_counts
end

#read_counts ⇒ Object (readonly)

Returns the value of attribute read_counts.



6
7
8
# File 'lib/inst_statsd/sql_tracker.rb', line 6

def read_counts
  @read_counts
end

#write_counts ⇒ Object (readonly)

Returns the value of attribute write_counts.



6
7
8
# File 'lib/inst_statsd/sql_tracker.rb', line 6

def write_counts
  @write_counts
end

Instance Method Details

#finalize_counts(cookies) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/inst_statsd/sql_tracker.rb', line 32

def finalize_counts(cookies)
  [
    read_counts.finalize_count(cookies[0]),
    write_counts.finalize_count(cookies[1]),
    cache_counts.finalize_count(cookies[2])
  ]
end

#start ⇒ Object



16
17
18
# File 'lib/inst_statsd/sql_tracker.rb', line 16

def start
  [read_counts, write_counts, cache_counts].map(&:start)
end

#track(name, sql) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/inst_statsd/sql_tracker.rb', line 20

def track name, sql
  return unless sql && accepted_name?(name)

  if name.match(/CACHE/)
    cache_counts.track name
  elsif truncate(sql).match(/SELECT/) || name.match(/LOAD/)
    read_counts.track(sql)
  else
    write_counts.track(sql)
  end
end