aerogear / keycloak-metrics-spi

Adds a Metrics Endpoint to Keycloak
Apache License 2.0
530 stars 152 forks source link

Add keycloak online/offline sessions records #74

Open jermarchand opened 3 years ago

jermarchand commented 3 years ago

Motivation

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

What

Add 2 Gauge :

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:

Progress

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.

pb82 commented 3 years ago

@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
jermarchand commented 3 years ago

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.