amplitude / Amplitude-Node

Server-side Node.js SDK for Amplitude
MIT License
67 stars 20 forks source link

Missing events even with flush #119

Open florian-milky opened 3 years ago

florian-milky commented 3 years ago

I am sending 13 events with logEvent at about the same time. I am not awaiting for the promise to resolve. Later on, before I terminate, I am calling flush to make sure all events will be sent. Flush responds with status: skipped

Expected Behavior

All events to be logged

Current Behavior

Some events are missing, sometimes 1, sometimes more

Possible Solution

I guess the request is still ongoing for some events? But flush doesn't know about it?

Steps to Reproduce

1. 2. 3. 4.

Environment

ajhorst commented 3 years ago

@florian-milky the skipped response should only be returned from flush when either there are no events remaining in the buffer, or the events are incorrectly formatted. Where is it that you notice events are missing? Are they not appearing on the amplitude website?

florian-milky commented 3 years ago

@ajhorst I log something when the promise is resolved. So I got like 13 logs right before logEvents, and then I got like 10 logs that the promise resolved. Then the flush resolves. On amplitude website I don't see events that didn't resolve.

Now I double checked my code and I realise I expected the promise to throw because I was using async/await, but when I stopped awaiting the logEvent, I removed the async/await which means the code is not gonna throw anymore 🤦

So I'll handle that and check why it is throwing.

Edit: ok obviously not fully awake yet. I am not catching the promise so it would fail if one of them would throw.

Some code:

export function logMetrics(metrics) {
  console.log(`Logging metrics into ${process.env.ENV}`, metrics);
  client
    .logEvent({
      event_type: 'KPI - PageSpeed audit',
      user_id: 'lambda-lighthouse',
      event_properties: metrics,
    })
    .then((r) => console.log(metrics.target, r));
}
   // iterating over some stuff and calling logMetrics

    console.log('Flushing metrics to amplitude');
    const response = await flush();
    if (response.status !== Status.Success && response.status !== Status.Skipped) {
      console.log('Flushing did not work', response);
      throw new Error('Could not flush amplitude events');
    }
    console.log('Flushed', { status: response.status });
ajhorst commented 3 years ago

@florian-milky are there any console warnings in your browser window when you try this?