How to know when transaction is finished

Hi All,

I was wondering how one can determine if a transaction is finished?

I am working on an angular project and want to log how long a user is logged in.

When the user logs out, I start a transaction, calculate the session duration, and then end the transaction. However, I see in my network traffic in chrome that this transaction is not always sent, because part of the logoutprocess is to redirect the user back to the login page. As far as I can tell, this prevents the transaction from completing and no data is sent to elastic.

Here is the simplified code:

     const transaction = apm.startTransaction('User authenticated logging out', 'custom');
      // some session duration calculation is happening here
    ...
      
      apm.addLabels({ sessionDuration: sessionDuration });
      transaction.end();

      this.router.navigateByUrl('/auth/login'); // sometimes prevents transaction from finishing
   ...

If I delay the rediction by, say, 1 second, like in the code below, then I have no issues and I can see the logout event in Kibana.

     const transaction = apm.startTransaction('User authenticated logging out', 'custom');
      // some session duration calculation is happening here
    ...
      
      apm.addLabels({ sessionDuration: sessionDuration });
      transaction.end();
      setTimeout(() => {
        this.router.navigateByUrl('/auth/login'); // transaction is always finished
      }, 1000);

I've searched online for a solution, but I cant seem to find people experiencing the same problem, which suggests I'm probably doing something wrong.

I did find this api call: transaction.isFInished(): boolean, but unfortunately checking for this to be true before redirecting doesnt make a difference. Also the comment on this function says:

      /**
       * undocumented, might be removed in future versions
       */
      isFinished(): boolean

Which is a real shame, cos it sounds exactly like what I need.

So how do I determine if a transaction is finished before executing other code?

I am using "@elastic/apm-rum-angular": "^1.1.9"

Also, I am very new to APM and this forum, so if more information is needed, please let me know.

Any help is greatly appreciated

Hi @the_duke,

For custom transactions in general, the user is in full control of how and when to end the transaction. So in your case, the transaction is never finished and sent to the browser until you call transaction.end method.

The reason why you are not seeing the transaction event in Kibana is because the browser navigates away from the page and the previous transaction could be lost which explains why the event is sent when you add a timeout with setTimeout call. You can configure how frequent to flush the event queue using the flushInterval config - Configuration | APM Real User Monitoring JavaScript Agent Reference [5.x] | Elastic.

We are aware of this issue and trying to track all events that unloads the previous page. Please track the issue here - Send RUM events for all activity (including partial page loads that don't reach onLoad) · Issue #970 · elastic/apm-agent-rum-js · GitHub and let us know if we can help in any way.

Hi @vigneshshanmugam ,

Thank you for your reply. I will look into the flushInterval and am subscribed to the github issue you mentioned.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.