-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#16 Add support for Dropwizard Metrics 4.0.0
Add dropwizard metrics package to support the 4.0.0 package change
- Loading branch information
1 parent
3579e5f
commit 0c6b2d9
Showing
21 changed files
with
711 additions
and
43 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<parent> | ||
<groupId>com.vladmihalcea.flexy-pool</groupId> | ||
<artifactId>flexy-pool-parent</artifactId> | ||
<version>1.2.2-SNAPSHOT</version> | ||
</parent> | ||
|
||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>flexy-dropwizard-metrics</artifactId> | ||
<version>1.2.2-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>flexy-dropwizard-metrics</name> | ||
<description>The flexible pool Dropwizard Metrics adapter</description> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>com.vladmihalcea.flexy-pool</groupId> | ||
<artifactId>flexy-pool-core</artifactId> | ||
<version>1.2.2-SNAPSHOT</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>io.dropwizard.metrics</groupId> | ||
<artifactId>metrics-core</artifactId> | ||
<version>${dropwizard.metrics.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<profiles> | ||
<profile> | ||
<id>travis</id> | ||
<activation> | ||
<property> | ||
<name>env.TRAVIS</name> | ||
<value>true</value> | ||
</property> | ||
</activation> | ||
<properties> | ||
<jdk.version>7</jdk.version> | ||
<jdk>${env.JAVA_HOME}</jdk> | ||
<forkMode>always</forkMode> | ||
</properties> | ||
</profile> | ||
</profiles> | ||
|
||
<properties> | ||
<jdk.version>7</jdk.version> | ||
<jdk>${env.JAVA_HOME_7}</jdk> | ||
</properties> | ||
|
||
</project> |
33 changes: 33 additions & 0 deletions
33
...trics/src/main/java/com/vladmihalcea/flexypool/metric/dropwizard/DropwizardHistogram.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,33 @@ | ||
package com.vladmihalcea.flexypool.metric.dropwizard; | ||
|
||
import com.vladmihalcea.flexypool.metric.Histogram; | ||
|
||
/** | ||
* <code>DropwizardHistogram</code> implements the {@link com.vladmihalcea.flexypool.metric.Histogram} interface by | ||
* delegating calls to {@link io.dropwizard.metrics.Histogram} | ||
* | ||
* @author Vlad Mihalcea | ||
* @version %I%, %E% | ||
* @since 1.0 | ||
*/ | ||
public class DropwizardHistogram implements Histogram { | ||
|
||
private final io.dropwizard.metrics.Histogram histogram; | ||
|
||
/** | ||
* Create a {@link io.dropwizard.metrics.Histogram} wrapper | ||
* | ||
* @param histogram actual histogram | ||
*/ | ||
public DropwizardHistogram(io.dropwizard.metrics.Histogram histogram) { | ||
this.histogram = histogram; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void update(long value) { | ||
histogram.update(value); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...metrics/src/main/java/com/vladmihalcea/flexypool/metric/dropwizard/DropwizardMetrics.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,112 @@ | ||
package com.vladmihalcea.flexypool.metric.dropwizard; | ||
|
||
import io.dropwizard.metrics.*; | ||
import com.vladmihalcea.flexypool.metric.*; | ||
import com.vladmihalcea.flexypool.metric.Histogram; | ||
import com.vladmihalcea.flexypool.metric.Timer; | ||
import com.vladmihalcea.flexypool.util.ConfigurationProperties; | ||
|
||
import java.util.Collection; | ||
import java.util.LinkedHashSet; | ||
|
||
/** | ||
* <code>DropwizardMetrics</code> extends the {@link AbstractMetrics} class and configures the Codahale | ||
* {@link MetricRegistry}. By default, the {@link Slf4jReporter} is used for logging metrics data. | ||
* If the Jmx is enabled, a {@link JmxReporter} will also be added. | ||
* <p/> | ||
* This class implements the {@link com.vladmihalcea.flexypool.lifecycle.LifeCycleCallback} interface so it can | ||
* start/stop the metrics reports. | ||
* | ||
* @author Vlad Mihalcea | ||
* @version %I%, %E% | ||
* @since 1.0 | ||
*/ | ||
public class DropwizardMetrics extends AbstractMetrics { | ||
|
||
public static class ReservoirMetricsFactory implements MetricsFactory { | ||
|
||
private final ReservoirFactory reservoirFactory; | ||
|
||
public ReservoirMetricsFactory(ReservoirFactory reservoirFactory) { | ||
this.reservoirFactory = reservoirFactory; | ||
} | ||
|
||
@Override | ||
public Metrics newInstance(ConfigurationProperties configurationProperties) { | ||
return new DropwizardMetrics(configurationProperties, reservoirFactory); | ||
} | ||
} | ||
|
||
public static final MetricsFactory FACTORY = new ReservoirMetricsFactory(new ReservoirFactory() { | ||
@Override | ||
public Reservoir newInstance(Class<? extends Metric> metricClass, String metricName) { | ||
return new ExponentiallyDecayingReservoir(); | ||
} | ||
}); | ||
|
||
public static final MetricsFactory UNIFORM_RESERVOIR_FACTORY = new ReservoirMetricsFactory(new ReservoirFactory() { | ||
@Override | ||
public Reservoir newInstance(Class<? extends Metric> metricClass, String metricName) { | ||
return new UniformReservoir(); | ||
} | ||
}); | ||
|
||
private final MetricRegistry metricRegistry; | ||
|
||
private final ReservoirFactory reservoirFactory; | ||
|
||
private final Collection<MetricsLifeCycleCallback> callbacks = new LinkedHashSet<MetricsLifeCycleCallback>(); | ||
|
||
public DropwizardMetrics(ConfigurationProperties configurationProperties, | ||
ReservoirFactory reservoirFactory, | ||
MetricsLifeCycleCallback... callbacks) { | ||
super(configurationProperties); | ||
this.metricRegistry = new MetricRegistry(); | ||
this.reservoirFactory = reservoirFactory; | ||
this.callbacks.add(new Slf4jMetricReporter().init(configurationProperties, metricRegistry)); | ||
this.callbacks.add(new JmxMetricReporter().init(configurationProperties, metricRegistry)); | ||
for (MetricsLifeCycleCallback callback : callbacks) { | ||
this.callbacks.add(callback.init(configurationProperties, metricRegistry)); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Histogram histogram(String name) { | ||
io.dropwizard.metrics.Histogram histogram = new io.dropwizard.metrics.Histogram( | ||
reservoirFactory.newInstance(io.dropwizard.metrics.Histogram.class, name) | ||
); | ||
return new DropwizardHistogram(metricRegistry.register(name, histogram)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public Timer timer(String name) { | ||
io.dropwizard.metrics.Timer timer = new io.dropwizard.metrics.Timer( | ||
reservoirFactory.newInstance(io.dropwizard.metrics.Timer.class, name) | ||
); | ||
return new DropwizardTimer(metricRegistry.register(name, timer)); | ||
} | ||
|
||
/** | ||
* Start metrics reports. | ||
*/ | ||
public void start() { | ||
for (MetricsLifeCycleCallback callback : callbacks) { | ||
callback.start(); | ||
} | ||
} | ||
|
||
/** | ||
* Stop metrics reports. | ||
*/ | ||
public void stop() { | ||
for (MetricsLifeCycleCallback callback : callbacks) { | ||
callback.stop(); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...in/java/com/vladmihalcea/flexypool/metric/dropwizard/DropwizardMetricsFactoryService.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,24 @@ | ||
package com.vladmihalcea.flexypool.metric.dropwizard; | ||
|
||
import io.dropwizard.metrics.Metric; | ||
import com.vladmihalcea.flexypool.metric.MetricsFactory; | ||
import com.vladmihalcea.flexypool.metric.MetricsFactoryService; | ||
import com.vladmihalcea.flexypool.util.ClassLoaderUtils; | ||
|
||
/** | ||
* DropwizardMetricsFactoryService - Dropwizard MetricsFactoryService | ||
* | ||
* @author Vlad Mihalcea | ||
*/ | ||
public class DropwizardMetricsFactoryService implements MetricsFactoryService { | ||
|
||
/** | ||
* Load DropwizardMetrics Factory if the Codahale Metrics is available at runtime | ||
* | ||
* @return DropwizardMetrics Factory | ||
*/ | ||
@Override | ||
public MetricsFactory load() { | ||
return ClassLoaderUtils.findClass(Metric.class.getName()) ? DropwizardMetrics.FACTORY : null; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...d-metrics/src/main/java/com/vladmihalcea/flexypool/metric/dropwizard/DropwizardTimer.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,35 @@ | ||
package com.vladmihalcea.flexypool.metric.dropwizard; | ||
|
||
import com.vladmihalcea.flexypool.metric.Timer; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* <code>DropwizardTimer</code> implements the {@link com.vladmihalcea.flexypool.metric.Timer} interface by | ||
* delegating calls to {@link io.dropwizard.metrics.Timer} | ||
* | ||
* @author Vlad Mihalcea | ||
* @version %I%, %E% | ||
* @since 1.0 | ||
*/ | ||
public class DropwizardTimer implements Timer { | ||
|
||
private final io.dropwizard.metrics.Timer timer; | ||
|
||
/** | ||
* Create a {@link io.dropwizard.metrics.Timer} wrapper | ||
* | ||
* @param timer actual timer | ||
*/ | ||
public DropwizardTimer(io.dropwizard.metrics.Timer timer) { | ||
this.timer = timer; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public void update(long duration, TimeUnit unit) { | ||
timer.update(duration, unit); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...metrics/src/main/java/com/vladmihalcea/flexypool/metric/dropwizard/JmxMetricReporter.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,57 @@ | ||
package com.vladmihalcea.flexypool.metric.dropwizard; | ||
|
||
import io.dropwizard.metrics.JmxReporter; | ||
import io.dropwizard.metrics.MetricRegistry; | ||
import com.vladmihalcea.flexypool.util.ConfigurationProperties; | ||
|
||
/** | ||
* <code>JmxMetricReporter</code> - Jmx Metric Reporter | ||
* | ||
* @author Vlad Mihalcea | ||
*/ | ||
public class JmxMetricReporter implements MetricsLifeCycleCallback { | ||
|
||
private JmxReporter jmxReporter; | ||
|
||
/** | ||
* The JMX Reporter is activated only if the jmxEnabled property is set. If the jmxAutoStart property is enabled, | ||
* the JMX Reporter will start automatically. | ||
* | ||
* @param configurationProperties configuration properties | ||
* @param metricRegistry metric registry | ||
* @return {@link JmxMetricReporter} | ||
*/ | ||
@Override | ||
public JmxMetricReporter init(ConfigurationProperties configurationProperties, MetricRegistry metricRegistry) { | ||
if (configurationProperties.isJmxEnabled()) { | ||
jmxReporter = JmxReporter | ||
.forRegistry(metricRegistry) | ||
.inDomain(getClass().getName() + "." + configurationProperties.getUniqueName()) | ||
.build(); | ||
} | ||
if(configurationProperties.isJmxAutoStart()) { | ||
start(); | ||
} | ||
return this; | ||
} | ||
|
||
/** | ||
* Start JMX Reporter | ||
*/ | ||
@Override | ||
public void start() { | ||
if (jmxReporter != null) { | ||
jmxReporter.start(); | ||
} | ||
} | ||
|
||
/** | ||
* Stop JMX Reporter | ||
*/ | ||
@Override | ||
public void stop() { | ||
if (jmxReporter != null) { | ||
jmxReporter.stop(); | ||
} | ||
} | ||
} |
Oops, something went wrong.