Skip to content

Commit ffc05c1

Browse files
committed
post-review: JmxRemoteClient > JmxConnectorBuilder
1 parent 7a7fed7 commit ffc05c1

File tree

4 files changed

+46
-37
lines changed

4 files changed

+46
-37
lines changed
+5-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.contrib.jmxscraper.client;
6+
package io.opentelemetry.contrib.jmxscraper;
77

88
import static org.assertj.core.api.Assertions.assertThat;
99

10-
import io.opentelemetry.contrib.jmxscraper.TestApp;
11-
import io.opentelemetry.contrib.jmxscraper.TestAppContainer;
1210
import java.io.IOException;
1311
import javax.management.ObjectName;
1412
import javax.management.remote.JMXConnector;
@@ -17,7 +15,7 @@
1715
import org.junit.jupiter.api.Test;
1816
import org.testcontainers.containers.Network;
1917

20-
public class JmxRemoteClientTest {
18+
public class JmxConnectorBuilderTest {
2119

2220
private static Network network;
2321

@@ -36,7 +34,7 @@ void noAuth() {
3634
try (TestAppContainer app = new TestAppContainer().withNetwork(network).withJmxPort(9990)) {
3735
app.start();
3836
testConnector(
39-
() -> JmxRemoteClient.createNew(app.getHost(), app.getMappedPort(9990)).connect());
37+
() -> JmxConnectorBuilder.createNew(app.getHost(), app.getMappedPort(9990)).build());
4038
}
4139
}
4240

@@ -49,9 +47,9 @@ void loginPwdAuth() {
4947
app.start();
5048
testConnector(
5149
() ->
52-
JmxRemoteClient.createNew(app.getHost(), app.getMappedPort(9999))
50+
JmxConnectorBuilder.createNew(app.getHost(), app.getMappedPort(9999))
5351
.userCredentials(login, pwd)
54-
.connect());
52+
.build());
5553
}
5654
}
5755

