Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shutdown method does not guarantee sending all pending metrics #35

Open
Nevon opened this issue Oct 12, 2020 · 0 comments
Open

Shutdown method does not guarantee sending all pending metrics #35

Nevon opened this issue Oct 12, 2020 · 0 comments

Comments

@Nevon
Copy link
Contributor

Nevon commented Oct 12, 2020

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:

  1. 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.
  2. 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.
  3. 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant