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

instrumentation.rb « migrations « database « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f34768350b1667b5c90eb978be45cb31527b372 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
# frozen_string_literal: true

module Gitlab
  module Database
    module Migrations
      class Instrumentation
        STATS_FILENAME = 'migration-stats.json'

        attr_reader :observations

        def initialize(result_dir:, observer_classes: ::Gitlab::Database::Migrations::Observers.all_observers)
          @observer_classes = observer_classes
          @observations = []
          @result_dir = result_dir
        end

        def observe(version:, name:, connection:, &block)
          observation = Observation.new(version: version, name: name, success: false)

          observers = observer_classes.map { |c| c.new(observation, @result_dir, connection) }

          on_each_observer(observers) { |observer| observer.before }

          start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

          yield

          observation.success = true

          observation
        ensure
          observation.walltime = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start

          on_each_observer(observers) { |observer| observer.after }
          on_each_observer(observers) { |observer| observer.record }

          record_observation(observation)
        end

        private

        attr_reader :observer_classes

        def record_observation(observation)
          @observations << observation
        end

        def on_each_observer(observers, &block)
          observers.each do |observer|
            yield observer
          rescue StandardError => e
            Gitlab::AppLogger.error("Migration observer #{observer.class} failed with: #{e}")
          end
        end
      end
    end
  end
end