Prevent slow shutdown caused by waiting full metrics export interval #1827
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.
We noticed that when using the new metrics functionality in the library that our Rails application now took longer to shut down. We want to export metrics every 60 seconds, but we don't want to wait an additional 60 seconds for the app to stop. This was caused by the shutdown logic in
PeriodicMetricReader
which worked by setting@continue
to false, indicating to the loop instart
to no longer continue sleeping and writing. However, this meant that on shutdown, it takes a full@export_interval
in order to "notice" that@continue
is false an the program should exit.With this change, upon
shutdown
, the program performs a finalexport
and then kills the writer thread. This is an approach suggested by @xuan-cao-swi in #1662 (comment).Some questions:
kill
need to acquire the lock first?@continue
to false, if@continue
is no longer used to control shutdown behavior?@continue
even needed any more?ConditionVariable
withwait(@export_mutex, @export_interval)
in the writing loop, andsignal()
inshutdown
? This would allow the writing loop "wake up" responsively on shutdown, while ordinarily waiting@export_interval
before looping and writing again.