keycloak-metrics-spi icon indicating copy to clipboard operation
keycloak-metrics-spi copied to clipboard

Add keycloak online/offline sessions records

Open jermarchand opened this issue 4 years ago • 2 comments

Motivation

Add online/offline sessions by realm and clientId. Similar to #51

What

Add 2 Gauge :

  • keycloak_online_sessions
  • keycloak_offline_sessions

Why

Show the number of sessions

How

On each user/admin events, put in a Gauge the result of the getActiveClientSessionStats.

Verification Steps

Add the steps required to check this change. Following an example.

  1. Build the SPI from this branch and start Keycloak with it.
  2. Login with users
  3. Open the metrics endpoint in a browser.

Checklist:

  • [x] Code has been tested locally by PR requester
  • [ ] Changes have been successfully verified by another team member

Progress

  • [x] Finished task

Additional Notes

I'm a newby with Prometheus metrics, so fell free to comment and propose better implementation.

The getActiveClientSessionStats contains only ClientId with active sessions. When all sessions of this client are logout, I don't found a solution to set the metric to 0.0.

jermarchand avatar Aug 13 '20 16:08 jermarchand

@jermarchand I tried it and it seems to work, only one question: when does a session count as offline? I logged in and then closed the tab but the counter is still counting it as online:

keycloak_online_sessions{realm="user_realm",client_id="account",} 1.0

pb82 avatar Nov 16 '20 13:11 pb82

Closing a tab does not send a disconnection request. When a session is terminated due to "SSO Session Idle", no event is generated :(

When a ClientId has no more sessions it does not appear in the response of "getActiveClientSessionStats" so it might be necessary to send a metric to 0 for all existing clients ....

On one of our Keycloak clusters where we tested this metrics, we had a crash for several reasons (mem + network + ..). Today we do not know if one of these reasons is to have called too much the "getActiveClientSessionStats" which is not stable when there are more than 500,000 sessions.

I don't know if this PR should be merged until someone do more tests.

jermarchand avatar Nov 21 '20 14:11 jermarchand