From c1c5019ac56b44c947fc721a399266b6c6d56f81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Manteaux?= Date: Thu, 7 Nov 2024 17:38:01 +0100 Subject: [PATCH] Add grizzly and hikaricp monitoring --- src/main/java/com/coreoz/WebApplication.java | 2 ++ .../com/coreoz/guice/ApplicationModule.java | 3 --- .../java/com/coreoz/jersey/GrizzlySetup.java | 8 ++++++-- .../webservices/internal/MonitoringWs.java | 18 ++++++++++++------ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/coreoz/WebApplication.java b/src/main/java/com/coreoz/WebApplication.java index 9f9eef2..211bbad 100644 --- a/src/main/java/com/coreoz/WebApplication.java +++ b/src/main/java/com/coreoz/WebApplication.java @@ -4,6 +4,7 @@ import com.coreoz.guice.ApplicationModule; import com.coreoz.jersey.GrizzlySetup; import com.coreoz.plume.admin.services.scheduler.LogApiScheduledJobs; +import com.coreoz.plume.jersey.grizzly.GrizzlyThreadPoolProbe; import com.coreoz.plume.jersey.guice.JerseyGuiceFeature; import com.coreoz.wisp.Scheduler; import com.google.inject.Guice; @@ -46,6 +47,7 @@ public static void main(String[] args) { // starts the server HttpServer httpServer = GrizzlySetup.start( jerseyResourceConfig, + injector.getInstance(GrizzlyThreadPoolProbe.class), System.getProperty("http.port"), System.getProperty("http.address") ); diff --git a/src/main/java/com/coreoz/guice/ApplicationModule.java b/src/main/java/com/coreoz/guice/ApplicationModule.java index 32bfb59..bb99b91 100644 --- a/src/main/java/com/coreoz/guice/ApplicationModule.java +++ b/src/main/java/com/coreoz/guice/ApplicationModule.java @@ -46,9 +46,6 @@ protected void configure() { install(new GuiceFileModule()); bind(FileTypesProvider.class).to(ShowCaseFileTypesProvider.class); - // database setup for the demo - install(new DataSourceModule()); - // prepare Jersey configuration bind(ResourceConfig.class).toProvider(JerseyConfigProvider.class); } diff --git a/src/main/java/com/coreoz/jersey/GrizzlySetup.java b/src/main/java/com/coreoz/jersey/GrizzlySetup.java index 7dd3bd4..65df129 100644 --- a/src/main/java/com/coreoz/jersey/GrizzlySetup.java +++ b/src/main/java/com/coreoz/jersey/GrizzlySetup.java @@ -3,6 +3,7 @@ import java.io.IOException; import java.net.URI; +import com.coreoz.plume.jersey.grizzly.GrizzlyThreadPoolProbe; import jakarta.ws.rs.core.UriBuilder; import org.glassfish.grizzly.http.server.CLStaticHttpHandler; @@ -21,7 +22,7 @@ public class GrizzlySetup { private static final int DEFAULT_HTTP_PORT = 8080; private static final String DEFAULT_HTTP_HOST = "0.0.0.0"; - public static HttpServer start(ResourceConfig jerseyResourceConfig, String httpPort, String httpHost) + public static HttpServer start(ResourceConfig jerseyResourceConfig, GrizzlyThreadPoolProbe grizzlyThreadPoolProbe, String httpPort, String httpHost) throws IOException { // replace JUL logger (used by Grizzly) by SLF4J logger SLF4JBridgeHandler.removeHandlersForRootLogger(); @@ -40,7 +41,10 @@ public static HttpServer start(ResourceConfig jerseyResourceConfig, String httpP false ); - // minimal error page to avoid leaking server information + // thread pool information + httpServer.getServerConfiguration().getMonitoringConfig().getThreadPoolConfig().addProbes(grizzlyThreadPoolProbe); + + // minimal error page to avoid leaking server information httpServer.getServerConfiguration().setDefaultErrorPageGenerator(new GrizzlyErrorPageHandler()); // webjars for swagger ui diff --git a/src/main/java/com/coreoz/webservices/internal/MonitoringWs.java b/src/main/java/com/coreoz/webservices/internal/MonitoringWs.java index f45e2f0..d0e6f20 100644 --- a/src/main/java/com/coreoz/webservices/internal/MonitoringWs.java +++ b/src/main/java/com/coreoz/webservices/internal/MonitoringWs.java @@ -2,6 +2,8 @@ import java.util.Map; +import com.coreoz.plume.jersey.grizzly.GrizzlyThreadPoolProbe; +import com.zaxxer.hikari.HikariDataSource; import jakarta.inject.Inject; import jakarta.inject.Provider; import jakarta.inject.Singleton; @@ -30,8 +32,8 @@ @Singleton public class MonitoringWs { private final ApplicationInfo applicationInfo; - private final Provider healthStatusProvider; - private final Provider> metricsStatusProvider; + private final Provider healthStatus; + private final Provider> metrics; private final BasicAuthenticator basicAuthenticator; @@ -39,17 +41,21 @@ public class MonitoringWs { public MonitoringWs( ApplicationInfoProvider applicationInfoProvider, TransactionManager transactionManager, + GrizzlyThreadPoolProbe grizzlyThreadPoolProbe, + HikariDataSource hikariDataSource, InternalApiAuthenticator apiAuthenticator ) { this.applicationInfo = applicationInfoProvider.get(); // Registering health checks - this.healthStatusProvider = new HealthCheckBuilder() + this.healthStatus = new HealthCheckBuilder() .registerDatabaseHealthCheck(transactionManager) .build(); // Registering metrics to monitor - this.metricsStatusProvider = new MetricsCheckBuilder() + this.metrics = new MetricsCheckBuilder() .registerJvmMetrics() + .registerGrizzlyMetrics(grizzlyThreadPoolProbe) + .registerHikariMetrics(hikariDataSource) .build(); // Require authentication to access monitoring endpoints @@ -67,7 +73,7 @@ public ApplicationInfo info(@Context ContainerRequestContext requestContext) { @Path("/health") public HealthStatus health(@Context ContainerRequestContext requestContext) { basicAuthenticator.requireAuthentication(requestContext); - return this.healthStatusProvider.get(); + return this.healthStatus.get(); } @GET @@ -75,6 +81,6 @@ public HealthStatus health(@Context ContainerRequestContext requestContext) { @Produces(MediaType.APPLICATION_JSON) public Map metrics(@Context ContainerRequestContext requestContext) { basicAuthenticator.requireAuthentication(requestContext); - return metricsStatusProvider.get(); + return metrics.get(); } }