Skip to content

Commit

Permalink
Add grizzly and hikaricp monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
amanteaux committed Nov 7, 2024
1 parent b2a05f4 commit c1c5019
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/coreoz/WebApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
);
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/coreoz/guice/ApplicationModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/coreoz/jersey/GrizzlySetup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/com/coreoz/webservices/internal/MonitoringWs.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -30,26 +32,30 @@
@Singleton
public class MonitoringWs {
private final ApplicationInfo applicationInfo;
private final Provider<HealthStatus> healthStatusProvider;
private final Provider<Map<String, Metric>> metricsStatusProvider;
private final Provider<HealthStatus> healthStatus;
private final Provider<Map<String, Metric>> metrics;

private final BasicAuthenticator<String> basicAuthenticator;

@Inject
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
Expand All @@ -67,14 +73,14 @@ 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
@Path("/metrics")
@Produces(MediaType.APPLICATION_JSON)
public Map<String, Metric> metrics(@Context ContainerRequestContext requestContext) {
basicAuthenticator.requireAuthentication(requestContext);
return metricsStatusProvider.get();
return metrics.get();
}
}

0 comments on commit c1c5019

Please sign in to comment.