Module: Bootprint::InitializerProfiler::Instrumentation

Defined in:
lib/bootprint/initializer_profiler.rb

Instance Method Summary collapse

Instance Method Details

#run(*args) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/bootprint/initializer_profiler.rb', line 52

def run(*args)
  start_order = Bootprint::RailsState.initializers&.length.to_i + 1
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  constants_before = Object.constants.to_set
  config_before = Bootprint::InitializerProfiler.config_fingerprint
  missing_environment = []
  Thread.current[:bootprint_missing_environment] = missing_environment
  network_operation = false
  tracer = TracePoint.new(:call, :c_call) do |trace|
    owner = trace.defined_class.to_s
    network_operation = true if NETWORK_METHODS.include?(trace.method_id) && owner.match?(/Socket|Net::HTTP|OpenURI/)
  end
  tracer.enable
  exception = nil
  result = super
  result
rescue Exception => error # rubocop:disable Lint/RescueException -- record boot failures, then re-raise
  exception = { "class" => error.class.name, "message" => Sanitizer.text(error.message) }
  raise
ensure
  tracer&.disable
  duration = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1_000
  loaded = (Object.constants.to_set - constants_before).map(&:to_s).sort
  config_after = Bootprint::InitializerProfiler.config_fingerprint
  mutations = config_after.keys.reject { |key| config_before[key] == config_after[key] }
  Bootprint::RailsState.record_initializer(
    "name" => name.to_s,
    "start_order" => start_order,
    "completion_order" => Bootprint::RailsState.initializers&.length.to_i + 1,
    "duration_ms" => duration.round(3),
    "exception" => exception,
    "loaded_constants" => loaded.take(100),
    "missing_environment_variables" => missing_environment.uniq.sort,
    "network_operation_heuristic" => network_operation,
    "configuration_mutations" => mutations,
    "configuration_mutation_heuristic" => !mutations.empty?
  )
  Thread.current[:bootprint_missing_environment] = nil
end