Skip to content

Commit

Permalink
Refactor to have the vert.x metrics options in a MetricsOptions data …
Browse files Browse the repository at this point in the history
…object
  • Loading branch information
vietj committed Jan 30, 2015
1 parent a71718b commit 33d8e41
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 103 deletions.
6 changes: 5 additions & 1 deletion src/main/java/io/vertx/core/Starter.java
Expand Up @@ -21,6 +21,7 @@
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.impl.LoggerFactory;
import io.vertx.core.metrics.MetricsOptions;

import java.io.File;
import java.io.FileNotFoundException;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class Starter {

public static final String VERTX_OPTIONS_PROP_PREFIX = "vertx.options.";
public static final String DEPLOYMENT_OPTIONS_PROP_PREFIX = "vertx.deployment.options.";
public static final String METRICS_OPTIONS_PROP_PREFIX = "vertx.metrics.options.";

private static final Logger log = LoggerFactory.getLogger(Starter.class);
public static List<String> PROCESS_ARGS;
Expand Down Expand Up @@ -121,7 +123,9 @@ public void unblock() {
}

private Vertx startVertx(boolean clustered, boolean ha, Args args) {
options = new VertxOptions();
MetricsOptions metricsOptions = new MetricsOptions();
configureFromSystemProperties(metricsOptions, METRICS_OPTIONS_PROP_PREFIX);
options = new VertxOptions().setMetricsOptions(metricsOptions);
configureFromSystemProperties(options, VERTX_OPTIONS_PROP_PREFIX);
if (clustered) {
log.info("Starting clustering...");
Expand Down
78 changes: 12 additions & 66 deletions src/main/java/io/vertx/core/VertxOptions.java
Expand Up @@ -18,6 +18,7 @@

import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonObject;
import io.vertx.core.metrics.MetricsOptions;
import io.vertx.core.spi.cluster.ClusterManager;

/**
Expand Down Expand Up @@ -98,13 +99,6 @@ public class VertxOptions {
*/
public static final boolean DEFAULT_METRICS_ENABLED = false;

/**
* The default value of JMX enabled = false
*/
public static final boolean DEFAULT_JMX_ENABLED = false;



private int eventLoopPoolSize = DEFAULT_EVENT_LOOP_POOL_SIZE;
private int workerPoolSize = DEFAULT_WORKER_POOL_SIZE;
private int internalBlockingPoolSize = DEFAULT_INTERNAL_BLOCKING_POOL_SIZE;
Expand All @@ -120,9 +114,7 @@ public class VertxOptions {
private boolean haEnabled = DEFAULT_HA_ENABLED;
private int quorumSize = DEFAULT_QUORUM_SIZE;
private String haGroup;
private boolean metricsEnabled = DEFAULT_METRICS_ENABLED;
private boolean jmxEnabled = DEFAULT_JMX_ENABLED;
private String jmxDomain;
private MetricsOptions metrics;

/**
* Default constructor
Expand Down Expand Up @@ -151,9 +143,7 @@ public VertxOptions(VertxOptions other) {
this.haEnabled = other.isHAEnabled();
this.quorumSize = other.getQuorumSize();
this.haGroup = other.getHAGroup();
this.metricsEnabled = other.isMetricsEnabled();
this.jmxEnabled = other.isJmxEnabled();
this.jmxDomain = other.getJmxDomain();
this.metrics = other.getMetricsOptions() != null ? new MetricsOptions(other.getMetricsOptions()) : null;
}

/**
Expand All @@ -176,9 +166,8 @@ public VertxOptions(JsonObject json) {
this.haEnabled = json.getBoolean("haEnabled", false);
this.quorumSize = json.getInteger("quorumSize", DEFAULT_QUORUM_SIZE);
this.haGroup = json.getString("haGroup", null);
this.metricsEnabled = json.getBoolean("metricsEnabled", DEFAULT_METRICS_ENABLED);
this.jmxEnabled = json.getBoolean("jmxEnabled", DEFAULT_JMX_ENABLED);
this.jmxDomain = json.getString("jmxDomain");
JsonObject metricsJson = json.getJsonObject("metricsOptions");
this.metrics = metricsJson != null ? new MetricsOptions(metricsJson) : null;
}

/**
Expand Down Expand Up @@ -539,63 +528,20 @@ public VertxOptions setHAGroup(String haGroup) {
}

/**
* Will metrics be enabled on the Vert.x instance?
*
* @return true if enabled, false if not.
*/
public boolean isMetricsEnabled() {
return metricsEnabled;
}

/**
* Set whether metrics will be enabled on the Vert.x instance.
*
* @param enable true if metrics enabled, or false if not.
* @return a reference to this, so the API can be used fluently
*/
public VertxOptions setMetricsEnabled(boolean enable) {
this.metricsEnabled = enable;
return this;
}

/**
* Will JMX be enabled on the Vert.x instance?
*
* @return true if enabled, false if not.
*/
public boolean isJmxEnabled() {
return jmxEnabled;
}

/**
* Set whether JMX will be enabled on the Vert.x instance.
*
* @param jmxEnabled true if JMX enabled, or false if not.
* @return a reference to this, so the API can be used fluently
*/
public VertxOptions setJmxEnabled(boolean jmxEnabled) {
this.jmxEnabled = jmxEnabled;
if (jmxEnabled) metricsEnabled = true;
return this;
}

/**
* Get the JMX domain to use when JMX metrics are enabled.
*
* @return the JMX domain
* @return the metrics options
*/
public String getJmxDomain() {
return jmxDomain;
public MetricsOptions getMetricsOptions() {
return metrics;
}

/**
* Set the JMX domain to use when JMX metrics are enabled.
* Set the metrics options
*
* @param jmxDomain the JMX domain
* @param metrics the options
* @return a reference to this, so the API can be used fluently
*/
public VertxOptions setJmxDomain(String jmxDomain) {
this.jmxDomain = jmxDomain;
public VertxOptions setMetricsOptions(MetricsOptions metrics) {
this.metrics = metrics;
return this;
}

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/io/vertx/core/impl/VertxImpl.java
Expand Up @@ -48,6 +48,7 @@
import io.vertx.core.json.JsonObject;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.impl.LoggerFactory;
import io.vertx.core.metrics.MetricsOptions;
import io.vertx.core.metrics.impl.DummyVertxMetrics;
import io.vertx.core.metrics.spi.VertxMetrics;
import io.vertx.core.net.NetClient;
Expand Down Expand Up @@ -356,18 +357,16 @@ public DnsClient createDnsClient(int port, String host) {
}

private VertxMetrics initialiseMetrics(VertxOptions options) {
if (options.isMetricsEnabled()) {
if (options.getMetricsOptions() != null && options.getMetricsOptions().isEnabled()) {
ServiceLoader<VertxMetricsFactory> factories = ServiceLoader.load(VertxMetricsFactory.class);
if (factories.iterator().hasNext()) {
VertxMetricsFactory factory = factories.iterator().next();
return factory.metrics(this, options);
} else {
log.warn("Metrics has been set to enabled but no VertxMetricsFactory found on classpath");
return new DummyVertxMetrics();
}
} else {
return new DummyVertxMetrics();
}
return new DummyVertxMetrics();
}

private ClusterManager getClusterManager(VertxOptions options) {
Expand Down
135 changes: 135 additions & 0 deletions src/main/java/io/vertx/core/metrics/MetricsOptions.java
@@ -0,0 +1,135 @@
/*
* Copyright (c) 2011-2013 The original author or authors
* ------------------------------------------------------
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
*
* The Eclipse Public License is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* The Apache License v2.0 is available at
* http://www.opensource.org/licenses/apache2.0.php
*
* You may elect to redistribute this code under either of these licenses.
*/

package io.vertx.core.metrics;

import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonObject;

/**
* Vert.x metrics configuration.
*
* @author <a href="mailto:julien@julienviet.com">Julien Viet</a>
*/
@DataObject
public class MetricsOptions {

/**
* The default value of metrics enabled false
*/
public static final boolean DEFAULT_METRICS_ENABLED = false;

/**
* The default value of JMX enabled = false
*/
public static final boolean DEFAULT_JMX_ENABLED = false;

private boolean enabled;
private boolean jmxEnabled;
private String jmxDomain;

/**
* Default constructor
*/
public MetricsOptions() {
enabled = DEFAULT_METRICS_ENABLED;
jmxEnabled = DEFAULT_JMX_ENABLED;
}

/**
* Copy constructor
*
* @param other The other {@link MetricsOptions} to copy when creating this
*/
public MetricsOptions(MetricsOptions other) {
this.enabled = other.isEnabled();
this.jmxEnabled = other.isJmxEnabled();
this.jmxDomain = other.getJmxDomain();
}

/**
* Create an instance from a {@link io.vertx.core.json.JsonObject}
*
* @param json the JsonObject to create it from
*/
public MetricsOptions(JsonObject json) {
this.enabled = json.getBoolean("enabled", DEFAULT_METRICS_ENABLED);
this.jmxEnabled = json.getBoolean("jmxEnabled", DEFAULT_JMX_ENABLED);
this.jmxDomain = json.getString("jmxDomain");
}

/**
* Will metrics be enabled on the Vert.x instance?
*
* @return true if enabled, false if not.
*/
public boolean isEnabled() {
return enabled;
}

/**
* Set whether metrics will be enabled on the Vert.x instance.
*
* @param enable true if metrics enabled, or false if not.
* @return a reference to this, so the API can be used fluently
*/
public MetricsOptions setEnabled(boolean enable) {
this.enabled = enable;
return this;
}

/**
* Will JMX be enabled on the Vert.x instance?
*
* @return true if enabled, false if not.
*/
public boolean isJmxEnabled() {
return jmxEnabled;
}

/**
* Set whether JMX will be enabled on the Vert.x instance.
*
* @param jmxEnabled true if JMX enabled, or false if not.
* @return a reference to this, so the API can be used fluently
*/
public MetricsOptions setJmxEnabled(boolean jmxEnabled) {
this.jmxEnabled = jmxEnabled;
if (jmxEnabled) enabled = true;
return this;
}

/**
* Get the JMX domain to use when JMX metrics are enabled.
*
* @return the JMX domain
*/
public String getJmxDomain() {
return jmxDomain;
}

/**
* Set the JMX domain to use when JMX metrics are enabled.
*
* @param jmxDomain the JMX domain
* @return a reference to this, so the API can be used fluently
*/
public MetricsOptions setJmxDomain(String jmxDomain) {
this.jmxDomain = jmxDomain;
return this;
}

}
11 changes: 11 additions & 0 deletions src/main/java/io/vertx/core/spi/VertxMetricsFactory.java
Expand Up @@ -18,11 +18,22 @@

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.metrics.MetricsOptions;
import io.vertx.core.metrics.spi.VertxMetrics;

/**
* A factory for the plugable metrics SPI.
*
* @author <a href="mailto:nscavell@redhat.com">Nick Scavelli</a>
*/
public interface VertxMetricsFactory {

/**
* Create a new {@link io.vertx.core.metrics.spi.VertxMetrics} object.
*
* @param vertx the vertx instance
* @param options the metrics configuration option
* @return the metrics implementation
*/
VertxMetrics metrics(Vertx vertx, VertxOptions options);
}
3 changes: 2 additions & 1 deletion src/test/java/io/vertx/test/core/DummyMetricsTest.java
Expand Up @@ -23,6 +23,7 @@
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.metrics.MetricsOptions;
import io.vertx.core.net.NetClient;
import io.vertx.core.net.NetClientOptions;
import io.vertx.core.net.NetServer;
Expand All @@ -36,7 +37,7 @@ public class DummyMetricsTest extends VertxTestBase {

@Override
protected VertxOptions getOptions() {
return new VertxOptions().setMetricsEnabled(false); // Just to be explicit
return new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(false)); // Just to be explicit
}

@Test
Expand Down

0 comments on commit 33d8e41

Please sign in to comment.