Search code examples
keycloakquarkusmicrometer

How to add custom metrics to Keycloak 22 with Quarkus and MicroMeter


The recent versions of Keycloak, at leat 22.0.1, which are based on Quarkus now provide MicroMeter metrics in Prometheus format as documented on https://www.keycloak.org/server/configuration-metrics.

I would like to write an extension JAR that adds some further metrics about about e.g. number of logins to each realm based on the Keycloak EventListenerProvider and RealmResourceProvider.

I've got everything working using my own PrometheusMeterRegistry that is available at a custom endpoint /auth/realms/master/custom-metrics. Somewhat similar to https://github.com/aerogear/keycloak-metrics-spi

The problem is, that Prometheus can only scape one URL, either the original or my custom one.

How can I add my custom metrics to the original MeterRegistry?

As the Keycloak metrics do not contain anything Keycloak specific, I assume they are coming from the underlying Quarkus server. So would I have to write a Quarkus extension to be able to use @Inject MeterRegistry meterRegistry? And combine that with implements RealmResourceProviderFactory? Or is there some Keycloak SPI that returns the MeterRegistry?


Solution

  • Not sure of the actual Micrometer Keycloak setup, but most likely the quarkus-micrometer extension is already there and you can see how it works here:

    https://quarkus.io/guides/telemetry-micrometer#obtain-a-reference-to-a-meterregistry