parent
181fcd6d38
commit
d1680a4ccc
2 changed files with 123 additions and 7 deletions
|
@ -1,10 +1,14 @@
|
|||
package io.kokuwa.keycloak.metrics;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
import org.keycloak.events.Event;
|
||||
import org.keycloak.events.EventListenerProvider;
|
||||
import org.keycloak.events.admin.AdminEvent;
|
||||
import org.keycloak.models.KeycloakContext;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.RealmModel;
|
||||
|
||||
import io.micrometer.core.instrument.MeterRegistry;
|
||||
|
||||
|
@ -20,7 +24,7 @@ public class MicrometerEventListener implements EventListenerProvider, AutoClose
|
|||
private final KeycloakSession session;
|
||||
private final boolean replace;
|
||||
|
||||
public MicrometerEventListener(MeterRegistry registry, KeycloakSession session, boolean replaceId) {
|
||||
MicrometerEventListener(MeterRegistry registry, KeycloakSession session, boolean replaceId) {
|
||||
this.registry = registry;
|
||||
this.session = session;
|
||||
this.replace = replaceId;
|
||||
|
@ -50,12 +54,17 @@ public class MicrometerEventListener implements EventListenerProvider, AutoClose
|
|||
public void close() {}
|
||||
|
||||
private String getRealmName(String id) {
|
||||
var model = session.getContext().getRealm();
|
||||
if (id == null || id.equals(model.getId())) {
|
||||
return model.getName();
|
||||
}
|
||||
log.warnv("Failed to resolve realmName for id {0}", id);
|
||||
return id;
|
||||
return Optional.ofNullable(session.getContext()).map(KeycloakContext::getRealm)
|
||||
.filter(realm -> id == null || id.equals(realm.getId()))
|
||||
.or(() -> {
|
||||
log.tracev("Context realm was empty with id {0}", id);
|
||||
return Optional.ofNullable(id).map(session.realms()::getRealm);
|
||||
})
|
||||
.map(RealmModel::getName)
|
||||
.orElseGet(() -> {
|
||||
log.warnv("Failed to find realm with id {0}", id);
|
||||
return id;
|
||||
});
|
||||
}
|
||||
|
||||
private String toBlank(Object value) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue