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.



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

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.



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

def blocked_names
  @blocked_names
end

#cache_countsObject (readonly)

Returns the value of attribute cache_counts.



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

def cache_counts
  @cache_counts
end

#read_countsObject (readonly)

Returns the value of attribute read_counts.



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

def read_counts
  @read_counts
end

#write_countsObject (readonly)

Returns the value of attribute write_counts.



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

def write_counts
  @write_counts
end

Instance Method Details

#finalize_counts(cookies) ⇒ Object



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

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

#startObject



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

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

#track(name, sql) ⇒ Object



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

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

  if name.include?("CACHE")
    cache_counts.track name
  elsif truncate(sql).include?("SELECT") || name.include?("LOAD")
    read_counts.track(sql)
  else
    write_counts.track(sql)
  end
end