15901 enabled Infinispan metrics

This commit is contained in:
Sebastian Schuster 2022-12-07 17:13:05 +01:00 committed by Pedro Igor
parent 16888eaeab
commit 54c34dc75b
4 changed files with 25 additions and 5 deletions

View file

@ -27,7 +27,10 @@ import java.nio.file.Paths;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
import org.infinispan.commons.util.FileLookupFactory; import org.infinispan.commons.util.FileLookupFactory;
import org.keycloak.config.MetricsOptions;
import org.keycloak.quarkus.runtime.KeycloakRecorder; import org.keycloak.quarkus.runtime.KeycloakRecorder;
import org.keycloak.quarkus.runtime.configuration.Configuration;
import org.keycloak.quarkus.runtime.configuration.MicroProfileConfigProvider;
import org.keycloak.quarkus.runtime.storage.legacy.infinispan.CacheManagerFactory; import org.keycloak.quarkus.runtime.storage.legacy.infinispan.CacheManagerFactory;
import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.arc.deployment.SyntheticBeanBuildItem;
@ -69,7 +72,7 @@ public class CacheBuildSteps {
.scope(ApplicationScoped.class) .scope(ApplicationScoped.class)
.unremovable() .unremovable()
.setRuntimeInit() .setRuntimeInit()
.runtimeValue(recorder.createCacheInitializer(config, shutdownContext)).done()); .runtimeValue(recorder.createCacheInitializer(config, isMetricsEnabled(), shutdownContext)).done());
} catch (Exception cause) { } catch (Exception cause) {
throw new RuntimeException("Failed to read clustering configuration from [" + url + "]", cause); throw new RuntimeException("Failed to read clustering configuration from [" + url + "]", cause);
} }
@ -78,4 +81,7 @@ public class CacheBuildSteps {
} }
} }
private boolean isMetricsEnabled() {
return Configuration.getOptionalBooleanValue(MicroProfileConfigProvider.NS_KEYCLOAK_PREFIX.concat(MetricsOptions.METRICS_ENABLED.getKey())).orElse(false);
}
} }

View file

@ -84,9 +84,9 @@ public class KeycloakRecorder {
QuarkusKeycloakSessionFactory.setInstance(new QuarkusKeycloakSessionFactory(factories, defaultProviders, preConfiguredProviders, themes, reaugmented)); QuarkusKeycloakSessionFactory.setInstance(new QuarkusKeycloakSessionFactory(factories, defaultProviders, preConfiguredProviders, themes, reaugmented));
} }
public RuntimeValue<CacheManagerFactory> createCacheInitializer(String config, ShutdownContext shutdownContext) { public RuntimeValue<CacheManagerFactory> createCacheInitializer(String config, boolean metricsEnabled, ShutdownContext shutdownContext) {
try { try {
CacheManagerFactory cacheManagerFactory = new CacheManagerFactory(config); CacheManagerFactory cacheManagerFactory = new CacheManagerFactory(config, metricsEnabled);
shutdownContext.addShutdownTask(new Runnable() { shutdownContext.addShutdownTask(new Runnable() {
@Override @Override

View file

@ -22,23 +22,28 @@ import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import io.micrometer.core.instrument.Metrics;
import org.infinispan.configuration.parsing.ConfigurationBuilderHolder; import org.infinispan.configuration.parsing.ConfigurationBuilderHolder;
import org.infinispan.configuration.parsing.ParserRegistry; import org.infinispan.configuration.parsing.ParserRegistry;
import org.infinispan.jboss.marshalling.core.JBossUserMarshaller; import org.infinispan.jboss.marshalling.core.JBossUserMarshaller;
import org.infinispan.manager.DefaultCacheManager; import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.metrics.config.MicrometerMeterRegisterConfigurationBuilder;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.keycloak.quarkus.runtime.configuration.Configuration; import org.keycloak.quarkus.runtime.configuration.Configuration;
public class CacheManagerFactory { public class CacheManagerFactory {
private String config; private String config;
private boolean metricsEnabled;
private DefaultCacheManager cacheManager; private DefaultCacheManager cacheManager;
private Future<DefaultCacheManager> cacheManagerFuture; private Future<DefaultCacheManager> cacheManagerFuture;
private ExecutorService executor; private ExecutorService executor;
private boolean initialized; private boolean initialized;
public CacheManagerFactory(String config) { public CacheManagerFactory(String config, boolean metricsEnabled) {
this.config = config; this.config = config;
this.metricsEnabled = metricsEnabled;
this.executor = createThreadPool(); this.executor = createThreadPool();
this.cacheManagerFuture = executor.submit(this::startCacheManager); this.cacheManagerFuture = executor.submit(this::startCacheManager);
} }
@ -78,6 +83,12 @@ public class CacheManagerFactory {
configureTransportStack(builder); configureTransportStack(builder);
} }
if (metricsEnabled) {
builder.getNamedConfigurationBuilders().values().stream().forEach(b -> b.statistics().enable());
builder.getGlobalConfigurationBuilder().addModule(MicrometerMeterRegisterConfigurationBuilder.class);
builder.getGlobalConfigurationBuilder().module(MicrometerMeterRegisterConfigurationBuilder.class).meterRegistry(Metrics.globalRegistry);
}
// For Infinispan 10, we go with the JBoss marshalling. // For Infinispan 10, we go with the JBoss marshalling.
// TODO: This should be replaced later with the marshalling recommended by infinispan. Probably protostream. // TODO: This should be replaced later with the marshalling recommended by infinispan. Probably protostream.
// See https://infinispan.org/docs/stable/titles/developing/developing.html#marshalling for the details // See https://infinispan.org/docs/stable/titles/developing/developing.html#marshalling for the details

View file

@ -25,11 +25,13 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.keycloak.it.junit5.extension.DistributionTest; import org.keycloak.it.junit5.extension.DistributionTest;
import org.keycloak.it.junit5.extension.LegacyStore;
import org.keycloak.it.utils.KeycloakDistribution; import org.keycloak.it.utils.KeycloakDistribution;
import io.quarkus.test.junit.main.Launch; import io.quarkus.test.junit.main.Launch;
@DistributionTest(keepAlive =true) @DistributionTest(keepAlive =true)
@LegacyStore
public class MetricsDistTest { public class MetricsDistTest {
@Test @Test
@ -46,7 +48,8 @@ public class MetricsDistTest {
void testMetricsEndpoint() { void testMetricsEndpoint() {
when().get("/metrics").then() when().get("/metrics").then()
.statusCode(200) .statusCode(200)
.body(containsString("jvm_gc_")); .body(containsString("jvm_gc_"))
.body(containsString("vendor_cache_manager_keycloak_cache_realms_"));
} }
@Test @Test