diff --git a/gearman-server/pom.xml b/gearman-server/pom.xml
index 786e30b..043154f 100644
--- a/gearman-server/pom.xml
+++ b/gearman-server/pom.xml
@@ -88,6 +88,11 @@
metrics-servlets
${metrics.version}
+
+ io.dropwizard.metrics
+ metrics-graphite
+ ${metrics.version}
+
io.dropwizard.metrics
metrics-annotation
@@ -201,4 +206,4 @@
-
\ No newline at end of file
+
diff --git a/gearman-server/src/main/java/net/johnewart/gearman/server/GearmanDaemon.java b/gearman-server/src/main/java/net/johnewart/gearman/server/GearmanDaemon.java
index 032b75e..509d024 100644
--- a/gearman-server/src/main/java/net/johnewart/gearman/server/GearmanDaemon.java
+++ b/gearman-server/src/main/java/net/johnewart/gearman/server/GearmanDaemon.java
@@ -1,5 +1,6 @@
package net.johnewart.gearman.server;
+import net.johnewart.gearman.server.net.GearmanGraphiteReporter;
import net.johnewart.gearman.server.config.DefaultServerConfiguration;
import net.johnewart.gearman.server.config.GearmanServerConfiguration;
import net.johnewart.gearman.server.net.ServerListener;
@@ -18,19 +19,21 @@ public static void main(String... args)
{
final String configFile;
- if (args.length != 1) {
+ if (args.length < 1) {
configFile = "config.yml";
} else {
- configFile = args[0];
+ configFile = args[args.length - 1];
}
final GearmanServerConfiguration serverConfiguration = loadFromConfigOrGenerateDefaultConfig(configFile);
final ServerListener serverListener = new ServerListener(serverConfiguration);
final WebListener webListener = new WebListener(serverConfiguration);
+ final GearmanGraphiteReporter gearmanGraphiteReporter = new GearmanGraphiteReporter(serverConfiguration);
try {
webListener.start();
+ gearmanGraphiteReporter.start();
serverListener.start();
} catch (Exception e) {
e.printStackTrace();
diff --git a/gearman-server/src/main/java/net/johnewart/gearman/server/config/GearmanGraphiteReporterConfiguration.java b/gearman-server/src/main/java/net/johnewart/gearman/server/config/GearmanGraphiteReporterConfiguration.java
new file mode 100644
index 0000000..042c086
--- /dev/null
+++ b/gearman-server/src/main/java/net/johnewart/gearman/server/config/GearmanGraphiteReporterConfiguration.java
@@ -0,0 +1,84 @@
+package net.johnewart.gearman.server.config;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.TimeUnit;
+
+public class GearmanGraphiteReporterConfiguration {
+
+ private static final Logger LOG = LoggerFactory.getLogger(GearmanGraphiteReporterConfiguration.class);
+
+ private Integer port;
+ private Long period;
+ private String host;
+ private String prefix;
+ private TimeUnit durations;
+ private TimeUnit rates;
+
+ public String getHost() {
+ return host == null ? "localhost" : host;
+ }
+
+ @SuppressWarnings("unused")
+ public void setHost(String host) {
+ this.host = host;
+ }
+
+ public int getPort() {
+ return port == null ? 2003 : port;
+ }
+
+ @SuppressWarnings("unused")
+ public void setPort(int port) {
+ this.port = port;
+ }
+
+ public String getPrefix() {
+ return prefix == null ? "" : prefix;
+ }
+
+ @SuppressWarnings("unused")
+ public void setPrefix(String prefix) {
+ this.prefix = prefix;
+ }
+
+ public TimeUnit getRates() {
+ return rates == null ? TimeUnit.SECONDS : rates;
+ }
+
+ @SuppressWarnings("unused")
+ public void setRates(String rates) {
+ if (rates != null) {
+ try {
+ this.rates = TimeUnit.valueOf(rates.toUpperCase());
+ } catch (IllegalArgumentException e) {
+ LOG.warn("Got illegal rates value : {}", rates);
+ }
+ }
+ }
+
+ public TimeUnit getDurations() {
+ return durations == null ? TimeUnit.SECONDS : durations;
+ }
+
+ @SuppressWarnings("unused")
+ public void setDurations(String durations) {
+ if (durations != null) {
+ try {
+ this.durations = TimeUnit.valueOf(durations.toUpperCase());
+ } catch (IllegalArgumentException e) {
+ LOG.warn("Got illegal durations value : {}", durations);
+ }
+ }
+ }
+
+ public long getPeriod() {
+ return period == null ? 60 : period;
+ }
+
+ @SuppressWarnings("unused")
+ public void setPeriod(long period) {
+ this.period = period;
+ }
+}
diff --git a/gearman-server/src/main/java/net/johnewart/gearman/server/config/GearmanServerConfiguration.java b/gearman-server/src/main/java/net/johnewart/gearman/server/config/GearmanServerConfiguration.java
index dc18cee..fe53abb 100644
--- a/gearman-server/src/main/java/net/johnewart/gearman/server/config/GearmanServerConfiguration.java
+++ b/gearman-server/src/main/java/net/johnewart/gearman/server/config/GearmanServerConfiguration.java
@@ -36,6 +36,7 @@ public class GearmanServerConfiguration implements ServerConfiguration {
private JobQueueMonitor jobQueueMonitor;
private ExceptionStorageEngine exceptionStorageEngine;
private PersistenceEngineConfiguration persistenceEngine;
+ private GearmanGraphiteReporterConfiguration graphite;
private ClusterConfiguration clusterConfiguration;
private ExceptionStoreConfiguration exceptionStoreConfiguration;
private JobHandleFactory jobHandleFactory;
@@ -239,4 +240,12 @@ public HealthCheckRegistry getHealthCheckRegistry()
}
return healthCheckRegistry;
}
+
+ public GearmanGraphiteReporterConfiguration getGraphite() {
+ return graphite;
+ }
+
+ public void setGraphite(GearmanGraphiteReporterConfiguration graphite) {
+ this.graphite = graphite;
+ }
}
diff --git a/gearman-server/src/main/java/net/johnewart/gearman/server/net/GearmanGraphiteReporter.java b/gearman-server/src/main/java/net/johnewart/gearman/server/net/GearmanGraphiteReporter.java
new file mode 100644
index 0000000..47be9ab
--- /dev/null
+++ b/gearman-server/src/main/java/net/johnewart/gearman/server/net/GearmanGraphiteReporter.java
@@ -0,0 +1,46 @@
+package net.johnewart.gearman.server.net;
+
+import com.codahale.metrics.MetricFilter;
+import com.codahale.metrics.graphite.Graphite;
+import com.codahale.metrics.graphite.GraphiteReporter;
+import net.johnewart.gearman.server.config.GearmanGraphiteReporterConfiguration;
+import net.johnewart.gearman.server.config.GearmanServerConfiguration;
+
+import java.util.concurrent.TimeUnit;
+
+public class GearmanGraphiteReporter {
+
+ private final boolean enabled;
+ private final GraphiteReporter reporter;
+ private final long period;
+
+ public GearmanGraphiteReporter(GearmanServerConfiguration configuration) {
+ GearmanGraphiteReporterConfiguration graphiteConfiguration = configuration.getGraphite();
+ if (graphiteConfiguration != null) {
+ enabled = true;
+ final Graphite graphite = new Graphite(graphiteConfiguration.getHost(), graphiteConfiguration.getPort());
+ period = graphiteConfiguration.getPeriod();
+
+ reporter = GraphiteReporter.forRegistry(configuration.getMetricRegistry())
+ .prefixedWith(graphiteConfiguration.getPrefix())
+ .convertRatesTo(graphiteConfiguration.getRates())
+ .convertDurationsTo(graphiteConfiguration.getDurations())
+ .filter(MetricFilter.ALL)
+ .build(graphite);
+ } else {
+ reporter = null;
+ period = 0L;
+ enabled = false;
+ }
+ }
+
+ public void start() {
+ if (enabled) {
+ reporter.start(period, TimeUnit.SECONDS);
+ }
+ }
+
+ boolean isEnabled() {
+ return enabled;
+ }
+}
diff --git a/gearman-server/src/test/java/net/johnewart/gearman/server/net/GearmanGraphiteReporterTest.java b/gearman-server/src/test/java/net/johnewart/gearman/server/net/GearmanGraphiteReporterTest.java
new file mode 100644
index 0000000..c62bd4b
--- /dev/null
+++ b/gearman-server/src/test/java/net/johnewart/gearman/server/net/GearmanGraphiteReporterTest.java
@@ -0,0 +1,31 @@
+package net.johnewart.gearman.server.net;
+
+import net.johnewart.gearman.server.config.GearmanGraphiteReporterConfiguration;
+import net.johnewart.gearman.server.config.GearmanServerConfiguration;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Created by genus on 08.12.2016.
+ */
+public class GearmanGraphiteReporterTest {
+
+ @Test
+ public void disabled() throws Exception {
+ final GearmanServerConfiguration configuration = new GearmanServerConfiguration();
+ final GearmanGraphiteReporter gearmanGraphiteReporter = new GearmanGraphiteReporter(configuration);
+
+ assertFalse(gearmanGraphiteReporter.isEnabled());
+ }
+
+ @Test
+ public void enabled() throws Exception {
+ final GearmanServerConfiguration configuration = new GearmanServerConfiguration();
+ configuration.setGraphite(new GearmanGraphiteReporterConfiguration());
+ final GearmanGraphiteReporter gearmanGraphiteReporter = new GearmanGraphiteReporter(configuration);
+
+ assertTrue(gearmanGraphiteReporter.isEnabled());
+
+ }
+}