Convert MetricsConfigiguration to use a builder instead of being mutable. (#1572)

Signed-off-by: Adrian Sutton <adrian.sutton@consensys.net>
pull/2/head
Adrian Sutton 6 years ago committed by GitHub
parent 164177e645
commit cee72c8590
  1. 2
      acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/configuration/PantheonFactoryConfigurationBuilder.java
  2. 8
      ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java
  3. 232
      metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/MetricsConfiguration.java
  4. 16
      metrics/core/src/test/java/tech/pegasys/pantheon/metrics/prometheus/MetricsHttpServiceTest.java
  5. 23
      metrics/core/src/test/java/tech/pegasys/pantheon/metrics/prometheus/PrometheusMetricsSystemTest.java
  6. 25
      pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java
  7. 5
      pantheon/src/test/java/tech/pegasys/pantheon/RunnerTest.java
  8. 10
      pantheon/src/test/java/tech/pegasys/pantheon/cli/PantheonCommandTest.java

@ -39,7 +39,7 @@ public class PantheonFactoryConfigurationBuilder {
private PrivacyParameters privacyParameters = PrivacyParameters.DEFAULT; private PrivacyParameters privacyParameters = PrivacyParameters.DEFAULT;
private JsonRpcConfiguration jsonRpcConfiguration = JsonRpcConfiguration.createDefault(); private JsonRpcConfiguration jsonRpcConfiguration = JsonRpcConfiguration.createDefault();
private WebSocketConfiguration webSocketConfiguration = WebSocketConfiguration.createDefault(); private WebSocketConfiguration webSocketConfiguration = WebSocketConfiguration.createDefault();
private MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); private MetricsConfiguration metricsConfiguration = MetricsConfiguration.builder().build();
private Optional<PermissioningConfiguration> permissioningConfiguration = Optional.empty(); private Optional<PermissioningConfiguration> permissioningConfiguration = Optional.empty();
private Optional<String> keyFilePath = Optional.empty(); private Optional<String> keyFilePath = Optional.empty();
private boolean devMode = true; private boolean devMode = true;

@ -250,9 +250,7 @@ public class JsonRpcHttpServiceRpcApisTest {
} }
private MetricsConfiguration createMetricsConfiguration() { private MetricsConfiguration createMetricsConfiguration() {
final MetricsConfiguration config = MetricsConfiguration.createDefault(); return MetricsConfiguration.builder().enabled(true).build();
config.setEnabled(true);
return config;
} }
private JsonRpcHttpService createJsonRpcHttpService( private JsonRpcHttpService createJsonRpcHttpService(
@ -375,7 +373,7 @@ public class JsonRpcHttpServiceRpcApisTest {
JsonRpcConfiguration jsonRpcConfiguration = JsonRpcConfiguration.createDefault(); JsonRpcConfiguration jsonRpcConfiguration = JsonRpcConfiguration.createDefault();
WebSocketConfiguration webSocketConfiguration = WebSocketConfiguration.createDefault(); WebSocketConfiguration webSocketConfiguration = WebSocketConfiguration.createDefault();
P2PNetwork p2pNetwork = mock(P2PNetwork.class); P2PNetwork p2pNetwork = mock(P2PNetwork.class);
MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); MetricsConfiguration metricsConfiguration = MetricsConfiguration.builder().build();
if (enabledNetServices[netServices.indexOf("jsonrpc")]) { if (enabledNetServices[netServices.indexOf("jsonrpc")]) {
jsonRpcConfiguration = createJsonRpcConfiguration(); jsonRpcConfiguration = createJsonRpcConfiguration();
@ -403,7 +401,7 @@ public class JsonRpcHttpServiceRpcApisTest {
JsonRpcConfiguration.createDefault(), JsonRpcConfiguration.createDefault(),
WebSocketConfiguration.createDefault(), WebSocketConfiguration.createDefault(),
mock(P2PNetwork.class), mock(P2PNetwork.class),
MetricsConfiguration.createDefault()); MetricsConfiguration.builder().build());
final RequestBody body = createNetServicesRequestBody(); final RequestBody body = createNetServicesRequestBody();
try (final Response resp = client.newCall(buildRequest(body)).execute()) { try (final Response resp = client.newCall(buildRequest(body)).execute()) {

@ -23,6 +23,8 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
import java.util.Set; import java.util.Set;
import com.google.common.base.MoreObjects;
public class MetricsConfiguration { public class MetricsConfiguration {
private static final String DEFAULT_METRICS_HOST = "127.0.0.1"; private static final String DEFAULT_METRICS_HOST = "127.0.0.1";
public static final int DEFAULT_METRICS_PORT = 9545; public static final int DEFAULT_METRICS_PORT = 9545;
@ -30,155 +32,116 @@ public class MetricsConfiguration {
private static final String DEFAULT_METRICS_PUSH_HOST = "127.0.0.1"; private static final String DEFAULT_METRICS_PUSH_HOST = "127.0.0.1";
public static final int DEFAULT_METRICS_PUSH_PORT = 9001; public static final int DEFAULT_METRICS_PUSH_PORT = 9001;
private boolean enabled; private final boolean enabled;
private int port; private final int port;
private String host; private final String host;
private Set<MetricCategory> metricCategories; private final Set<MetricCategory> metricCategories;
private boolean pushEnabled; private final boolean pushEnabled;
private int pushPort; private final int pushPort;
private String pushHost; private final String pushHost;
private int pushInterval; private final int pushInterval;
private String prometheusJob; private final String prometheusJob;
private List<String> hostsWhitelist = Arrays.asList("localhost", "127.0.0.1"); private final List<String> hostsWhitelist;
public static MetricsConfiguration createDefault() { public static Builder builder() {
final MetricsConfiguration metricsConfiguration = new MetricsConfiguration(); return new Builder();
metricsConfiguration.setEnabled(false); }
metricsConfiguration.setPort(DEFAULT_METRICS_PORT);
metricsConfiguration.setHost(DEFAULT_METRICS_HOST); private MetricsConfiguration(
metricsConfiguration.setMetricCategories(DEFAULT_METRIC_CATEGORIES); final boolean enabled,
metricsConfiguration.setPushEnabled(false); final int port,
metricsConfiguration.setPushPort(DEFAULT_METRICS_PUSH_PORT); final String host,
metricsConfiguration.setPushHost(DEFAULT_METRICS_PUSH_HOST); final Set<MetricCategory> metricCategories,
metricsConfiguration.setPushInterval(15); final boolean pushEnabled,
metricsConfiguration.setPrometheusJob("pantheon-client"); final int pushPort,
final String pushHost,
return metricsConfiguration; final int pushInterval,
} final String prometheusJob,
final List<String> hostsWhitelist) {
private MetricsConfiguration() {} this.enabled = enabled;
this.port = port;
this.host = host;
this.metricCategories = metricCategories;
this.pushEnabled = pushEnabled;
this.pushPort = pushPort;
this.pushHost = pushHost;
this.pushInterval = pushInterval;
this.prometheusJob = prometheusJob;
this.hostsWhitelist = hostsWhitelist;
}
public boolean isEnabled() { public boolean isEnabled() {
return enabled; return enabled;
} }
public void setEnabled(final boolean enabled) {
this.enabled = enabled;
}
public int getPort() { public int getPort() {
return port; return port;
} }
public void setPort(final int port) {
this.port = port;
}
public String getHost() { public String getHost() {
return host; return host;
} }
public void setHost(final String host) {
this.host = host;
}
public Set<MetricCategory> getMetricCategories() { public Set<MetricCategory> getMetricCategories() {
return metricCategories; return metricCategories;
} }
public void setMetricCategories(final Set<MetricCategory> metricCategories) {
this.metricCategories = metricCategories;
}
public int getPushPort() { public int getPushPort() {
return pushPort; return pushPort;
} }
public void setPushPort(final int pushPort) {
this.pushPort = pushPort;
}
public String getPushHost() { public String getPushHost() {
return pushHost; return pushHost;
} }
public void setPushHost(final String pushHost) {
this.pushHost = pushHost;
}
public boolean isPushEnabled() { public boolean isPushEnabled() {
return pushEnabled; return pushEnabled;
} }
public void setPushEnabled(final boolean pushEnabled) {
this.pushEnabled = pushEnabled;
}
public int getPushInterval() { public int getPushInterval() {
return pushInterval; return pushInterval;
} }
public void setPushInterval(final int pushInterval) {
this.pushInterval = pushInterval;
}
public String getPrometheusJob() { public String getPrometheusJob() {
return prometheusJob; return prometheusJob;
} }
public void setPrometheusJob(final String prometheusJob) {
this.prometheusJob = prometheusJob;
}
Collection<String> getHostsWhitelist() { Collection<String> getHostsWhitelist() {
return Collections.unmodifiableCollection(this.hostsWhitelist); return Collections.unmodifiableCollection(this.hostsWhitelist);
} }
public void setHostsWhitelist(final List<String> hostsWhitelist) {
this.hostsWhitelist = hostsWhitelist;
}
@Override @Override
public String toString() { public String toString() {
return "MetricsConfiguration{" return MoreObjects.toStringHelper(this)
+ "enabled=" .add("enabled", enabled)
+ enabled .add("port", port)
+ ", port=" .add("host", host)
+ port .add("metricCategories", metricCategories)
+ ", host='" .add("pushEnabled", pushEnabled)
+ host .add("pushPort", pushPort)
+ '\'' .add("pushHost", pushHost)
+ ", categories=" .add("pushInterval", pushInterval)
+ metricCategories.toString() .add("prometheusJob", prometheusJob)
+ ", pushEnabled=" .add("hostsWhitelist", hostsWhitelist)
+ pushEnabled .toString();
+ ", pushPort="
+ pushPort
+ ", pushHost='"
+ pushHost
+ '\''
+ ", pushInterval="
+ pushInterval
+ ", prometheusJob='"
+ prometheusJob
+ '\''
+ ", hostsWhitelist="
+ hostsWhitelist
+ '}';
} }
@Override @Override
public boolean equals(final Object o) { public boolean equals(final Object o) {
if (this == o) return true; if (this == o) {
if (o == null || getClass() != o.getClass()) return false; return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final MetricsConfiguration that = (MetricsConfiguration) o; final MetricsConfiguration that = (MetricsConfiguration) o;
return enabled == that.enabled return enabled == that.enabled
&& port == that.port && port == that.port
&& Objects.equals(metricCategories, that.metricCategories)
&& pushEnabled == that.pushEnabled && pushEnabled == that.pushEnabled
&& pushPort == that.pushPort && pushPort == that.pushPort
&& pushInterval == that.pushInterval && pushInterval == that.pushInterval
&& Objects.equals(host, that.host) && Objects.equals(host, that.host)
&& Objects.equals(metricCategories, that.metricCategories)
&& Objects.equals(pushHost, that.pushHost) && Objects.equals(pushHost, that.pushHost)
&& Objects.equals(prometheusJob, that.prometheusJob) && Objects.equals(prometheusJob, that.prometheusJob)
&& Objects.equals(hostsWhitelist, that.hostsWhitelist); && Objects.equals(hostsWhitelist, that.hostsWhitelist);
@ -198,4 +161,83 @@ public class MetricsConfiguration {
prometheusJob, prometheusJob,
hostsWhitelist); hostsWhitelist);
} }
public static class Builder {
private boolean enabled = false;
private int port = DEFAULT_METRICS_PORT;
private String host = DEFAULT_METRICS_HOST;
private Set<MetricCategory> metricCategories = DEFAULT_METRIC_CATEGORIES;
private boolean pushEnabled = false;
private int pushPort = DEFAULT_METRICS_PUSH_PORT;
private String pushHost = DEFAULT_METRICS_PUSH_HOST;
private int pushInterval = 15;
private String prometheusJob = "pantheon-client";
private List<String> hostsWhitelist = Arrays.asList("localhost", "127.0.0.1");
private Builder() {}
public Builder enabled(final boolean enabled) {
this.enabled = enabled;
return this;
}
public Builder port(final int port) {
this.port = port;
return this;
}
public Builder host(final String host) {
this.host = host;
return this;
}
public Builder metricCategories(final Set<MetricCategory> metricCategories) {
this.metricCategories = metricCategories;
return this;
}
public Builder pushEnabled(final boolean pushEnabled) {
this.pushEnabled = pushEnabled;
return this;
}
public Builder pushPort(final int pushPort) {
this.pushPort = pushPort;
return this;
}
public Builder pushHost(final String pushHost) {
this.pushHost = pushHost;
return this;
}
public Builder pushInterval(final int pushInterval) {
this.pushInterval = pushInterval;
return this;
}
public Builder prometheusJob(final String prometheusJob) {
this.prometheusJob = prometheusJob;
return this;
}
public Builder hostsWhitelist(final List<String> hostsWhitelist) {
this.hostsWhitelist = hostsWhitelist;
return this;
}
public MetricsConfiguration build() {
return new MetricsConfiguration(
enabled,
port,
host,
metricCategories,
pushEnabled,
pushPort,
pushHost,
pushInterval,
prometheusJob,
hostsWhitelist);
}
}
} }

@ -12,10 +12,10 @@
*/ */
package tech.pegasys.pantheon.metrics.prometheus; package tech.pegasys.pantheon.metrics.prometheus;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Properties; import java.util.Properties;
import io.vertx.core.Vertx; import io.vertx.core.Vertx;
@ -54,17 +54,16 @@ public class MetricsHttpServiceTest {
private static MetricsHttpService createMetricsHttpService() { private static MetricsHttpService createMetricsHttpService() {
final MetricsConfiguration metricsConfiguration = createMetricsConfig(); final MetricsConfiguration metricsConfiguration = createMetricsConfig();
metricsConfiguration.setEnabled(true);
return new MetricsHttpService( return new MetricsHttpService(
vertx, metricsConfiguration, PrometheusMetricsSystem.init(metricsConfiguration)); vertx, metricsConfiguration, PrometheusMetricsSystem.init(metricsConfiguration));
} }
private static MetricsConfiguration createMetricsConfig() { private static MetricsConfiguration createMetricsConfig() {
final MetricsConfiguration config = MetricsConfiguration.createDefault(); return createMetricsConfigBuilder().build();
config.setEnabled(true); }
config.setPort(0);
config.setHostsWhitelist(Collections.singletonList("*")); private static MetricsConfiguration.Builder createMetricsConfigBuilder() {
return config; return MetricsConfiguration.builder().enabled(true).port(0).hostsWhitelist(singletonList("*"));
} }
/** Tears down the HTTP server. */ /** Tears down the HTTP server. */
@ -114,8 +113,7 @@ public class MetricsHttpServiceTest {
@Test @Test
public void getSocketAddressWhenBindingToAllInterfaces() { public void getSocketAddressWhenBindingToAllInterfaces() {
final MetricsConfiguration config = createMetricsConfig(); final MetricsConfiguration config = createMetricsConfigBuilder().host("0.0.0.0").build();
config.setHost("0.0.0.0");
final MetricsHttpService service = createMetricsHttpService(config); final MetricsHttpService service = createMetricsHttpService(config);
service.start().join(); service.start().join();

@ -173,9 +173,11 @@ public class PrometheusMetricsSystemTest {
@Test @Test
public void shouldOnlyObserveEnabledMetrics() { public void shouldOnlyObserveEnabledMetrics() {
final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); final MetricsConfiguration metricsConfiguration =
metricsConfiguration.setMetricCategories(EnumSet.of(MetricCategory.RPC)); MetricsConfiguration.builder()
metricsConfiguration.setEnabled(true); .metricCategories(EnumSet.of(MetricCategory.RPC))
.enabled(true)
.build();
final MetricsSystem localMetricSystem = PrometheusMetricsSystem.init(metricsConfiguration); final MetricsSystem localMetricSystem = PrometheusMetricsSystem.init(metricsConfiguration);
// do a category we are not watching // do a category we are not watching
@ -198,9 +200,8 @@ public class PrometheusMetricsSystemTest {
@Test @Test
public void returnsNoOpMetricsWhenAllDisabled() { public void returnsNoOpMetricsWhenAllDisabled() {
final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); final MetricsConfiguration metricsConfiguration =
metricsConfiguration.setEnabled(false); MetricsConfiguration.builder().enabled(false).pushEnabled(false).build();
metricsConfiguration.setPushEnabled(false);
final MetricsSystem localMetricSystem = PrometheusMetricsSystem.init(metricsConfiguration); final MetricsSystem localMetricSystem = PrometheusMetricsSystem.init(metricsConfiguration);
assertThat(localMetricSystem).isInstanceOf(NoOpMetricsSystem.class); assertThat(localMetricSystem).isInstanceOf(NoOpMetricsSystem.class);
@ -208,9 +209,8 @@ public class PrometheusMetricsSystemTest {
@Test @Test
public void returnsPrometheusMetricsWhenEnabled() { public void returnsPrometheusMetricsWhenEnabled() {
final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); final MetricsConfiguration metricsConfiguration =
metricsConfiguration.setEnabled(true); MetricsConfiguration.builder().enabled(true).pushEnabled(false).build();
metricsConfiguration.setPushEnabled(false);
final MetricsSystem localMetricSystem = PrometheusMetricsSystem.init(metricsConfiguration); final MetricsSystem localMetricSystem = PrometheusMetricsSystem.init(metricsConfiguration);
assertThat(localMetricSystem).isInstanceOf(PrometheusMetricsSystem.class); assertThat(localMetricSystem).isInstanceOf(PrometheusMetricsSystem.class);
@ -218,9 +218,8 @@ public class PrometheusMetricsSystemTest {
@Test @Test
public void returnsNoOpMetricsWhenPushEnabled() { public void returnsNoOpMetricsWhenPushEnabled() {
final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); final MetricsConfiguration metricsConfiguration =
metricsConfiguration.setEnabled(false); MetricsConfiguration.builder().enabled(false).pushEnabled(true).build();
metricsConfiguration.setPushEnabled(true);
final MetricsSystem localMetricSystem = PrometheusMetricsSystem.init(metricsConfiguration); final MetricsSystem localMetricSystem = PrometheusMetricsSystem.init(metricsConfiguration);
assertThat(localMetricSystem).isInstanceOf(PrometheusMetricsSystem.class); assertThat(localMetricSystem).isInstanceOf(PrometheusMetricsSystem.class);

@ -27,7 +27,6 @@ import static tech.pegasys.pantheon.ethereum.jsonrpc.websocket.WebSocketConfigur
import static tech.pegasys.pantheon.metrics.MetricCategory.DEFAULT_METRIC_CATEGORIES; import static tech.pegasys.pantheon.metrics.MetricCategory.DEFAULT_METRIC_CATEGORIES;
import static tech.pegasys.pantheon.metrics.prometheus.MetricsConfiguration.DEFAULT_METRICS_PORT; import static tech.pegasys.pantheon.metrics.prometheus.MetricsConfiguration.DEFAULT_METRICS_PORT;
import static tech.pegasys.pantheon.metrics.prometheus.MetricsConfiguration.DEFAULT_METRICS_PUSH_PORT; import static tech.pegasys.pantheon.metrics.prometheus.MetricsConfiguration.DEFAULT_METRICS_PUSH_PORT;
import static tech.pegasys.pantheon.metrics.prometheus.MetricsConfiguration.createDefault;
import tech.pegasys.pantheon.Runner; import tech.pegasys.pantheon.Runner;
import tech.pegasys.pantheon.RunnerBuilder; import tech.pegasys.pantheon.RunnerBuilder;
@ -924,18 +923,18 @@ public class PantheonCommand implements DefaultCommandValues, Runnable {
"--metrics-push-interval", "--metrics-push-interval",
"--metrics-push-prometheus-job")); "--metrics-push-prometheus-job"));
final MetricsConfiguration metricsConfiguration = createDefault(); return MetricsConfiguration.builder()
metricsConfiguration.setEnabled(isMetricsEnabled); .enabled(isMetricsEnabled)
metricsConfiguration.setHost(metricsHost); .host(metricsHost)
metricsConfiguration.setPort(metricsPort); .port(metricsPort)
metricsConfiguration.setMetricCategories(metricCategories); .metricCategories(metricCategories)
metricsConfiguration.setPushEnabled(isMetricsPushEnabled); .pushEnabled(isMetricsPushEnabled)
metricsConfiguration.setPushHost(metricsPushHost); .pushHost(metricsPushHost)
metricsConfiguration.setPushPort(metricsPushPort); .pushPort(metricsPushPort)
metricsConfiguration.setPushInterval(metricsPushInterval); .pushInterval(metricsPushInterval)
metricsConfiguration.setPrometheusJob(metricsPrometheusJob); .hostsWhitelist(hostsWhitelist)
metricsConfiguration.setHostsWhitelist(hostsWhitelist); .prometheusJob(metricsPrometheusJob)
return metricsConfiguration; .build();
} }
private Optional<PermissioningConfiguration> permissioningConfiguration() throws Exception { private Optional<PermissioningConfiguration> permissioningConfiguration() throws Exception {

@ -360,10 +360,7 @@ public final class RunnerTest {
} }
private MetricsConfiguration metricsConfiguration() { private MetricsConfiguration metricsConfiguration() {
final MetricsConfiguration configuration = MetricsConfiguration.createDefault(); return MetricsConfiguration.builder().enabled(false).port(0).build();
configuration.setPort(0);
configuration.setEnabled(false);
return configuration;
} }
private static void setupState( private static void setupState(

@ -112,7 +112,7 @@ public class PantheonCommandTest extends CommandTestAbstract {
defaultWebSocketConfiguration = WebSocketConfiguration.createDefault(); defaultWebSocketConfiguration = WebSocketConfiguration.createDefault();
defaultMetricsConfiguration = MetricsConfiguration.createDefault(); defaultMetricsConfiguration = MetricsConfiguration.builder().build();
} }
@Test @Test
@ -297,10 +297,8 @@ public class PantheonCommandTest extends CommandTestAbstract {
webSocketConfiguration.setPort(9101); webSocketConfiguration.setPort(9101);
webSocketConfiguration.setRpcApis(expectedApis); webSocketConfiguration.setRpcApis(expectedApis);
final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); final MetricsConfiguration metricsConfiguration =
metricsConfiguration.setEnabled(false); MetricsConfiguration.builder().enabled(false).host("8.6.7.5").port(309).build();
metricsConfiguration.setHost("8.6.7.5");
metricsConfiguration.setPort(309);
parseCommand("--config-file", toml.toString()); parseCommand("--config-file", toml.toString());
@ -701,7 +699,7 @@ public class PantheonCommandTest extends CommandTestAbstract {
final WebSocketConfiguration webSocketConfiguration = WebSocketConfiguration.createDefault(); final WebSocketConfiguration webSocketConfiguration = WebSocketConfiguration.createDefault();
final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); final MetricsConfiguration metricsConfiguration = MetricsConfiguration.builder().build();
verify(mockRunnerBuilder).discovery(eq(true)); verify(mockRunnerBuilder).discovery(eq(true));
verify(mockRunnerBuilder) verify(mockRunnerBuilder)

Loading…
Cancel
Save