Skip to content

Shutdown method does not guarantee sending all pending metrics #35

Description

@Nevon

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;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions