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.



62
63
64
# File 'lib/gitlab/database/query_analyzer.rb', line 62

def initialize
  @all_analyzers = []
end

Instance Attribute Details

#all_analyzersObject (readonly)

Returns the value of attribute all_analyzers.



60
61
62
# File 'lib/gitlab/database/query_analyzer.rb', line 60

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/gitlab/database/query_analyzer.rb', line 91

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)



109
110
111
112
113
114
115
116
117
# File 'lib/gitlab/database/query_analyzer.rb', line 109

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



69
70
71
72
73
74
75
76
77
# File 'lib/gitlab/database/query_analyzer.rb', line 69

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

#within(analyzers = all_analyzers) ⇒ Object



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

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

  begin
    yield
  ensure
    end!(newly_enabled_analyzers)
  end
end