Module: OpenTelemetry::SDK::Metrics::ForkHooks

Defined in:
lib/opentelemetry/sdk/metrics/fork_hooks.rb

Overview

ForkHooks implements methods to run callbacks before and after forking a Process by overriding Process::_fork This is used to ensure that the PeriodicMetricReader is restarted after forking

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.after_fork ⇒ Object

Notifies metric readers that a fork just occurred.



22
23
24
25
26
# File 'lib/opentelemetry/sdk/metrics/fork_hooks.rb', line 22

def self.after_fork
  ::OpenTelemetry.meter_provider.metric_readers.each do |reader|
    reader.after_fork if reader.respond_to?(:after_fork)
  end
end

.attach! ⇒ Object

Installs the fork hook on Process, idempotently.



14
15
16
17
18
19
# File 'lib/opentelemetry/sdk/metrics/fork_hooks.rb', line 14

def self.attach!
  return if @fork_hooks_attached

  Process.singleton_class.prepend(ForkHooks)
  @fork_hooks_attached = true
end

Instance Method Details

#_fork ⇒ Object

Wraps Process._fork to trigger #after_fork in the child process.



29
30
31
32
33
34
# File 'lib/opentelemetry/sdk/metrics/fork_hooks.rb', line 29

def _fork
  parent_pid = Process.pid
  super.tap do
    ForkHooks.after_fork unless Process.pid == parent_pid
  end
end