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

Count users in keycloak_count_users metric

Open jermarchand opened this issue 4 years ago • 3 comments

Motivation

Add a new metric which is a gauge of the total number of users on the Keycloak instance. https://github.com/aerogear/keycloak-metrics-spi/issues/81

What

Add 1 Gauge :

  • keycloak_count_users

Why

Show the number of users

How

On REGISTER or CREATE/DELETE User events, count the users of the realm.

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. Register a new user
  3. Add new user with admin-console
  4. 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.

Until first REGISTER or CREATE/DELETE User events, the metric is not set.

jermarchand avatar Oct 18 '20 15:10 jermarchand

Thanks for the contribution @jermarchand , this looks reasonable to me

pb82 avatar Oct 20 '20 09:10 pb82

@jermarchand this works as long as users are created or register through the UI. When using the API to create users (like e.g. the keycloak operator does) no register or create event is fired and the users are not counted. One solution would be to count the users on every regular and admin event. Not sure if this could lead to excessive database queries when scraping metrics often?

pb82 avatar Nov 16 '20 13:11 pb82

I don't know how operator work :/ If it use the "Registration flow" a user event "Register" is generated. If it use the Keycloak Admin REST API an admin event "CREATE USER" is generated.

jermarchand avatar Nov 21 '20 14:11 jermarchand