Class: Gitlab::Database::QueryAnalyzer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/gitlab/database/query_analyzer.rb

Overview

The purpose of this class is to implement a various query analyzers based on ‘pg_query` And process them all via `Gitlab::Database::QueryAnalyzers::*`

Sometimes this might cause errors in specs. This is best to be disable with ‘describe ’…‘, query_analyzers: false do`

Defined Under Namespace

Classes: Parsed

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueryAnalyzer

Returns a new instance of QueryAnalyzer.



68
69
70
# File 'lib/gitlab/database/query_analyzer.rb', line 68

def initialize
  @all_analyzers = []
end

Instance Attribute Details

#all_analyzersObject (readonly)

Returns the value of attribute all_analyzers.



66
67
68
# File 'lib/gitlab/database/query_analyzer.rb', line 66

def all_analyzers
  @all_analyzers
end

Instance Method Details

#begin!(analyzers) ⇒ Object

Enable query analyzers (only the ones that were not yet enabled) Returns a list of newly enabled analyzers



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/gitlab/database/query_analyzer.rb', line 102

def begin!(analyzers)
  analyzers.select do |analyzer|
    next if enabled_analyzers.include?(analyzer)

    if analyzer.enabled?
      analyzer.begin!
      enabled_analyzers.append(analyzer)

      true
    end
  rescue StandardError, ::Gitlab::Database::QueryAnalyzers::Base::QueryAnalyzerError => e
    Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)

    false
  end
end

#end!(analyzers) ⇒ Object

Disable enabled query analyzers (only the ones that were enabled previously)



120
121
122
123
124
125
126
127
128
# File 'lib/gitlab/database/query_analyzer.rb', line 120

def end!(analyzers)
  analyzers.each do |analyzer|
    next unless enabled_analyzers.delete(analyzer)

    analyzer.end!
  rescue StandardError, ::Gitlab::Database::QueryAnalyzers::Base::QueryAnalyzerError => e
    Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
  end
end

#hook!Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gitlab/database/query_analyzer.rb', line 75

def hook!
  @subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |event|
    # In some cases analyzer code might trigger another SQL call
    # to avoid stack too deep this detects recursive call of subscriber
    with_ignored_recursive_calls do
      process_sql(
        event.payload[:sql],
        event.payload[:connection],
        event.payload[:name].to_s,
        event.payload[:cached]
      )
    end
  end
end

#within(analyzers = all_analyzers) ⇒ Object



90
91
92
93
94
95
96
97
98
# File 'lib/gitlab/database/query_analyzer.rb', line 90

def within(analyzers = all_analyzers)
  newly_enabled_analyzers = begin!(analyzers)

  begin
    yield
  ensure
    end!(newly_enabled_analyzers)
  end
end