module Metrician::QueryInterceptor

Constants

COMMAND_EXP
OTHER
QUERY_METRIC
SQL_EXP

Public Class Methods

included(instrumented_class) click to toggle source
# File lib/metrician/reporters/active_record.rb, line 25
def self.included(instrumented_class)
  return if instrumented_class.method_defined?(:log_without_metrician)
  instrumented_class.class_eval do
    alias_method :log_without_metrician, :log
    alias_method :log, :log_with_metrician
    protected :log
  end
end

Public Instance Methods

log_with_metrician(*args, &block) click to toggle source
# File lib/metrician/reporters/active_record.rb, line 34
def log_with_metrician(*args, &block)
  start_time = Time.now.to_f
  sql, name, _binds = args
  sql = sql.dup.force_encoding(Encoding::BINARY)
  config = Metrician.configuration[:database]
  metrics = []
  metrics << QUERY_METRIC if config[:query][:enabled]
  if [:command, :table, :command_and_table].any?{ |key| config[key][:enabled] }
    command, table = parse_sql(sql)
    metrics << "database.#{command}" if config[:command][:enabled] && command
    metrics << "database.#{table}" if config[:table][:enabled] && table
    metrics << "database.#{command}.#{table}" if config[:command_and_table][:enabled] && command && table
  end
  begin
    log_without_metrician(*args, &block)
  ensure
    duration = Time.now.to_f - start_time
    metrics.each { |m| Metrician.gauge(m, duration) }
  end
end
parse_sql(sql) click to toggle source
# File lib/metrician/reporters/active_record.rb, line 55
def parse_sql(sql)
  match = sql.match(SQL_EXP)
  command = (match && match[1].downcase) || OTHER
  table = (match && match[2] && match[2].downcase)
  [command, table]
end