Skip to content

Commit ceb7d7e

Browse files
committed
add some tests
1 parent c5fc196 commit ceb7d7e

File tree

7 files changed

+131
-63
lines changed

7 files changed

+131
-63
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ protected JmxScraperContainer customizeScraperContainer(
3434
.withTargetSystem("jvm")
3535
// TODO when JVM metrics will be added to instrumentation, the default "auto" source
3636
// means that the definitions in instrumentation will be used, and thus this test will fail
37-
// due to metrics differences, adding an explicit "legacy" source is required to continue testing
38-
// the JVM metrics defined in this project.
37+
// due to metrics differences, adding an explicit "legacy" source is required to continue
38+
// testing the JVM metrics defined in this project.
3939
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/13392
4040
// .withTargetSystem("legacy")
4141
// also testing custom yaml

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ public static JmxScraperConfig fromConfig(ConfigProperties config) {
227227
scraperConfig.realm = config.getString("otel.jmx.realm");
228228
scraperConfig.registrySsl = config.getBoolean("otel.jmx.remote.registry.ssl", false);
229229

230-
// ensures support for target systems by resolving the yaml resource
230+
// checks target system is supported by resolving the yaml resource, throws exception on
231+
// missing/error
231232
scraperConfig.targetSystems.forEach(scraperConfig::getTargetSystemYaml);
232233

233234
String source = config.getString(JMX_TARGET_SOURCE, TargetSystemSource.AUTO.name());

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

Lines changed: 83 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@
1212
import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.JMX_REGISTRY_SSL;
1313
import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.JMX_REMOTE_PROFILE;
1414
import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.JMX_SERVICE_URL;
15+
import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.JMX_TARGET_SOURCE;
1516
import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.JMX_TARGET_SYSTEM;
1617
import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.JMX_USERNAME;
1718
import static io.opentelemetry.contrib.jmxscraper.config.JmxScraperConfig.fromConfig;
1819
import static org.assertj.core.api.Assertions.assertThat;
1920
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2021

2122
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
23+
import java.io.InputStream;
2224
import java.time.Duration;
25+
import java.util.Locale;
2326
import java.util.Properties;
2427
import org.junit.jupiter.api.BeforeAll;
2528
import org.junit.jupiter.api.Test;
2629
import org.junit.jupiter.params.ParameterizedTest;
30+
import org.junit.jupiter.params.provider.EnumSource;
2731
import org.junit.jupiter.params.provider.ValueSource;
2832

2933
class JmxScraperConfigTest {
@@ -138,74 +142,93 @@ void shouldFailValidation_missingConfigPathAndTargetSystem() {
138142
.hasMessage("at least one of 'otel.jmx.target.system' or 'otel.jmx.config' must be set");
139143
}
140144

141-
@Test
142-
void shouldFailValidation_invalidTargetSystem() {
145+
@ParameterizedTest
146+
@EnumSource(JmxScraperConfig.TargetSystemSource.class)
147+
void shouldFailValidation_invalidTargetSystem(JmxScraperConfig.TargetSystemSource source) {
143148
// Given
144149
Properties properties = (Properties) validProperties.clone();
145150
properties.setProperty(JMX_TARGET_SYSTEM, "hal9000");
151+
properties.setProperty(JMX_TARGET_SOURCE, source.name().toLowerCase(Locale.ROOT));
146152

147153
// When and Then
148154
assertThatThrownBy(() -> fromConfig(TestUtil.configProperties(properties)))
149155
.isInstanceOf(ConfigurationException.class)
150156
.hasMessageStartingWith("unsupported target system");
151157
}
152158

153-
// TODO: Tests below will be reimplemented
154-
155-
// @Test
156-
// @SetSystemProperty(key = "otel.jmx.service.url", value = "myServiceUrl")
157-
// @SetSystemProperty(key = "javax.net.ssl.keyStorePassword", value = "truth")
158-
// void propertiesFileOverride() {
159-
// Properties props = new Properties();
160-
// JmxMetrics.loadPropertiesFromPath(
161-
// props, ClassLoader.getSystemClassLoader().getResource("all.properties").getPath());
162-
// JmxConfig config = new JmxConfig(props);
163-
//
164-
// // This property should retain the system property value, not the config file value
165-
// assertThat(config.serviceUrl).isEqualTo("myServiceUrl");
166-
// // These properties are set from the config file
167-
// assertThat(config.groovyScript).isEqualTo("/my/groovy/script");
168-
// assertThat(config.targetSystem).isEqualTo("jvm,cassandra");
169-
// assertThat(config.targetSystems).containsOnly("jvm", "cassandra");
170-
// assertThat(config.intervalMilliseconds).isEqualTo(20000);
171-
// assertThat(config.metricsExporterType).isEqualTo("otlp");
172-
// assertThat(config.otlpExporterEndpoint).isEqualTo("https://myotlpendpoint");
173-
// assertThat(config.prometheusExporterHost).isEqualTo("host123.domain.com");
174-
// assertThat(config.prometheusExporterPort).isEqualTo(67890);
175-
// assertThat(config.username).isEqualTo("myUser\nname");
176-
// assertThat(config.password).isEqualTo("myPassw\\ord");
177-
// assertThat(config.remoteProfile).isEqualTo("SASL/DIGEST-MD5");
178-
// assertThat(config.realm).isEqualTo("myRealm");
179-
//
180-
// // This property should retain the system property value, not the config file value
181-
// assertThat(System.getProperty("javax.net.ssl.keyStorePassword")).isEqualTo("truth");
182-
// // These properties are set from the config file loading into JmxConfig
183-
// assertThat(System.getProperty("javax.net.ssl.keyStore")).isEqualTo("/my/key/store");
184-
// assertThat(System.getProperty("javax.net.ssl.keyStoreType")).isEqualTo("JKS");
185-
// assertThat(System.getProperty("javax.net.ssl.trustStore")).isEqualTo("/my/trust/store");
186-
// assertThat(System.getProperty("javax.net.ssl.trustStorePassword")).isEqualTo("def456");
187-
// assertThat(System.getProperty("javax.net.ssl.trustStoreType")).isEqualTo("JKS");
188-
// }
189-
//
190-
// @Test
191-
// @SetSystemProperty(key = "otel.jmx.service.url", value = "myServiceUrl")
192-
// @SetSystemProperty(key = "otel.jmx.groovy.script", value = "myGroovyScript")
193-
// @SetSystemProperty(key = "otel.jmx.target.system", value = "myTargetSystem")
194-
// void canSupportScriptAndTargetSystem() {
195-
// JmxConfig config = new JmxConfig();
196-
//
197-
// assertThat(config.serviceUrl).isEqualTo("myServiceUrl");
198-
// assertThat(config.groovyScript).isEqualTo("myGroovyScript");
199-
// assertThat(config.targetSystem).isEqualTo("mytargetsystem");
200-
// assertThat(config.targetSystems).containsOnly("mytargetsystem");
201-
// }
202-
//
203-
// @Test
204-
// @SetSystemProperty(key = "otel.metric.export.interval", value = "123")
205-
// void otelMetricExportIntervalRespected() {
206-
// JmxConfig config = new JmxConfig();
207-
// assertThat(config.intervalMilliseconds).isEqualTo(10000);
208-
// assertThat(config.properties.getProperty("otel.metric.export.interval")).isEqualTo("123");
209-
// }
210-
//
159+
@ParameterizedTest
160+
@ValueSource(strings = {"auto", ""})
161+
void targetSystemSource_auto(String source) {
162+
Properties properties = (Properties) validProperties.clone();
163+
properties.setProperty(JMX_TARGET_SYSTEM, "fake-test-system1");
164+
if (!source.isEmpty()) {
165+
properties.setProperty(JMX_TARGET_SOURCE, source);
166+
}
167+
168+
JmxScraperConfig config = fromConfig(TestUtil.configProperties(properties));
169+
170+
// should resolve to instrumentation when available in both
171+
shouldResolveToInstrumentationYaml(config, "fake-test-system1");
172+
173+
// should resolve to legacy yaml when not available in instrumentation
174+
shouldResolveToLegacyYaml(config, "fake-test-system2");
175+
176+
// should resolve to instrumentation when only defined there
177+
shouldResolveToInstrumentationYaml(config, "fake-test-system3");
178+
}
179+
180+
@Test
181+
void targetSystemSource_legacy() {
182+
Properties properties = (Properties) validProperties.clone();
183+
properties.setProperty(JMX_TARGET_SYSTEM, "fake-test-system1");
184+
properties.setProperty(JMX_TARGET_SOURCE, "legacy");
185+
186+
JmxScraperConfig config = fromConfig(TestUtil.configProperties(properties));
187+
188+
shouldResolveToLegacyYaml(config, "fake-test-system1");
189+
190+
shouldResolveToLegacyYaml(config, "fake-test-system2");
191+
192+
// should not support system only defined in instrumentation
193+
shouldNotResolveYaml(config, "fake-test-system3");
194+
}
195+
196+
@Test
197+
void targetSystemSource_instrumentation() {
198+
Properties properties = (Properties) validProperties.clone();
199+
properties.setProperty(JMX_TARGET_SYSTEM, "fake-test-system1");
200+
properties.setProperty(JMX_TARGET_SOURCE, "instrumentation");
201+
202+
JmxScraperConfig config = fromConfig(TestUtil.configProperties(properties));
203+
204+
shouldResolveToInstrumentationYaml(config, "fake-test-system1");
205+
206+
// should not support system only defined in legacy
207+
shouldNotResolveYaml(config, "fake-test-system2");
208+
209+
shouldResolveToInstrumentationYaml(config, "fake-test-system3");
210+
}
211+
212+
private static InputStream getYaml(String path) {
213+
return JmxScraperConfigTest.class.getClassLoader().getResourceAsStream(path);
214+
}
215+
216+
private static void shouldResolveToInstrumentationYaml(JmxScraperConfig config, String target) {
217+
assertThat(config.getTargetSystemYaml(target))
218+
.describedAs("should resolve to instrumentation yaml")
219+
.hasSameContentAs(getYaml("jmx/rules/" + target + ".yaml"));
220+
}
221+
222+
private static void shouldResolveToLegacyYaml(JmxScraperConfig config, String target) {
223+
assertThat(config.getTargetSystemYaml(target))
224+
.describedAs("should resolve to legacy yaml")
225+
.hasSameContentAs(getYaml(target + ".yaml"));
226+
}
227+
228+
private static void shouldNotResolveYaml(JmxScraperConfig config, String target) {
229+
assertThatThrownBy(() -> config.getTargetSystemYaml(target))
230+
.describedAs("should not support system")
231+
.isInstanceOf(ConfigurationException.class)
232+
.hasMessageStartingWith("unsupported target system");
233+
}
211234
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
# test system 1 that is in the same location as legacy yaml files
3+
rules:
4+
5+
- bean: java.lang:type=Runtime
6+
mapping:
7+
Uptime:
8+
metric: custom.jvm.uptime
9+
type: counter
10+
unit: ms
11+
desc: JVM uptime in milliseconds
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
# test system 2 that is in the same location as legacy yaml files and is not suppored in instrumentation
3+
rules:
4+
5+
- bean: java.lang:type=Runtime
6+
mapping:
7+
Uptime:
8+
metric: custom.jvm.uptime
9+
type: counter
10+
unit: ms
11+
desc: JVM uptime in milliseconds
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
# test system 1 that is in the same location as instrumentation yaml files
3+
rules:
4+
5+
- bean: java.lang:type=Runtime
6+
mapping:
7+
Uptime:
8+
metric: custom.jvm.uptime
9+
type: counter
10+
unit: ms
11+
desc: JVM uptime in milliseconds
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
# test system 3 that is in the same location as instrumentation yaml files and is NOT defined in legacy
3+
rules:
4+
5+
- bean: java.lang:type=Runtime
6+
mapping:
7+
Uptime:
8+
metric: custom.jvm.uptime
9+
type: counter
10+
unit: ms
11+
desc: JVM uptime in milliseconds

0 commit comments

Comments
 (0)