-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PLM-Jersey-Monitoring] Adding new metrics: Grizzly, HikariDB
- Loading branch information
Showing
11 changed files
with
299 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 2 additions & 17 deletions
19
plume-db/src/main/java/com/coreoz/plume/db/transaction/DataSourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,11 @@ | ||
package com.coreoz.plume.db.transaction; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.inject.Provider; | ||
import jakarta.inject.Singleton; | ||
|
||
import javax.sql.DataSource; | ||
|
||
/** | ||
* Expose a {@link DataSource} Object through dependency injection. | ||
*/ | ||
@Singleton | ||
public class DataSourceProvider implements Provider<DataSource> { | ||
|
||
private final DataSource dataSource; | ||
|
||
@Inject | ||
public DataSourceProvider(TransactionManager transactionManager) { | ||
this.dataSource = transactionManager.dataSource(); | ||
} | ||
|
||
@Override | ||
public DataSource get() { | ||
return dataSource; | ||
} | ||
|
||
public interface DataSourceProvider extends Provider<DataSource> { | ||
} |
30 changes: 30 additions & 0 deletions
30
plume-db/src/main/java/com/coreoz/plume/db/transaction/HikariDataSourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.coreoz.plume.db.transaction; | ||
|
||
import com.typesafe.config.Config; | ||
import com.zaxxer.hikari.HikariDataSource; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Singleton; | ||
|
||
/** | ||
* Expose a {@link HikariDataSource} Object through dependency injection. | ||
*/ | ||
@Singleton | ||
public class HikariDataSourceProvider implements DataSourceProvider { | ||
|
||
private final HikariDataSource dataSource; | ||
|
||
@Inject | ||
public HikariDataSourceProvider(Config config) { | ||
this(config, "db"); | ||
} | ||
|
||
public HikariDataSourceProvider(Config config, String prefix) { | ||
this.dataSource = HikariDataSources.fromConfig(config, prefix + ".hikari"); | ||
} | ||
|
||
@Override | ||
public HikariDataSource get() { | ||
return dataSource; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
plume-web-jersey-monitoring/src/main/resources/application.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
plume-web-jersey-monitoring/src/main/resources/logback.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
109 changes: 109 additions & 0 deletions
109
...c/test/java/com/coreoz/plume/jersey/monitoring/utils/metrics/MetricsCheckBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
Oops, something went wrong.