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

transaction_duration.rb « observers « migrations « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 182aeb10fda93a5ea9f060461a2766154141c10f (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
# frozen_string_literal: true

module Gitlab
  module Database
    module Migrations
      module Observers
        class TransactionDuration < MigrationObserver
          def before
            file_path = File.join(output_dir, "transaction-duration.json")
            @file = File.open(file_path, 'wb')
            @writer = Oj::StreamWriter.new(@file, {})
            @writer.push_array
            @subscriber = ActiveSupport::Notifications.subscribe('transaction.active_record') do |*args|
              record_sql_event(*args)
            end
          end

          def after
            ActiveSupport::Notifications.unsubscribe(@subscriber)
            @writer.pop_all
            @writer.flush
            @file.close
          end

          def record
            # no-op
          end

          def record_sql_event(_name, started, finished, _unique_id, payload)
            return if payload[:transaction_type] == :fake_transaction

            @writer.push_value({
                                 start_time: started.iso8601(6),
                                 end_time: finished.iso8601(6),
                                 transaction_type: payload[:transaction_type]
                               })
          end
        end
      end
    end
  end
end