How to Track Client or Actor Online Duration?

I am currently facing a requirement to track the usage duration of each client or actor within my system. The goal is to calculate how long each user is online or interacting with the system. To achieve this, I need to explore whether there are any relevant APIs or database fields that can be utilized for this purpose.

Here are a few key questions and considerations:

  1. Existing API Support: Does the system provide built-in APIs to fetch connection times, session start/end, or activity duration?
  2. Database Fields: Are there specific columns in the database that store timestamps for login and logout events, or track active sessions?
  3. Custom Tracking: If no such API or field exists, what are the best practices for implementing custom tracking mechanisms, such as logging connection events and calculating the time difference between them?

The goal is to implement an efficient way to monitor online time, ideally by leveraging existing infrastructure without adding significant overhead to the system.

Any suggestions on how to approach this, or examples from similar implementations, would be greatly appreciated.