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.



19
20
21
# File 'lib/gitlab/database/query_analyzer.rb', line 19

def initialize
  @all_analyzers = []
end

Instance Attribute Details

#all_analyzersObject (readonly)

Returns the value of attribute all_analyzers.



17
18
19
# File 'lib/gitlab/database/query_analyzer.rb', line 17

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gitlab/database/query_analyzer.rb', line 45

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)



63
64
65
66
67
68
69
70
71
# File 'lib/gitlab/database/query_analyzer.rb', line 63

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



23
24
25
26
27
28
29
30
31
# File 'lib/gitlab/database/query_analyzer.rb', line 23

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])
    end
  end
end

#within(analyzers = all_analyzers) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/gitlab/database/query_analyzer.rb', line 33

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

  begin
    yield
  ensure
    end!(newly_enabled_analyzers)
  end
end