Auto-flush on process exit #141
Merged
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 is a bit of an ugly first pass. The code needs some cleanup and there are some open questions, but it works and has docs and tests.Fixes #136.This attaches to the process’s
beforeExitevent (supported in Node.js, Deno, and Bun) when auto-flushing is enabled in order to automatically flush when a program exits. Before, you needed to remember to callmetrics.flush()manually at the end of your program, which is easy to forget (or not even know about in the first place!).It also adds a
close()method (not sure about the name) that flushes and buffered metrics and disables auto-flushing. It does not prevent adding new metrics to the buffer, but maybe it should? (Would they error? Get dropped silently? Call theonErrorhandler?)To-do/open questions:
BufferedMetricsLoggerconstructor.close()a good name? Other ideas: (Update: renamed tostop)stop()shutdown()destroy()disable()pause()close()also prevent buffering new metrics? (i.e. makeaddPoint()a no-op, or maybe throw an exception or call the global error handler) (Update: decided no for now.)