Skip to content

Commit

Permalink
#16 Add support for Dropwizard Metrics 4.0.0
Browse files Browse the repository at this point in the history
Add dropwizard metrics package to support the 4.0.0 package change
  • Loading branch information
vladmihalcea committed Jul 1, 2015
1 parent 3579e5f commit 0c6b2d9
Show file tree
Hide file tree
Showing 21 changed files with 711 additions and 43 deletions.
42 changes: 0 additions & 42 deletions flexy-codahale-metrics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,6 @@
<scope>provided</scope>
</dependency>

<!--<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>${dropwizard.metrics.version}</version>
<scope>provided</scope>
</dependency>-->

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
7 changes: 7 additions & 0 deletions flexy-common-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>com.vladmihalcea.flexy-pool</groupId>
<artifactId>flexy-dropwizard-metrics</artifactId>
<version>1.2.2-SNAPSHOT</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
Expand Down
1 change: 1 addition & 0 deletions flexy-dbcp2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<properties>
<jdk.version>7</jdk.version>
<jdk>${env.JAVA_HOME}</jdk>
<forkMode>always</forkMode>
</properties>
</profile>
</profiles>
Expand Down
57 changes: 57 additions & 0 deletions flexy-dropwizard-metrics/pom.xml
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>
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);
}
}
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();
}
}
}
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;
}
}
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);
}
}
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();
}
}
}
Loading

0 comments on commit 0c6b2d9

Please sign in to comment.