Module: Marginalia::ActiveRecordInstrumentation

Defined in:
lib/marginalia.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(instrumented_class) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/marginalia.rb', line 9

def self.included(instrumented_class)
  instrumented_class.class_eval do
    if instrumented_class.method_defined?(:execute)
      alias_method :execute_without_marginalia, :execute
      alias_method :execute, :execute_with_marginalia
    end

    if instrumented_class.private_method_defined?(:execute_and_clear)
      alias_method :execute_and_clear_without_marginalia, :execute_and_clear
      alias_method :execute_and_clear, :execute_and_clear_with_marginalia
    else
      is_mysql2 = defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) &&
        ActiveRecord::ConnectionAdapters::Mysql2Adapter == instrumented_class
      # Dont instrument exec_query on mysql2 as it calls execute internally
      unless is_mysql2
        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

      is_postgres = defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter) &&
        ActiveRecord::ConnectionAdapters::PostgreSQLAdapter == instrumented_class
      # Instrument exec_delete and exec_update since they don't call
      # execute internally
      if is_postgres
        if instrumented_class.method_defined?(:exec_delete)
          alias_method :exec_delete_without_marginalia, :exec_delete
          alias_method :exec_delete, :exec_delete_with_marginalia
        end
        if instrumented_class.method_defined?(:exec_update)
          alias_method :exec_update_without_marginalia, :exec_update
          alias_method :exec_update, :exec_update_with_marginalia
        end
      end
    end
  end
end

Instance Method Details

#annotate_sql(sql) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/marginalia.rb', line 48

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

  sql
end

#exec_delete_with_marginalia(sql, *args) ⇒ Object



80
81
82
# File 'lib/marginalia.rb', line 80

def exec_delete_with_marginalia(sql, *args)
  exec_delete_without_marginalia(annotate_sql(sql), *args)
end

#exec_query_with_marginalia(sql, *args, **options) ⇒ Object



75
76
77
78
# File 'lib/marginalia.rb', line 75

def exec_query_with_marginalia(sql, *args, **options)
  options[:prepare] ||= false
  exec_query_without_marginalia(annotate_sql(sql), *args, **options)
end

#exec_update_with_marginalia(sql, *args) ⇒ Object



85
86
87
# File 'lib/marginalia.rb', line 85

def exec_update_with_marginalia(sql, *args)
  exec_update_without_marginalia(annotate_sql(sql), *args)
end

#execute_and_clear_with_marginalia(sql, *args, &block) ⇒ Object



90
91
92
# File 'lib/marginalia.rb', line 90

def execute_and_clear_with_marginalia(sql, *args, &block)
  execute_and_clear_without_marginalia(annotate_sql(sql), *args, &block)
end

#execute_with_marginalia(sql, *args) ⇒ Object



70
71
72
# File 'lib/marginalia.rb', line 70

def execute_with_marginalia(sql, *args)
  execute_without_marginalia(annotate_sql(sql), *args)
end