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.



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

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_namesObject (readonly)

Returns the value of attribute blocked_names.



4
5
6
# File 'lib/inst_statsd/sql_tracker.rb', line 4

def blocked_names
  @blocked_names
end

#cache_countsObject (readonly)

Returns the value of attribute cache_counts.



4
5
6
# File 'lib/inst_statsd/sql_tracker.rb', line 4

def cache_counts
  @cache_counts
end

#read_countsObject (readonly)

Returns the value of attribute read_counts.



4
5
6
# File 'lib/inst_statsd/sql_tracker.rb', line 4

def read_counts
  @read_counts
end

#write_countsObject (readonly)

Returns the value of attribute write_counts.



4
5
6
# File 'lib/inst_statsd/sql_tracker.rb', line 4

def write_counts
  @write_counts
end

Instance Method Details

#finalize_counts(cookies) ⇒ Object



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

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

#startObject



14
15
16
# File 'lib/inst_statsd/sql_tracker.rb', line 14

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

#track(name, sql) ⇒ Object



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

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