2021-03-11 19:13:27 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module Migrations
|
|
|
|
class Instrumentation
|
2021-06-08 01:23:25 +05:30
|
|
|
STATS_FILENAME = 'migration-stats.json'
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
attr_reader :observations
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
def initialize(result_dir:, observer_classes: ::Gitlab::Database::Migrations::Observers.all_observers)
|
2021-10-27 15:23:28 +05:30
|
|
|
@observer_classes = observer_classes
|
2021-03-11 19:13:27 +05:30
|
|
|
@observations = []
|
2021-11-18 22:05:49 +05:30
|
|
|
@result_dir = result_dir
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def observe(version:, name:, &block)
|
|
|
|
observation = Observation.new(version, name)
|
2021-03-11 19:13:27 +05:30
|
|
|
observation.success = true
|
|
|
|
|
2021-11-18 22:05:49 +05:30
|
|
|
observers = observer_classes.map { |c| c.new(observation, @result_dir) }
|
2021-10-27 15:23:28 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
exception = nil
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
on_each_observer(observers) { |observer| observer.before }
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
observation.walltime = Benchmark.realtime do
|
|
|
|
yield
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError => e
|
2021-03-11 19:13:27 +05:30
|
|
|
exception = e
|
|
|
|
observation.success = false
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
on_each_observer(observers) { |observer| observer.after }
|
|
|
|
on_each_observer(observers) { |observer| observer.record }
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
record_observation(observation)
|
|
|
|
|
|
|
|
raise exception if exception
|
|
|
|
|
|
|
|
observation
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
attr_reader :observer_classes
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
def record_observation(observation)
|
|
|
|
@observations << observation
|
|
|
|
end
|
|
|
|
|
2021-10-27 15:23:28 +05:30
|
|
|
def on_each_observer(observers, &block)
|
2021-03-11 19:13:27 +05:30
|
|
|
observers.each do |observer|
|
|
|
|
yield observer
|
2021-06-08 01:23:25 +05:30
|
|
|
rescue StandardError => e
|
2021-03-11 19:13:27 +05:30
|
|
|
Gitlab::AppLogger.error("Migration observer #{observer.class} failed with: #{e}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|