Skip to content

Commit 3a1021f

Browse files
authored
Merge pull request #7 from robsunday/jmx-scraper-impl
Code review followup changes - added default value for OTLP endpoint
2 parents 86c3e54 + 2c95782 commit 3a1021f

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

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

+8-5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ public class JmxScraperConfig {
3434
static final String JMX_REMOTE_PROFILE = "otel.jmx.remote.profile";
3535
static final String JMX_REALM = "otel.jmx.realm";
3636

37+
static final String OTLP_METRICS_EXPORTER = "otlp";
38+
3739
static final List<String> AVAILABLE_TARGET_SYSTEMS =
3840
Collections.unmodifiableList(
3941
Arrays.asList(
@@ -148,8 +150,10 @@ public static JmxScraperConfig fromProperties(
148150

149151
config.metricsExporterType =
150152
getAndSetPropertyIfUndefined(properties, METRICS_EXPORTER_TYPE, "logging");
151-
config.otlpExporterEndpoint = properties.getProperty(OTLP_ENDPOINT);
152-
153+
if (OTLP_METRICS_EXPORTER.equalsIgnoreCase(config.metricsExporterType)) {
154+
config.otlpExporterEndpoint =
155+
getAndSetPropertyIfUndefined(properties, OTLP_ENDPOINT, "http://localhost:4318");
156+
}
153157
config.username = properties.getProperty(JMX_USERNAME);
154158
config.password = properties.getProperty(JMX_PASSWORD);
155159

@@ -232,9 +236,8 @@ private static void validateConfig(JmxScraperConfig config) throws Configuration
232236
"%s must specify targets from %s", config.targetSystems, AVAILABLE_TARGET_SYSTEMS));
233237
}
234238

235-
if (isBlank(config.otlpExporterEndpoint)
236-
&& (!isBlank(config.metricsExporterType)
237-
&& config.metricsExporterType.equalsIgnoreCase("otlp"))) {
239+
if (OTLP_METRICS_EXPORTER.equalsIgnoreCase(config.metricsExporterType)
240+
&& isBlank(config.otlpExporterEndpoint)) {
238241
throw new ConfigurationException(OTLP_ENDPOINT + " must be specified for otlp format.");
239242
}
240243

jmx-scraper/src/test/java/io/opentelemetry/contrib/jmxscraper/config/JmxScraperConfigTest.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ static void setUp() {
3737
SERVICE_URL, "jservice:jmx:rmi:///jndi/rmi://localhost:9010/jmxrmi");
3838
validProperties.setProperty(CUSTOM_JMX_SCRAPING_CONFIG, "");
3939
validProperties.setProperty(TARGET_SYSTEM, "tomcat, activemq");
40-
validProperties.setProperty(METRICS_EXPORTER_TYPE, "otel");
40+
validProperties.setProperty(METRICS_EXPORTER_TYPE, "otlp");
4141
validProperties.setProperty(INTERVAL_MILLISECONDS, "1410");
4242
validProperties.setProperty(REGISTRY_SSL, "true");
4343
validProperties.setProperty(OTLP_ENDPOINT, "http://localhost:4317");
@@ -77,13 +77,29 @@ void shouldCreateMinimalValidConfiguration() throws ConfigurationException {
7777
assertThat(config.getTargetSystems()).isEmpty();
7878
assertThat(config.getIntervalMilliseconds()).isEqualTo(10000);
7979
assertThat(config.getMetricsExporterType()).isEqualTo("logging");
80-
assertThat(config.getOtlpExporterEndpoint()).isNull();
80+
assertThat(config.getOtlpExporterEndpoint()).isBlank();
8181
assertThat(config.getUsername()).isNull();
8282
assertThat(config.getPassword()).isNull();
8383
assertThat(config.getRemoteProfile()).isNull();
8484
assertThat(config.getRealm()).isNull();
8585
}
8686

87+
@Test
88+
void shouldCreateConfig_defaultOtlEndpoint() throws ConfigurationException {
89+
// Given
90+
Properties properties = new Properties();
91+
properties.setProperty(SERVICE_URL, "jservice:jmx:rmi:///jndi/rmi://localhost:9010/jmxrmi");
92+
properties.setProperty(CUSTOM_JMX_SCRAPING_CONFIG, "/file.properties");
93+
properties.setProperty(METRICS_EXPORTER_TYPE, "otlp");
94+
95+
// When
96+
JmxScraperConfig config = fromProperties(properties, new Properties());
97+
98+
// Then
99+
assertThat(config.getMetricsExporterType()).isEqualTo("otlp");
100+
assertThat(config.getOtlpExporterEndpoint()).isEqualTo("http://localhost:4318");
101+
}
102+
87103
@Test
88104
@ClearSystemProperty(key = "javax.net.ssl.keyStore")
89105
@ClearSystemProperty(key = "javax.net.ssl.keyStorePassword")
@@ -116,7 +132,7 @@ void shouldUseValuesFromProperties() throws ConfigurationException {
116132
assertThat(config.getCustomJmxScrapingConfigPath()).isEqualTo("");
117133
assertThat(config.getTargetSystems()).containsOnly("tomcat", "activemq");
118134
assertThat(config.getIntervalMilliseconds()).isEqualTo(1410);
119-
assertThat(config.getMetricsExporterType()).isEqualTo("otel");
135+
assertThat(config.getMetricsExporterType()).isEqualTo("otlp");
120136
assertThat(config.getOtlpExporterEndpoint()).isEqualTo("http://localhost:4317");
121137
assertThat(config.getUsername()).isEqualTo("some-user");
122138
assertThat(config.getPassword()).isEqualTo("some-password");
@@ -196,10 +212,10 @@ void shouldFailValidation_invalidTargetSystem() {
196212
}
197213

198214
@Test
199-
void shouldFailValidation_missingOtlpEndpoint() {
215+
void shouldFailValidation_blankOtlpEndpointProvided() {
200216
// Given
201217
Properties properties = (Properties) validProperties.clone();
202-
properties.remove(OTLP_ENDPOINT);
218+
properties.setProperty(OTLP_ENDPOINT, "");
203219
properties.setProperty(METRICS_EXPORTER_TYPE, "otlp");
204220

205221
// When and Then

0 commit comments

Comments
 (0)