Skip to content

Commit

Permalink
[PLM-Jersey-Monitoring] Adding new metrics: Grizzly, HikariDB
Browse files Browse the repository at this point in the history
  • Loading branch information
wlatif authored and Wail-LT committed Oct 15, 2024
1 parent 543465e commit 2e46588
Show file tree
Hide file tree
Showing 13 changed files with 309 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,16 @@ public class TransactionManagerQuerydsl extends TransactionManager {
private final Configuration querydslConfiguration;

@Inject
public TransactionManagerQuerydsl(Config config) {
this(config, "db");
public TransactionManagerQuerydsl(DataSource dataSource, Config config) {
super(dataSource);
String dialect = config.getString("db.dialect");
this.querydslConfiguration = new Configuration(QuerydslTemplates.valueOf(dialect).sqlTemplates());
}

public TransactionManagerQuerydsl(Config config, String prefix) {
super(config, prefix);

String dialect = config.getString(prefix + ".dialect");
this.querydslConfiguration = new Configuration(QuerydslTemplates.valueOf(dialect).sqlTemplates());
}

public TransactionManagerQuerydsl(DataSource dataSource, Configuration querydslConfiguration) {
super(dataSource);

this.querydslConfiguration = querydslConfiguration;
}
public TransactionManagerQuerydsl(DataSource dataSource, Configuration querydslConfiguration) {
super(dataSource);
this.querydslConfiguration = querydslConfiguration;
}

// API

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import javax.sql.DataSource;

import com.coreoz.plume.db.transaction.DataSourceProvider;
import com.coreoz.plume.db.transaction.HikariDataSourceProvider;
import com.google.inject.AbstractModule;
import com.zaxxer.hikari.HikariDataSource;

public class DataSourceModule extends AbstractModule {

@Override
protected void configure() {
bind(DataSource.class).toProvider(DataSourceProvider.class);
}

@Override
protected void configure() {
bind(DataSource.class).toProvider(HikariDataSourceProvider.class);
bind(HikariDataSource.class).toProvider(HikariDataSourceProvider.class);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.coreoz.plume.db.transaction;

import com.typesafe.config.Config;
import com.zaxxer.hikari.HikariDataSource;
import jakarta.inject.Inject;
import jakarta.inject.Provider;
import jakarta.inject.Singleton;

/**
* Expose a {@link HikariDataSource} Object through dependency injection.
*/
@Singleton
public class HikariDataSourceProvider implements Provider<HikariDataSource> {

private final HikariDataSource dataSource;

@Inject
public HikariDataSourceProvider(Config config) {
this.dataSource = HikariDataSources.fromConfig(config, "db.hikari");
}

@Override
public HikariDataSource get() {
return dataSource;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
public class HikariDataSources {

public static DataSource fromConfig(Config config, String prefix) {
public static HikariDataSource fromConfig(Config config, String prefix) {
return new HikariDataSource(createHikariConfig(config, prefix));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import javax.sql.DataSource;

import com.google.common.base.Throwables;
import com.typesafe.config.Config;

/**
* Handle transactions over a classic JDBC {@link Connection}.
Expand All @@ -20,18 +19,10 @@ public class TransactionManager {

private final DataSource dataSource;

@Inject
public TransactionManager(Config config) {
this(config, "db");
}

public TransactionManager(Config config, String prefix) {
this(HikariDataSources.fromConfig(config, prefix + ".hikari"));
}

public TransactionManager(DataSource dataSource) {
this.dataSource = dataSource;
}
@Inject
public TransactionManager(DataSource dataSource) {
this.dataSource = dataSource;
}

// API

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
import static org.assertj.core.api.Assertions.assertThat;

import java.sql.Connection;
import java.sql.SQLException;

import com.zaxxer.hikari.HikariDataSource;
import org.junit.Test;

import com.typesafe.config.ConfigFactory;

public class TransactionManagerTest {

private final HikariDataSource dataSource = new HikariDataSourceProvider(ConfigFactory.load()).get();

@Test
public void should_disable_autocommit_during_transaction() throws SQLException {
TransactionManager transactionManager = new TransactionManager(ConfigFactory.load());
public void should_disable_autocommit_during_transaction() {
TransactionManager transactionManager = new TransactionManager(dataSource);
transactionManager.execute(connection -> {
try {
assertThat(connection.getAutoCommit()).isFalse();
Expand All @@ -24,8 +26,8 @@ public void should_disable_autocommit_during_transaction() throws SQLException {
}

@Test
public void should_leave_connection_with_autocommit() throws SQLException {
TransactionManager transactionManager = new TransactionManager(ConfigFactory.load());
public void should_leave_connection_with_autocommit() {
TransactionManager transactionManager = new TransactionManager(dataSource);
transactionManager.execute(connection -> {
try {
connection.prepareStatement("SELECT 1").execute();
Expand Down
18 changes: 18 additions & 0 deletions plume-web-jersey-monitoring/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<groupId>com.coreoz</groupId>
<artifactId>plume-web-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.coreoz</groupId>
<artifactId>plume-db</artifactId>
Expand Down Expand Up @@ -84,6 +89,19 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

<!-- Tests -->
<dependency>
<groupId>com.coreoz</groupId>
<artifactId>plume-db-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.14.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.coreoz.plume.jersey.monitoring.utils.metrics;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.MetricSet;
import com.codahale.metrics.jvm.MemoryUsageGaugeSet;
import com.codahale.metrics.jvm.ThreadStatesGaugeSet;
import com.coreoz.plume.jersey.grizzly.GrizzlyThreadPoolProbe;
import com.zaxxer.hikari.HikariDataSource;

import jakarta.inject.Provider;
import java.util.Map;
Expand All @@ -27,6 +30,20 @@ public MetricsCheckBuilder registerJvmMetrics() {
return this;
}

public MetricsCheckBuilder registerGrizzlyMetrics(GrizzlyThreadPoolProbe grizzlyThreadPoolProbe) {
this.metricRegistry.register("http-pool.max-size", (Gauge<Integer>) grizzlyThreadPoolProbe::getPoolMaxSize);
this.metricRegistry.register("http-pool.current-size", (Gauge<Integer>) grizzlyThreadPoolProbe::getTasksWaitingSize);
this.metricRegistry.register("http-pool.waiting-size", (Gauge<Integer>) grizzlyThreadPoolProbe::getTasksWaitingSize);
this.metricRegistry.register("http-pool.usage-size", (Gauge<Integer>) grizzlyThreadPoolProbe::getPoolUsageSize);
this.metricRegistry.register("http-pool.usage", (Gauge<Float>) () -> ((float) grizzlyThreadPoolProbe.getPoolUsageSize()) / ((float)grizzlyThreadPoolProbe.getPoolMaxSize()));
return this;
}

public MetricsCheckBuilder registerHikariMetrics(HikariDataSource hikariDataSource) {
hikariDataSource.setMetricRegistry(this.metricRegistry);
return this;
}

public Provider<Map<String, Metric>> build() {
return this::getMetrics;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
db.hikari."dataSourceClassName"="org.h2.jdbcx.JdbcDataSource"
db.hikari."dataSource.url"="jdbc:h2:mem:test"
db.hikari."dataSource.user"=sa
db.hikari."dataSource.password"=sa
db.hikari.minimumIdle=1
db.hikari.maximumPoolSize=1
13 changes: 13 additions & 0 deletions plume-web-jersey-monitoring/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="STDOUT" />
</root>

</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package com.coreoz.plume.jersey.monitoring.utils.metrics;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Metric;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.MetricSet;
import com.coreoz.plume.jersey.grizzly.GrizzlyThreadPoolProbe;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Map;

class MetricsCheckBuilderTest {

private MetricsCheckBuilder metricsCheckBuilder;

@BeforeEach
void setUp() {
metricsCheckBuilder = new MetricsCheckBuilder();
}

@Test
void registerMetric_should_registerSingleMetric() {
Metric mockMetric = mock(Metric.class);

metricsCheckBuilder.registerMetric("custom-metric", mockMetric);

Map<String, Metric> registeredMetrics = metricsCheckBuilder.build().get();
assertTrue(registeredMetrics.containsKey("custom-metric"));
assertEquals(mockMetric, registeredMetrics.get("custom-metric"));
}

@Test
void registerMetric_should_registerMetricSet() {
MetricSet mockMetricSet = mock(MetricSet.class);
Map<String, Metric> mockMetrics = Map.of("metric1", mock(Metric.class), "metric2", mock(Metric.class));
when(mockMetricSet.getMetrics()).thenReturn(mockMetrics);

metricsCheckBuilder.registerMetric("custom-metric-set", mockMetricSet);

Map<String, Metric> registeredMetrics = metricsCheckBuilder.build().get();
assertTrue(registeredMetrics.containsKey("custom-metric-set.metric1"));
assertTrue(registeredMetrics.containsKey("custom-metric-set.metric2"));
}

@Test
void registerJvmMetrics_should_registerJvmMetricsCorrectly() {
metricsCheckBuilder.registerJvmMetrics();

Map<String, Metric> registeredMetrics = metricsCheckBuilder.build().get();
assertTrue(registeredMetrics.containsKey("memory-usage.heap.max"));
assertTrue(registeredMetrics.containsKey("thread-states.runnable.count"));
}

@Test
void registerGrizzlyMetrics_should_registerGrizzlyMetricsCorrectly() {
GrizzlyThreadPoolProbe mockProbe = mock(GrizzlyThreadPoolProbe.class);
when(mockProbe.getPoolMaxSize()).thenReturn(100);
when(mockProbe.getTasksWaitingSize()).thenReturn(10);
when(mockProbe.getPoolUsageSize()).thenReturn(50);

metricsCheckBuilder.registerGrizzlyMetrics(mockProbe);

Map<String, Metric> registeredMetrics = metricsCheckBuilder.build().get();
assertTrue(registeredMetrics.containsKey("http-pool.max-size"));
assertTrue(registeredMetrics.containsKey("http-pool.current-size"));
assertTrue(registeredMetrics.containsKey("http-pool.waiting-size"));
assertTrue(registeredMetrics.containsKey("http-pool.usage-size"));
assertTrue(registeredMetrics.containsKey("http-pool.usage"));

Gauge<Integer> maxSizeGauge = (Gauge<Integer>) registeredMetrics.get("http-pool.max-size");
assertEquals(100, maxSizeGauge.getValue());

Gauge<Integer> waitingSizeGauge = (Gauge<Integer>) registeredMetrics.get("http-pool.waiting-size");
assertEquals(10, waitingSizeGauge.getValue());

Gauge<Float> usageGauge = (Gauge<Float>) registeredMetrics.get("http-pool.usage");
assertEquals(0.5f, usageGauge.getValue(), 0.01);
}

@Test
void registerHikariMetrics_should_registerHikariMetricsCorrectly() {
HikariDataSource mockDataSource = mock(HikariDataSource.class);

metricsCheckBuilder.registerHikariMetrics(mockDataSource);

verify(mockDataSource).setMetricRegistry(any(MetricRegistry.class));
}

@Test
void build_should_returnAllRegisteredMetrics() {
Metric mockMetric1 = mock(Metric.class);
Metric mockMetric2 = mock(Metric.class);

metricsCheckBuilder
.registerMetric("metric1", mockMetric1)
.registerMetric("metric2", mockMetric2);

Map<String, Metric> registeredMetrics = metricsCheckBuilder.build().get();
assertEquals(2, registeredMetrics.size());
assertTrue(registeredMetrics.containsKey("metric1"));
assertTrue(registeredMetrics.containsKey("metric2"));
}
}
Loading

0 comments on commit 2e46588

Please sign in to comment.