Module: Marginalia::ActiveRecordInstrumentation

Defined in:
lib/marginalia.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(instrumented_class) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/marginalia.rb', line 8

def self.included(instrumented_class)
  Marginalia::Comment.components = [:application, :controller, :action]
  instrumented_class.class_eval do
    if instrumented_class.method_defined?(:execute)
      alias_method :execute_without_marginalia, :execute
      alias_method :execute, :execute_with_marginalia
    end

    is_mysql2 = defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) &&
      ActiveRecord::ConnectionAdapters::Mysql2Adapter == instrumented_class
    # Dont instrument exec_query on mysql2 and AR 3.2+, as it calls execute internally
    unless is_mysql2 && ActiveRecord::VERSION::STRING > "3.1"
      if instrumented_class.method_defined?(:exec_query)
        alias_method :exec_query_without_marginalia, :exec_query
        alias_method :exec_query, :exec_query_with_marginalia
      end
    end
  end
end

Instance Method Details

#annotate_sql(sql) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/marginalia.rb', line 28

def annotate_sql(sql)
  comment = Marginalia::Comment.construct_comment
  if comment.present? && !sql.include?(comment)
    "#{sql} /*#{comment}*/"
  else
    sql
  end
end

#exec_query_with_marginalia(sql, name = 'SQL', binds = []) ⇒ Object



41
42
43
# File 'lib/marginalia.rb', line 41

def exec_query_with_marginalia(sql, name = 'SQL', binds = [])
  exec_query_without_marginalia(annotate_sql(sql), name, binds)
end

#execute_with_marginalia(sql, name = nil) ⇒ Object



37
38
39
# File 'lib/marginalia.rb', line 37

def execute_with_marginalia(sql, name = nil)
  execute_without_marginalia(annotate_sql(sql), name)
end