Auto-flush on process exit#141
Conversation
This is an ugly first-pass at auto-flushing before the process exits. This previously required manually flushing, which is easy to forget if everything just happens automatically otherwise. Fixes #136. There's a lot that should be cleaned up here (the `BufferedMetricsLogger` just has way too much logic now, for example). But this gets it working and adds tests. This also adds a `close()` method (maybe it should be renamed? stop? shutdown?) that disables autoflushing and flushes any buffered metrics.
|
Cleaned this up a bit and it should be good to go. Besides burying too much code in the constructor, I think my initial implementation was a bit too fancy by trying to retain a subtle previous behavior where you could adjust auto-flushing on the fly by changing Since renaming the method to |
Release v0.12.1 with some maintenance updates and the new `process.beforeExit` functionality from #141.
Release v0.12.1 with some maintenance updates and the new `process.beforeExit` functionality from #141.
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.)