diff --git a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/configuration/PantheonFactoryConfigurationBuilder.java b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/configuration/PantheonFactoryConfigurationBuilder.java index 309573a486..a673c26b30 100644 --- a/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/configuration/PantheonFactoryConfigurationBuilder.java +++ b/acceptance-tests/src/test/java/tech/pegasys/pantheon/tests/acceptance/dsl/node/configuration/PantheonFactoryConfigurationBuilder.java @@ -39,7 +39,7 @@ public class PantheonFactoryConfigurationBuilder { private PrivacyParameters privacyParameters = PrivacyParameters.DEFAULT; private JsonRpcConfiguration jsonRpcConfiguration = JsonRpcConfiguration.createDefault(); private WebSocketConfiguration webSocketConfiguration = WebSocketConfiguration.createDefault(); - private MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); + private MetricsConfiguration metricsConfiguration = MetricsConfiguration.builder().build(); private Optional permissioningConfiguration = Optional.empty(); private Optional keyFilePath = Optional.empty(); private boolean devMode = true; diff --git a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java index 13d531a299..6853e1e3e8 100644 --- a/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java +++ b/ethereum/jsonrpc/src/test/java/tech/pegasys/pantheon/ethereum/jsonrpc/JsonRpcHttpServiceRpcApisTest.java @@ -250,9 +250,7 @@ public class JsonRpcHttpServiceRpcApisTest { } private MetricsConfiguration createMetricsConfiguration() { - final MetricsConfiguration config = MetricsConfiguration.createDefault(); - config.setEnabled(true); - return config; + return MetricsConfiguration.builder().enabled(true).build(); } private JsonRpcHttpService createJsonRpcHttpService( @@ -375,7 +373,7 @@ public class JsonRpcHttpServiceRpcApisTest { JsonRpcConfiguration jsonRpcConfiguration = JsonRpcConfiguration.createDefault(); WebSocketConfiguration webSocketConfiguration = WebSocketConfiguration.createDefault(); P2PNetwork p2pNetwork = mock(P2PNetwork.class); - MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); + MetricsConfiguration metricsConfiguration = MetricsConfiguration.builder().build(); if (enabledNetServices[netServices.indexOf("jsonrpc")]) { jsonRpcConfiguration = createJsonRpcConfiguration(); @@ -403,7 +401,7 @@ public class JsonRpcHttpServiceRpcApisTest { JsonRpcConfiguration.createDefault(), WebSocketConfiguration.createDefault(), mock(P2PNetwork.class), - MetricsConfiguration.createDefault()); + MetricsConfiguration.builder().build()); final RequestBody body = createNetServicesRequestBody(); try (final Response resp = client.newCall(buildRequest(body)).execute()) { diff --git a/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/MetricsConfiguration.java b/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/MetricsConfiguration.java index 7e9b4a0dc5..cfee72626f 100644 --- a/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/MetricsConfiguration.java +++ b/metrics/core/src/main/java/tech/pegasys/pantheon/metrics/prometheus/MetricsConfiguration.java @@ -23,6 +23,8 @@ import java.util.List; import java.util.Objects; import java.util.Set; +import com.google.common.base.MoreObjects; + public class MetricsConfiguration { private static final String DEFAULT_METRICS_HOST = "127.0.0.1"; 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"; public static final int DEFAULT_METRICS_PUSH_PORT = 9001; - private boolean enabled; - private int port; - private String host; - private Set metricCategories; - private boolean pushEnabled; - private int pushPort; - private String pushHost; - private int pushInterval; - private String prometheusJob; - private List hostsWhitelist = Arrays.asList("localhost", "127.0.0.1"); - - public static MetricsConfiguration createDefault() { - final MetricsConfiguration metricsConfiguration = new MetricsConfiguration(); - metricsConfiguration.setEnabled(false); - metricsConfiguration.setPort(DEFAULT_METRICS_PORT); - metricsConfiguration.setHost(DEFAULT_METRICS_HOST); - metricsConfiguration.setMetricCategories(DEFAULT_METRIC_CATEGORIES); - metricsConfiguration.setPushEnabled(false); - metricsConfiguration.setPushPort(DEFAULT_METRICS_PUSH_PORT); - metricsConfiguration.setPushHost(DEFAULT_METRICS_PUSH_HOST); - metricsConfiguration.setPushInterval(15); - metricsConfiguration.setPrometheusJob("pantheon-client"); - - return metricsConfiguration; - } - - private MetricsConfiguration() {} + private final boolean enabled; + private final int port; + private final String host; + private final Set metricCategories; + private final boolean pushEnabled; + private final int pushPort; + private final String pushHost; + private final int pushInterval; + private final String prometheusJob; + private final List hostsWhitelist; + + public static Builder builder() { + return new Builder(); + } + + private MetricsConfiguration( + final boolean enabled, + final int port, + final String host, + final Set metricCategories, + final boolean pushEnabled, + final int pushPort, + final String pushHost, + final int pushInterval, + final String prometheusJob, + final List hostsWhitelist) { + 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() { return enabled; } - public void setEnabled(final boolean enabled) { - this.enabled = enabled; - } - public int getPort() { return port; } - public void setPort(final int port) { - this.port = port; - } - public String getHost() { return host; } - public void setHost(final String host) { - this.host = host; - } - public Set getMetricCategories() { return metricCategories; } - public void setMetricCategories(final Set metricCategories) { - this.metricCategories = metricCategories; - } - public int getPushPort() { return pushPort; } - public void setPushPort(final int pushPort) { - this.pushPort = pushPort; - } - public String getPushHost() { return pushHost; } - public void setPushHost(final String pushHost) { - this.pushHost = pushHost; - } - public boolean isPushEnabled() { return pushEnabled; } - public void setPushEnabled(final boolean pushEnabled) { - this.pushEnabled = pushEnabled; - } - public int getPushInterval() { return pushInterval; } - public void setPushInterval(final int pushInterval) { - this.pushInterval = pushInterval; - } - public String getPrometheusJob() { return prometheusJob; } - public void setPrometheusJob(final String prometheusJob) { - this.prometheusJob = prometheusJob; - } - Collection getHostsWhitelist() { return Collections.unmodifiableCollection(this.hostsWhitelist); } - public void setHostsWhitelist(final List hostsWhitelist) { - this.hostsWhitelist = hostsWhitelist; - } - @Override public String toString() { - return "MetricsConfiguration{" - + "enabled=" - + enabled - + ", port=" - + port - + ", host='" - + host - + '\'' - + ", categories=" - + metricCategories.toString() - + ", pushEnabled=" - + pushEnabled - + ", pushPort=" - + pushPort - + ", pushHost='" - + pushHost - + '\'' - + ", pushInterval=" - + pushInterval - + ", prometheusJob='" - + prometheusJob - + '\'' - + ", hostsWhitelist=" - + hostsWhitelist - + '}'; + return MoreObjects.toStringHelper(this) + .add("enabled", enabled) + .add("port", port) + .add("host", host) + .add("metricCategories", metricCategories) + .add("pushEnabled", pushEnabled) + .add("pushPort", pushPort) + .add("pushHost", pushHost) + .add("pushInterval", pushInterval) + .add("prometheusJob", prometheusJob) + .add("hostsWhitelist", hostsWhitelist) + .toString(); } @Override public boolean equals(final Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } final MetricsConfiguration that = (MetricsConfiguration) o; return enabled == that.enabled && port == that.port - && Objects.equals(metricCategories, that.metricCategories) && pushEnabled == that.pushEnabled && pushPort == that.pushPort && pushInterval == that.pushInterval && Objects.equals(host, that.host) + && Objects.equals(metricCategories, that.metricCategories) && Objects.equals(pushHost, that.pushHost) && Objects.equals(prometheusJob, that.prometheusJob) && Objects.equals(hostsWhitelist, that.hostsWhitelist); @@ -198,4 +161,83 @@ public class MetricsConfiguration { prometheusJob, hostsWhitelist); } + + public static class Builder { + private boolean enabled = false; + private int port = DEFAULT_METRICS_PORT; + private String host = DEFAULT_METRICS_HOST; + private Set 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 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 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 hostsWhitelist) { + this.hostsWhitelist = hostsWhitelist; + return this; + } + + public MetricsConfiguration build() { + return new MetricsConfiguration( + enabled, + port, + host, + metricCategories, + pushEnabled, + pushPort, + pushHost, + pushInterval, + prometheusJob, + hostsWhitelist); + } + } } diff --git a/metrics/core/src/test/java/tech/pegasys/pantheon/metrics/prometheus/MetricsHttpServiceTest.java b/metrics/core/src/test/java/tech/pegasys/pantheon/metrics/prometheus/MetricsHttpServiceTest.java index 98f7fbdf4d..baf0b48185 100644 --- a/metrics/core/src/test/java/tech/pegasys/pantheon/metrics/prometheus/MetricsHttpServiceTest.java +++ b/metrics/core/src/test/java/tech/pegasys/pantheon/metrics/prometheus/MetricsHttpServiceTest.java @@ -12,10 +12,10 @@ */ package tech.pegasys.pantheon.metrics.prometheus; +import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; import java.net.InetSocketAddress; -import java.util.Collections; import java.util.Properties; import io.vertx.core.Vertx; @@ -54,17 +54,16 @@ public class MetricsHttpServiceTest { private static MetricsHttpService createMetricsHttpService() { final MetricsConfiguration metricsConfiguration = createMetricsConfig(); - metricsConfiguration.setEnabled(true); return new MetricsHttpService( vertx, metricsConfiguration, PrometheusMetricsSystem.init(metricsConfiguration)); } private static MetricsConfiguration createMetricsConfig() { - final MetricsConfiguration config = MetricsConfiguration.createDefault(); - config.setEnabled(true); - config.setPort(0); - config.setHostsWhitelist(Collections.singletonList("*")); - return config; + return createMetricsConfigBuilder().build(); + } + + private static MetricsConfiguration.Builder createMetricsConfigBuilder() { + return MetricsConfiguration.builder().enabled(true).port(0).hostsWhitelist(singletonList("*")); } /** Tears down the HTTP server. */ @@ -114,8 +113,7 @@ public class MetricsHttpServiceTest { @Test public void getSocketAddressWhenBindingToAllInterfaces() { - final MetricsConfiguration config = createMetricsConfig(); - config.setHost("0.0.0.0"); + final MetricsConfiguration config = createMetricsConfigBuilder().host("0.0.0.0").build(); final MetricsHttpService service = createMetricsHttpService(config); service.start().join(); diff --git a/metrics/core/src/test/java/tech/pegasys/pantheon/metrics/prometheus/PrometheusMetricsSystemTest.java b/metrics/core/src/test/java/tech/pegasys/pantheon/metrics/prometheus/PrometheusMetricsSystemTest.java index cf4f57c211..57e264f9bb 100644 --- a/metrics/core/src/test/java/tech/pegasys/pantheon/metrics/prometheus/PrometheusMetricsSystemTest.java +++ b/metrics/core/src/test/java/tech/pegasys/pantheon/metrics/prometheus/PrometheusMetricsSystemTest.java @@ -173,9 +173,11 @@ public class PrometheusMetricsSystemTest { @Test public void shouldOnlyObserveEnabledMetrics() { - final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); - metricsConfiguration.setMetricCategories(EnumSet.of(MetricCategory.RPC)); - metricsConfiguration.setEnabled(true); + final MetricsConfiguration metricsConfiguration = + MetricsConfiguration.builder() + .metricCategories(EnumSet.of(MetricCategory.RPC)) + .enabled(true) + .build(); final MetricsSystem localMetricSystem = PrometheusMetricsSystem.init(metricsConfiguration); // do a category we are not watching @@ -198,9 +200,8 @@ public class PrometheusMetricsSystemTest { @Test public void returnsNoOpMetricsWhenAllDisabled() { - final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); - metricsConfiguration.setEnabled(false); - metricsConfiguration.setPushEnabled(false); + final MetricsConfiguration metricsConfiguration = + MetricsConfiguration.builder().enabled(false).pushEnabled(false).build(); final MetricsSystem localMetricSystem = PrometheusMetricsSystem.init(metricsConfiguration); assertThat(localMetricSystem).isInstanceOf(NoOpMetricsSystem.class); @@ -208,9 +209,8 @@ public class PrometheusMetricsSystemTest { @Test public void returnsPrometheusMetricsWhenEnabled() { - final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); - metricsConfiguration.setEnabled(true); - metricsConfiguration.setPushEnabled(false); + final MetricsConfiguration metricsConfiguration = + MetricsConfiguration.builder().enabled(true).pushEnabled(false).build(); final MetricsSystem localMetricSystem = PrometheusMetricsSystem.init(metricsConfiguration); assertThat(localMetricSystem).isInstanceOf(PrometheusMetricsSystem.class); @@ -218,9 +218,8 @@ public class PrometheusMetricsSystemTest { @Test public void returnsNoOpMetricsWhenPushEnabled() { - final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); - metricsConfiguration.setEnabled(false); - metricsConfiguration.setPushEnabled(true); + final MetricsConfiguration metricsConfiguration = + MetricsConfiguration.builder().enabled(false).pushEnabled(true).build(); final MetricsSystem localMetricSystem = PrometheusMetricsSystem.init(metricsConfiguration); assertThat(localMetricSystem).isInstanceOf(PrometheusMetricsSystem.class); diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java b/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java index ffa08493a2..e4da69421e 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java @@ -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.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.createDefault; import tech.pegasys.pantheon.Runner; import tech.pegasys.pantheon.RunnerBuilder; @@ -924,18 +923,18 @@ public class PantheonCommand implements DefaultCommandValues, Runnable { "--metrics-push-interval", "--metrics-push-prometheus-job")); - final MetricsConfiguration metricsConfiguration = createDefault(); - metricsConfiguration.setEnabled(isMetricsEnabled); - metricsConfiguration.setHost(metricsHost); - metricsConfiguration.setPort(metricsPort); - metricsConfiguration.setMetricCategories(metricCategories); - metricsConfiguration.setPushEnabled(isMetricsPushEnabled); - metricsConfiguration.setPushHost(metricsPushHost); - metricsConfiguration.setPushPort(metricsPushPort); - metricsConfiguration.setPushInterval(metricsPushInterval); - metricsConfiguration.setPrometheusJob(metricsPrometheusJob); - metricsConfiguration.setHostsWhitelist(hostsWhitelist); - return metricsConfiguration; + return MetricsConfiguration.builder() + .enabled(isMetricsEnabled) + .host(metricsHost) + .port(metricsPort) + .metricCategories(metricCategories) + .pushEnabled(isMetricsPushEnabled) + .pushHost(metricsPushHost) + .pushPort(metricsPushPort) + .pushInterval(metricsPushInterval) + .hostsWhitelist(hostsWhitelist) + .prometheusJob(metricsPrometheusJob) + .build(); } private Optional permissioningConfiguration() throws Exception { diff --git a/pantheon/src/test/java/tech/pegasys/pantheon/RunnerTest.java b/pantheon/src/test/java/tech/pegasys/pantheon/RunnerTest.java index 373068bacb..0957b92a80 100644 --- a/pantheon/src/test/java/tech/pegasys/pantheon/RunnerTest.java +++ b/pantheon/src/test/java/tech/pegasys/pantheon/RunnerTest.java @@ -360,10 +360,7 @@ public final class RunnerTest { } private MetricsConfiguration metricsConfiguration() { - final MetricsConfiguration configuration = MetricsConfiguration.createDefault(); - configuration.setPort(0); - configuration.setEnabled(false); - return configuration; + return MetricsConfiguration.builder().enabled(false).port(0).build(); } private static void setupState( diff --git a/pantheon/src/test/java/tech/pegasys/pantheon/cli/PantheonCommandTest.java b/pantheon/src/test/java/tech/pegasys/pantheon/cli/PantheonCommandTest.java index fc8a3d854c..fdf8b3ec1c 100644 --- a/pantheon/src/test/java/tech/pegasys/pantheon/cli/PantheonCommandTest.java +++ b/pantheon/src/test/java/tech/pegasys/pantheon/cli/PantheonCommandTest.java @@ -112,7 +112,7 @@ public class PantheonCommandTest extends CommandTestAbstract { defaultWebSocketConfiguration = WebSocketConfiguration.createDefault(); - defaultMetricsConfiguration = MetricsConfiguration.createDefault(); + defaultMetricsConfiguration = MetricsConfiguration.builder().build(); } @Test @@ -297,10 +297,8 @@ public class PantheonCommandTest extends CommandTestAbstract { webSocketConfiguration.setPort(9101); webSocketConfiguration.setRpcApis(expectedApis); - final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); - metricsConfiguration.setEnabled(false); - metricsConfiguration.setHost("8.6.7.5"); - metricsConfiguration.setPort(309); + final MetricsConfiguration metricsConfiguration = + MetricsConfiguration.builder().enabled(false).host("8.6.7.5").port(309).build(); parseCommand("--config-file", toml.toString()); @@ -701,7 +699,7 @@ public class PantheonCommandTest extends CommandTestAbstract { final WebSocketConfiguration webSocketConfiguration = WebSocketConfiguration.createDefault(); - final MetricsConfiguration metricsConfiguration = MetricsConfiguration.createDefault(); + final MetricsConfiguration metricsConfiguration = MetricsConfiguration.builder().build(); verify(mockRunnerBuilder).discovery(eq(true)); verify(mockRunnerBuilder)