jmx-scraper/src/integrationTest/java/io/opentelemetry/contrib/jmxscraper/target_systems/TargetSystemIntegrationTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import com.linecorp.armeria.testing.junit5.server.ServerExtension;
1313
import io.grpc.stub.StreamObserver;
1414
import io.opentelemetry.contrib.jmxscraper.JmxScraperContainer;
15-
import io.opentelemetry.contrib.jmxscraper.client.JmxRemoteClient;
15+
import io.opentelemetry.contrib.jmxscraper.JmxConnectorBuilder;
1616
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceRequest;
1717
import io.opentelemetry.proto.collector.metrics.v1.ExportMetricsServiceResponse;
1818
import io.opentelemetry.proto.collector.metrics.v1.MetricsServiceGrpc;
@@ -106,7 +106,7 @@ void endToEndTest() {
106106

107107
// TODO : wait for metrics to be sent and add assertions on what is being captured
108108
// for now we just test that we can connect to remote JMX using our client.
109-
try (JMXConnector connector = JmxRemoteClient.createNew(targetHost, targetPort).connect()) {
109+
try (JMXConnector connector = JmxConnectorBuilder.createNew(targetHost, targetPort).build()) {
110110
assertThat(connector.getMBeanServerConnection()).isNotNull();
111111
} catch (IOException e) {
112112
throw new RuntimeException(e);

jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/client/JmxRemoteClient.java renamed to jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/JmxConnectorBuilder.java

+35-23
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.contrib.jmxscraper.client;
6+
package io.opentelemetry.contrib.jmxscraper;
77

88
import com.google.errorprone.annotations.CanIgnoreReturnValue;
99
import java.io.IOException;
@@ -26,9 +26,9 @@
2626
import javax.security.auth.callback.UnsupportedCallbackException;
2727
import javax.security.sasl.RealmCallback;
2828

29-
public class JmxRemoteClient {
29+
public class JmxConnectorBuilder {
3030

31-
private static final Logger logger = Logger.getLogger(JmxRemoteClient.class.getName());
31+
private static final Logger logger = Logger.getLogger(JmxConnectorBuilder.class.getName());
3232

3333
private final JMXServiceURL url;
3434
@Nullable private String userName;
@@ -37,44 +37,65 @@ public class JmxRemoteClient {
3737
@Nullable private String realm;
3838
private boolean sslRegistry;
3939

40-
private JmxRemoteClient(JMXServiceURL url) {
40+
private JmxConnectorBuilder(JMXServiceURL url) {
4141
this.url = url;
4242
}
4343

44-
public static JmxRemoteClient createNew(String host, int port) {
45-
return new JmxRemoteClient(buildUrl(host, port));
44+
public static JmxConnectorBuilder createNew(String host, int port) {
45+
return new JmxConnectorBuilder(buildUrl(host, port));
4646
}
4747

48-
public static JmxRemoteClient createNew(String url) {
49-
return new JmxRemoteClient(buildUrl(url));
48+
public static JmxConnectorBuilder createNew(String url) {
49+
return new JmxConnectorBuilder(buildUrl(url));
5050
}
5151

5252
@CanIgnoreReturnValue
53-
public JmxRemoteClient userCredentials(String userName, String password) {
53+
public JmxConnectorBuilder userCredentials(String userName, String password) {
5454
this.userName = userName;
5555
this.password = password;
5656
return this;
5757
}
5858

5959
@CanIgnoreReturnValue
60-
public JmxRemoteClient withRemoteProfile(String profile) {
60+
public JmxConnectorBuilder withRemoteProfile(String profile) {
6161
this.profile = profile;
6262
return this;
6363
}
6464

6565
@CanIgnoreReturnValue
66-
public JmxRemoteClient withRealm(String realm) {
66+
public JmxConnectorBuilder withRealm(String realm) {
6767
this.realm = realm;
6868
return this;
6969
}
7070

7171
@CanIgnoreReturnValue
72-
public JmxRemoteClient withSslRegistry() {
72+
public JmxConnectorBuilder withSslRegistry() {
7373
this.sslRegistry = true;
7474
return this;
7575
}
7676

77-
public JMXConnector connect() throws IOException {
77+
/**
78+
* Builds JMX connector instance by connecting to the remote JMX endpoint
79+
*
80+
* @return JMX connector
81+
* @throws IOException in case of communication error
82+
*/
83+
public JMXConnector build() throws IOException {
84+
Map<String, Object> env = buildEnv();
85+
86+
try {
87+
if (sslRegistry) {
88+
return doConnectSslRegistry(url, env);
89+
}
90+
91+
return doConnect(url, env);
92+
93+
} catch (IOException e) {
94+
throw new IOException("Unable to connect to " + url.getHost() + ":" + url.getPort(), e);
95+
}
96+
}
97+
98+
private Map<String, Object> buildEnv() {
7899
Map<String, Object> env = new HashMap<>();
79100
if (userName != null && password != null) {
80101
env.put(JMXConnector.CREDENTIALS, new String[] {userName, password});
@@ -111,16 +132,7 @@ public JMXConnector connect() throws IOException {
111132
} catch (ReflectiveOperationException e) {
112133
logger.log(Level.WARNING, "SASL unsupported in current environment: " + e.getMessage());
113134
}
114-
115-
try {
116-
if (sslRegistry) {
117-
return doConnectSslRegistry(url, env);
118-
} else {
119-
return doConnect(url, env);
120-
}
121-
} catch (IOException e) {
122-
throw new IOException("Unable to connect to " + url.getHost() + ":" + url.getPort(), e);
123-
}
135+
return env;
124136
}
125137

126138
@SuppressWarnings("BanJNDI")

jmx-scraper/src/main/java/io/opentelemetry/contrib/jmxscraper/JmxScraper.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package io.opentelemetry.contrib.jmxscraper;
77

8-
import io.opentelemetry.contrib.jmxscraper.client.JmxRemoteClient;
98
import io.opentelemetry.contrib.jmxscraper.config.ConfigurationException;
109
import io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig;
1110
import io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfigFactory;
@@ -25,7 +24,7 @@ public class JmxScraper {
2524
private static final Logger logger = Logger.getLogger(JmxScraper.class.getName());
2625
private static final String CONFIG_ARG = "-config";
2726

28-
private final JmxRemoteClient client;
27+
private final JmxConnectorBuilder client;
2928

3029
// TODO depend on instrumentation 2.9.0 snapshot
3130
// private final JmxMetricInsight service;
@@ -41,7 +40,7 @@ public static void main(String[] args) {
4140
JmxScraperConfig config = JmxScraper.createConfigFromArgs(Arrays.asList(args));
4241
// TODO: depend on instrumentation 2.9.0 snapshot
4342
// service = JmxMetricInsight.createService(GlobalOpenTelemetry.get(), config.getIntervalMilliseconds());
44-
JmxScraper jmxScraper = new JmxScraper(JmxRemoteClient.createNew(config.getServiceUrl()));
43+
JmxScraper jmxScraper = new JmxScraper(JmxConnectorBuilder.createNew(config.getServiceUrl()));
4544
jmxScraper.start();
4645

4746
} catch (ArgumentsParsingException e) {
@@ -109,13 +108,13 @@ private static Properties loadPropertiesFromPath(String path)
109108
}
110109
}
111110

112-
JmxScraper(JmxRemoteClient client) {
111+
JmxScraper(JmxConnectorBuilder client) {
113112
this.client = client;
114113
}
115114

116115
private void start() throws IOException {
117116

118-
JMXConnector connector = client.connect();
117+
JMXConnector connector = client.build();
119118

120119
@SuppressWarnings("unused")
121120
MBeanServerConnection connection = connector.getMBeanServerConnection();

0 commit comments

Comments
 (0)