From 54c34dc75b4bdf9f5dd8de08f7e39e8e475c9aae Mon Sep 17 00:00:00 2001 From: Sebastian Schuster Date: Wed, 7 Dec 2022 17:13:05 +0100 Subject: [PATCH] 15901 enabled Infinispan metrics --- .../quarkus/deployment/CacheBuildSteps.java | 8 +++++++- .../keycloak/quarkus/runtime/KeycloakRecorder.java | 4 ++-- .../legacy/infinispan/CacheManagerFactory.java | 13 ++++++++++++- .../org/keycloak/it/cli/dist/MetricsDistTest.java | 5 ++++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/CacheBuildSteps.java b/quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/CacheBuildSteps.java index 1d341cac13..096c0b85de 100644 --- a/quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/CacheBuildSteps.java +++ b/quarkus/deployment/src/main/java/org/keycloak/quarkus/deployment/CacheBuildSteps.java @@ -27,7 +27,10 @@ import java.nio.file.Paths; import java.util.stream.Collectors; import javax.enterprise.context.ApplicationScoped; import org.infinispan.commons.util.FileLookupFactory; +import org.keycloak.config.MetricsOptions; 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 io.quarkus.arc.deployment.SyntheticBeanBuildItem; @@ -69,7 +72,7 @@ public class CacheBuildSteps { .scope(ApplicationScoped.class) .unremovable() .setRuntimeInit() - .runtimeValue(recorder.createCacheInitializer(config, shutdownContext)).done()); + .runtimeValue(recorder.createCacheInitializer(config, isMetricsEnabled(), shutdownContext)).done()); } catch (Exception 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); + } } diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/KeycloakRecorder.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/KeycloakRecorder.java index 64867cb295..d5e15eead2 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/KeycloakRecorder.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/KeycloakRecorder.java @@ -84,9 +84,9 @@ public class KeycloakRecorder { QuarkusKeycloakSessionFactory.setInstance(new QuarkusKeycloakSessionFactory(factories, defaultProviders, preConfiguredProviders, themes, reaugmented)); } - public RuntimeValue createCacheInitializer(String config, ShutdownContext shutdownContext) { + public RuntimeValue createCacheInitializer(String config, boolean metricsEnabled, ShutdownContext shutdownContext) { try { - CacheManagerFactory cacheManagerFactory = new CacheManagerFactory(config); + CacheManagerFactory cacheManagerFactory = new CacheManagerFactory(config, metricsEnabled); shutdownContext.addShutdownTask(new Runnable() { @Override diff --git a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/legacy/infinispan/CacheManagerFactory.java b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/legacy/infinispan/CacheManagerFactory.java index 8d9a096983..1146d623f7 100644 --- a/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/legacy/infinispan/CacheManagerFactory.java +++ b/quarkus/runtime/src/main/java/org/keycloak/quarkus/runtime/storage/legacy/infinispan/CacheManagerFactory.java @@ -22,23 +22,28 @@ import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; + +import io.micrometer.core.instrument.Metrics; import org.infinispan.configuration.parsing.ConfigurationBuilderHolder; import org.infinispan.configuration.parsing.ParserRegistry; import org.infinispan.jboss.marshalling.core.JBossUserMarshaller; import org.infinispan.manager.DefaultCacheManager; +import org.infinispan.metrics.config.MicrometerMeterRegisterConfigurationBuilder; import org.jboss.logging.Logger; import org.keycloak.quarkus.runtime.configuration.Configuration; public class CacheManagerFactory { private String config; + private boolean metricsEnabled; private DefaultCacheManager cacheManager; private Future cacheManagerFuture; private ExecutorService executor; private boolean initialized; - public CacheManagerFactory(String config) { + public CacheManagerFactory(String config, boolean metricsEnabled) { this.config = config; + this.metricsEnabled = metricsEnabled; this.executor = createThreadPool(); this.cacheManagerFuture = executor.submit(this::startCacheManager); } @@ -78,6 +83,12 @@ public class CacheManagerFactory { 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. // 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 diff --git a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/MetricsDistTest.java b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/MetricsDistTest.java index fac8acdc1f..03775083a0 100644 --- a/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/MetricsDistTest.java +++ b/quarkus/tests/integration/src/test/java/org/keycloak/it/cli/dist/MetricsDistTest.java @@ -25,11 +25,13 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.Test; import org.keycloak.it.junit5.extension.DistributionTest; +import org.keycloak.it.junit5.extension.LegacyStore; import org.keycloak.it.utils.KeycloakDistribution; import io.quarkus.test.junit.main.Launch; @DistributionTest(keepAlive =true) +@LegacyStore public class MetricsDistTest { @Test @@ -46,7 +48,8 @@ public class MetricsDistTest { void testMetricsEndpoint() { when().get("/metrics").then() .statusCode(200) - .body(containsString("jvm_gc_")); + .body(containsString("jvm_gc_")) + .body(containsString("vendor_cache_manager_keycloak_cache_realms_")); } @Test