datalust / seq-logging

A Node.js client for the Seq HTTP ingestion API
Apache License 2.0
25 stars 15 forks source link

Get final set of data for navigator.sendBeacon #9

Closed ngbrown closed 3 years ago

ngbrown commented 5 years ago

When using seq-logging on a client side application, the flush doesn't work in unload or pagehide events. The sequence of promises and waiting doesn't actually get waited for and the network event doesn't start.

An alternative is to get a final POST data and set up a navigator.sendBeacon(url, data). There is no opportunity to set headers, but setting the url to use ?apiKey= should work. I just need the data string out of the SeqLogger somehow.

What are your thoughts?

nblumhardt commented 5 years ago

Hi Nathan,

This sounds interesting - I can't see any reason it couldn't work :-)

There's going to still be the possibility that the timer in SeqLogger files just before unloading (so perhaps an in-flight batch might not complete?), but getting the contents of the log event queue as a string should be straightforward.

There's also the possibility that the batch will hit the (~64 kB) beacon size limit, but that would still be a big improvement on the status quo.

Perhaps it's worth a sanity check, by either hacking at the source, or accessing the "private" fields in SeqLogger? A production-quality API seems like it'll take a bit of thought, due to the browser-only nature of sendBeacon(), but it's definitely not insurmountable.

ngbrown commented 5 years ago

My solution can be tested through the npm package @ngbrown/bunyan-seq@0.3.3-patch.1.

I use it it my code like this:

function onPagehide() {
  rootLogger.info({url: window.location.href}, 'Application page hiding');
  const result = seqStream.stream.flushToBeacon();
  // eslint-disable-next-line no-console
  console.log(`flushToBeacon result: ${result}`);
}

typeof window !== 'undefined' &&
  window.addEventListener &&
  window.addEventListener('pagehide', onPagehide, false);

I see the resulting network request in the debug tab and the 'Application page hiding' message in the Seq UI. At least for Seq 4.2, it doesn't care that the Content-Type has to be text/plain.

nblumhardt commented 3 years ago

Sounds like this should be covered by #10 - just catching back up on the backlog here, let me know if this is still an issue 👍