Since the metrics are batched and each call to send only sends up to the metric limit, shutdown may only send one batch of metrics, rather than all metrics in the queue. Furthermore, since the shutdown method is synchronous there's no guarantee that the metrics have actually been flushed by the time it returns.
Suggestion
Change the signature of shutdown to:
interface Metric {
shutdown: () => Promise<void>;
}
There are a few things that need to happen for this:
- The returned promise should resolve when all the metrics have been sent. This can be achieved by registering an internal
sendCallback to know that the metrics have actually been flushed.
shutdown needs to keep calling the send method until there are no more metrics to sent. It may have to do this according to the sendInterval.
- All the
put methods need to stop accepting new metrics, so as to avoid filling up the queue while trying to drain it. Since the methods are all synchronous and return void, there's no good way to communicate this beyond throwing, but this would be quite a big change for users, so maybe silently dropping metrics added after calling shutdown is a decent compromise? That's essentially what will happen now anyway, so at least it's no worse than the current state.
Concerns
Changing shutdown to start returning a promise means that unless users are aware of this, they may get unhandled promise rejections if something goes wrong. An alternative solution that would make the "block-until-flushed" behavior opt-in would be to have a callback:
interface Metric {
shutdown: (cb?: (err?: Error) => void) => void;
}
Since the metrics are batched and each call to send only sends up to the metric limit,
shutdownmay only send one batch of metrics, rather than all metrics in the queue. Furthermore, since theshutdownmethod is synchronous there's no guarantee that the metrics have actually been flushed by the time it returns.Suggestion
Change the signature of
shutdownto:There are a few things that need to happen for this:
sendCallbackto know that the metrics have actually been flushed.shutdownneeds to keep calling the send method until there are no more metrics to sent. It may have to do this according to thesendInterval.putmethods need to stop accepting new metrics, so as to avoid filling up the queue while trying to drain it. Since the methods are all synchronous and return void, there's no good way to communicate this beyond throwing, but this would be quite a big change for users, so maybe silently dropping metrics added after callingshutdownis a decent compromise? That's essentially what will happen now anyway, so at least it's no worse than the current state.Concerns
Changing
shutdownto start returning a promise means that unless users are aware of this, they may get unhandled promise rejections if something goes wrong. An alternative solution that would make the "block-until-flushed" behavior opt-in would be to have a callback: