debian-mirror-gitlab/lib/gitlab/database/migrations/instrumentation.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

61 lines
1.8 KiB
Ruby
Raw Normal View History

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-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-11-18 22:05:49 +05:30
@result_dir = result_dir
2021-03-11 19:13:27 +05:30
end
2022-01-26 12:08:38 +05:30
def observe(version:, name:, connection:, &block)
2022-04-04 11:22:00 +05:30
observation = Observation.new(version: version, name: name, success: false)
2021-03-11 19:13:27 +05:30
2022-05-07 20:08:51 +05:30
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-10-27 15:23:28 +05:30
on_each_observer(observers) { |observer| observer.before }
2021-03-11 19:13:27 +05:30
2022-04-04 11:22:00 +05:30
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
yield
observation.success = true
observation
ensure
observation.walltime = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
2021-03-11 19:13:27 +05:30
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
2022-06-21 17:19:12 +05:30
record_observation(observation, destination_dir: per_migration_result_dir)
2021-03-11 19:13:27 +05:30
end
private
2021-10-27 15:23:28 +05:30
attr_reader :observer_classes
2021-03-11 19:13:27 +05:30
2022-06-21 17:19:12 +05:30
def record_observation(observation, destination_dir:)
stats_file_location = File.join(destination_dir, STATS_FILENAME)
File.write(stats_file_location, observation.to_json)
2021-03-11 19:13:27 +05:30
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