fix: Recover periodic metric readers after forking #1823
+176
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR provides a fix to
PeriodicMetricReader
so that it continues working after a Ruby process is forked. This problem currently surfaces itself on applications that use forking webservers.When forking a Ruby process only the thread creating the child process is coped across from the parent process, which means that background threads need to be created for monitoring tools to continue working. This means that, before this fix,
PeriodicMetricReader
would not work on the child process as the background thread is not copied.The fix is implemented by overriding the
Process._fork
method provided in Ruby versions 3.1+. Overriding this method allows the authors of monitoring tools to introduce callbacks on fork events.An alternative approach, required prior to 3.1, would require clients to call hooks directly from their forking webserver's configuration such as use
on_worker_boot
inpuma.rb
. OverridingProcess._fork
removes the need for this boilerplate.When resetting the
PeriodicMetricReader
, theafter_fork
hook will first read and discard metrics that would have been published by the parent process. This is in order to avoid duplicate publishing of metrics that will be handled by the parent process.The parent process'
PeriodicMetricReader
is left running after forking as it is assumed that clients will want to publish metrics from it still.