Welcome to mirror list, hosted at ThFree Co, Russian Federation.

query_recorder.rb « query_analyzers « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 88fe829c3d2f530be66d02892e8d9440cbd3cb3e (plain)
1
2
3
4
5
6
7
8
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
# frozen_string_literal: true

module Gitlab
  module Database
    module QueryAnalyzers
      class QueryRecorder < Base
        LOG_FILE = 'rspec/query_recorder.ndjson'

        class << self
          def raw?
            true
          end

          def enabled?
            # Only enable QueryRecorder in CI
            ENV['CI'].present?
          end

          def analyze(sql)
            payload = {
              sql: sql
            }

            log_query(payload)
          end

          private

          def log_query(payload)
            log_path = Rails.root.join(LOG_FILE)
            log_dir = File.dirname(log_path)

            # Create log directory if it does not exist since it is only created
            # ahead of time by certain CI jobs
            FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)

            log_line = "#{Gitlab::Json.dump(payload)}\n"

            File.write(log_path, log_line, mode: 'a')
          end
        end
      end
    end
  end
end