2021-02-10 16:09:24 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Database
|
|
|
|
module Migrations
|
|
|
|
class Instrumentation
|
2021-05-05 11:10:05 -04:00
|
|
|
STATS_FILENAME = 'migration-stats.json'
|
|
|
|
|
2021-10-05 20:11:56 -04:00
|
|
|
def initialize(result_dir:, observer_classes: ::Gitlab::Database::Migrations::Observers.all_observers)
|
2021-08-11 23:10:11 -04:00
|
|
|
@observer_classes = observer_classes
|
2021-10-05 20:11:56 -04:00
|
|
|
@result_dir = result_dir
|
2021-02-10 16:09:24 -05:00
|
|
|
end
|
|
|
|
|
2021-12-16 19:15:24 -05:00
|
|
|
def observe(version:, name:, connection:, &block)
|
2022-02-02 10:17:50 -05:00
|
|
|
observation = Observation.new(version: version, name: name, success: false)
|
2021-02-10 16:09:24 -05:00
|
|
|
|
2022-03-17 17:08:35 -04:00
|
|
|
per_migration_result_dir = File.join(@result_dir, name)
|
|
|
|
|
|
|
|
FileUtils.mkdir_p(per_migration_result_dir)
|
|
|
|
|
|
|
|
observers = observer_classes.map { |c| c.new(observation, per_migration_result_dir, connection) }
|
2021-08-11 23:10:11 -04:00
|
|
|
|
|
|
|
on_each_observer(observers) { |observer| observer.before }
|
2021-02-15 16:08:59 -05:00
|
|
|
|
2022-02-02 10:17:50 -05:00
|
|
|
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
|
|
|
|
|
|
yield
|
|
|
|
|
|
|
|
observation.success = true
|
|
|
|
|
|
|
|
observation
|
|
|
|
ensure
|
|
|
|
observation.walltime = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
|
2021-02-10 16:09:24 -05:00
|
|
|
|
2021-08-11 23:10:11 -04:00
|
|
|
on_each_observer(observers) { |observer| observer.after }
|
|
|
|
on_each_observer(observers) { |observer| observer.record }
|
2021-02-15 16:08:59 -05:00
|
|
|
|
2022-04-05 02:08:25 -04:00
|
|
|
record_observation(observation, destination_dir: per_migration_result_dir)
|
2021-02-10 16:09:24 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-08-11 23:10:11 -04:00
|
|
|
attr_reader :observer_classes
|
2021-02-15 16:08:59 -05:00
|
|
|
|
2022-04-05 02:08:25 -04:00
|
|
|
def record_observation(observation, destination_dir:)
|
|
|
|
stats_file_location = File.join(destination_dir, STATS_FILENAME)
|
|
|
|
File.write(stats_file_location, observation.to_json)
|
2021-02-10 16:09:24 -05:00
|
|
|
end
|
2021-02-15 16:08:59 -05:00
|
|
|
|
2021-08-11 23:10:11 -04:00
|
|
|
def on_each_observer(observers, &block)
|
2021-02-15 16:08:59 -05:00
|
|
|
observers.each do |observer|
|
|
|
|
yield observer
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError => e
|
2021-02-15 16:08:59 -05:00
|
|
|
Gitlab::AppLogger.error("Migration observer #{observer.class} failed with: #{e}")
|
|
|
|
end
|
|
|
|
end
|
2021-02-10 16:09:24 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|