Skip to content

Commit d8e5217

Browse files
authored
ZOOKEEPER-4972: Fix flaky tests in PrometheusMetricsProviderConfigTest
Reviewers: anmolnar Author: kezhuw Closes #2311 from kezhuw/ZOOKEEPER-4972-fix-flaky-PrometheusMetricsProviderConfigTest-tests
1 parent 23c0446 commit d8e5217

File tree

3 files changed

+32
-17
lines changed

3 files changed

+32
-17
lines changed

zookeeper-metrics-providers/zookeeper-prometheus-metrics/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@
6969
<artifactId>jetty-servlet</artifactId>
7070
<scope>provided</scope>
7171
</dependency>
72+
<dependency>
73+
<groupId>ch.qos.logback</groupId>
74+
<artifactId>logback-classic</artifactId>
75+
<scope>runtime</scope>
76+
<optional>true</optional>
77+
</dependency>
7278
<dependency>
7379
<groupId>org.mockito</groupId>
7480
<artifactId>mockito-core</artifactId>

zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import io.prometheus.client.exporter.MetricsServlet;
2424
import io.prometheus.client.hotspot.DefaultExports;
2525
import java.io.IOException;
26-
import java.net.InetSocketAddress;
2726
import java.util.Enumeration;
2827
import java.util.Objects;
2928
import java.util.Optional;
@@ -170,35 +169,45 @@ public void configure(Properties configuration) throws MetricsProviderLifeCycleE
170169
public void start() throws MetricsProviderLifeCycleException {
171170
this.executorOptional = createExecutor();
172171
try {
173-
LOG.info("Starting /metrics {} endpoint at HTTP port: {}, HTTPS port: {}, exportJvmInfo: {}",
174-
httpPort > 0 ? httpPort : "disabled",
175-
httpsPort > 0 ? httpsPort : "disabled",
172+
LOG.info("Starting /metrics endpoint at HTTP port: {}, HTTPS port: {}, exportJvmInfo: {}",
173+
httpPort >= 0 ? httpPort : "disabled",
174+
httpsPort >= 0 ? httpsPort : "disabled",
176175
exportJvmInfo);
177176
if (exportJvmInfo) {
178177
DefaultExports.initialize();
179178
}
180-
if (httpPort == -1) {
181-
server = new Server();
182-
} else {
183-
server = new Server(new InetSocketAddress(host, httpPort));
179+
server = new Server();
180+
ServerConnector httpConnector = null;
181+
ServerConnector httpsConnector = null;
182+
if (httpPort >= 0) {
183+
httpConnector = new ServerConnector(server);
184+
httpConnector.setHost(host);
185+
httpConnector.setPort(httpPort);
186+
server.addConnector(httpConnector);
184187
}
185-
if (httpsPort != -1) {
188+
if (httpsPort >= 0) {
186189
SslContextFactory sslServerContextFactory = new SslContextFactory.Server();
187190
configureSslContextFactory(sslServerContextFactory);
188191
KeyStoreScanner keystoreScanner = new KeyStoreScanner(sslServerContextFactory);
189192
keystoreScanner.setScanInterval(SCAN_INTERVAL);
190193
server.addBean(keystoreScanner);
191-
ServerConnector connector = new ServerConnector(server, sslServerContextFactory);
192-
connector.setPort(httpsPort);
193-
connector.setHost(host);
194-
server.addConnector(connector);
194+
httpsConnector = new ServerConnector(server, sslServerContextFactory);
195+
httpsConnector.setHost(host);
196+
httpsConnector.setPort(httpsPort);
197+
server.addConnector(httpsConnector);
195198
}
196199
ServletContextHandler context = new ServletContextHandler();
197200
context.setContextPath("/");
198201
constrainTraceMethod(context);
199202
server.setHandler(context);
200203
context.addServlet(new ServletHolder(servlet), "/metrics");
201204
server.start();
205+
if (httpPort == 0) {
206+
LOG.info("Bound /metrics endpoint to HTTP port: {}", httpConnector.getLocalPort());
207+
}
208+
if (httpsPort == 0) {
209+
LOG.info("Bound /metrics endpoint to HTTPS port: {}", httpsConnector.getLocalPort());
210+
}
202211
} catch (Exception err) {
203212
LOG.error("Cannot start /metrics server", err);
204213
if (server != null) {

zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/test/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProviderConfigTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void testValidSslConfig() throws MetricsProviderLifeCycleException {
6464
Properties configuration = new Properties();
6565
String testDataPath = System.getProperty("test.data.dir", "src/test/resources/data");
6666
configuration.setProperty("httpHost", "127.0.0.1");
67-
configuration.setProperty("httpsPort", "50511");
67+
configuration.setProperty("httpsPort", "0");
6868
configuration.setProperty("ssl.keyStore.location", testDataPath + "/ssl/server_keystore.jks");
6969
configuration.setProperty("ssl.keyStore.password", "testpass");
7070
configuration.setProperty("ssl.trustStore.location", testDataPath + "/ssl/server_truststore.jks");
@@ -78,8 +78,8 @@ public void testValidHttpsAndHttpConfig() throws MetricsProviderLifeCycleExcepti
7878
PrometheusMetricsProvider provider = new PrometheusMetricsProvider();
7979
Properties configuration = new Properties();
8080
String testDataPath = System.getProperty("test.data.dir", "src/test/resources/data");
81-
configuration.setProperty("httpPort", "50512");
82-
configuration.setProperty("httpsPort", "50513");
81+
configuration.setProperty("httpPort", "0");
82+
configuration.setProperty("httpsPort", "0");
8383
configuration.setProperty("ssl.keyStore.location", testDataPath + "/ssl/server_keystore.jks");
8484
configuration.setProperty("ssl.keyStore.password", "testpass");
8585
configuration.setProperty("ssl.trustStore.location", testDataPath + "/ssl/server_truststore.jks");
@@ -95,7 +95,7 @@ public void testInvalidSslConfig() throws MetricsProviderLifeCycleException {
9595
PrometheusMetricsProvider provider = new PrometheusMetricsProvider();
9696
Properties configuration = new Properties();
9797
String testDataPath = System.getProperty("test.data.dir", "src/test/resources/data");
98-
configuration.setProperty("httpsPort", "50514");
98+
configuration.setProperty("httpsPort", "0");
9999
//keystore missing
100100
configuration.setProperty("ssl.keyStore.password", "testpass");
101101
configuration.setProperty("ssl.trustStore.location", testDataPath + "/ssl/server_truststore.jks");

0 commit comments

Comments
 (0)