Class: Gitlab::Database::Migrations::Instrumentation

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/migrations/instrumentation.rb

Constant Summary collapse

STATS_FILENAME =
'migration-stats.json'

Instance Method Summary collapse

Constructor Details

#initialize(result_dir:, observer_classes: ::Gitlab::Database::Migrations::Observers.all_observers) ⇒ Instrumentation

Returns a new instance of Instrumentation.


9
10
11
12
# File 'lib/gitlab/database/migrations/instrumentation.rb', line 9

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

Instance Method Details

#observe(version:, name:, connection:, &block) ⇒ Object


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
# File 'lib/gitlab/database/migrations/instrumentation.rb', line 14

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

  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) }

  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, destination_dir: per_migration_result_dir)
end