Add additional metrics for user/client/session count (#31)
This commit is contained in:
parent
566f31ddc2
commit
37dcc07309
11 changed files with 312 additions and 6 deletions
|
@ -1,5 +1,7 @@
|
|||
package io.kokuwa.keycloak.metrics.junit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
@ -45,7 +47,8 @@ public class KeycloakClient {
|
|||
client.setClientId(clientId);
|
||||
client.setPublicClient(true);
|
||||
client.setDirectAccessGrantsEnabled(true);
|
||||
keycloak.realms().realm(realmName).clients().create(client);
|
||||
var response = keycloak.realms().realm(realmName).clients().create(client);
|
||||
assertEquals(201, response.getStatus());
|
||||
}
|
||||
|
||||
public void createUser(String realmName, String username, String password) {
|
||||
|
@ -59,7 +62,15 @@ public class KeycloakClient {
|
|||
user.setEmailVerified(true);
|
||||
user.setUsername(username);
|
||||
user.setCredentials(List.of(credential));
|
||||
keycloak.realms().realm(realmName).users().create(user);
|
||||
var response = keycloak.realms().realm(realmName).users().create(user);
|
||||
assertEquals(201, response.getStatus());
|
||||
}
|
||||
|
||||
public void deleteUser(String realmName, String username) {
|
||||
keycloak.realms().realm(realmName).users()
|
||||
.searchByUsername(username, true).stream()
|
||||
.map(UserRepresentation::getId)
|
||||
.forEach(keycloak.realms().realm(realmName).users()::delete);
|
||||
}
|
||||
|
||||
public boolean login(String clientId, String realmName, String username, String password) {
|
||||
|
|
|
@ -55,8 +55,11 @@ public class KeycloakExtension implements BeforeAllCallback, ParameterResolver {
|
|||
.withEnv("KEYCLOAK_ADMIN", "admin")
|
||||
.withEnv("KEYCLOAK_ADMIN_PASSWORD", "password")
|
||||
.withEnv("KC_LOG_CONSOLE_COLOR", "true")
|
||||
.withEnv("KC_LOG_LEVEL", "io.kokuwa:trace")
|
||||
.withEnv("KC_HEALTH_ENABLED", "true")
|
||||
.withEnv("KC_METRICS_ENABLED", "true")
|
||||
.withEnv("KC_METRICS_STATS_ENABLED", "true")
|
||||
.withEnv("KC_METRICS_STATS_INTERVAL", "PT1s")
|
||||
.withCopyFileToContainer(MountableFile.forHostPath(jar), "/opt/keycloak/providers/metrics.jar")
|
||||
.withLogConsumer(out -> System.out.print(out.getUtf8String()))
|
||||
.withExposedPorts(8080)
|
||||
|
|
|
@ -41,6 +41,14 @@ public class Prometheus {
|
|||
.sum();
|
||||
}
|
||||
|
||||
public int userCount(String realm) {
|
||||
return state.stream()
|
||||
.filter(metric -> Objects.equals(metric.name(), "keycloak_users"))
|
||||
.filter(metric -> Objects.equals(metric.tags().get("realm"), realm))
|
||||
.mapToInt(metric -> metric.value().intValue())
|
||||
.sum();
|
||||
}
|
||||
|
||||
public void scrap() {
|
||||
state.clear();
|
||||
Stream.of(client.scrap().split("[\\r\\n]+"))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue