Skip to content

Commit 2b97c46

Browse files
committed
post-review changes
1 parent 64b5e3a commit 2b97c46

File tree

2 files changed

+68
-59
lines changed

2 files changed

+68
-59
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.function.Function;
1414
import org.junit.jupiter.api.AfterAll;
1515
import org.junit.jupiter.api.BeforeAll;
16-
import org.junit.jupiter.api.BeforeEach;
1716
import org.junit.jupiter.api.Test;
1817
import org.junit.jupiter.api.io.TempDir;
1918
import org.junit.jupiter.params.ParameterizedTest;
@@ -60,12 +59,6 @@ static void afterAll() {
6059
network.close();
6160
}
6261

63-
@BeforeEach
64-
void beforeEach() {
65-
// extra safety to ensure temp folder is empty before each test method
66-
assertThat(tempDir).isEmptyDirectory();
67-
}
68-
6962
@Test
7063
void connectionError() {
7164
try (JmxScraperContainer scraper = scraperContainer().withRmiServiceUrl("unknown_host", 1234)) {

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

Lines changed: 68 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Map;
2121
import java.util.Set;
2222
import java.util.stream.Collectors;
23+
import org.jetbrains.annotations.NotNull;
2324
import org.testcontainers.containers.GenericContainer;
2425
import org.testcontainers.containers.wait.strategy.Wait;
2526
import org.testcontainers.utility.MountableFile;
@@ -214,43 +215,47 @@ public JmxScraperContainer withConfigSource(ConfigSource source) {
214215
@Override
215216
public void start() {
216217

217-
Map<String, String> config = new HashMap<>();
218-
config.put("otel.metrics.exporter", "otlp");
219-
config.put("otel.exporter.otlp.endpoint", endpoint);
220-
221-
if (!targetSystems.isEmpty()) {
222-
config.put("otel.jmx.target.system", String.join(",", targetSystems));
223-
}
218+
Map<String, String> config = initConfig();
224219

225-
if (serviceUrl == null) {
226-
throw new IllegalStateException("Missing service URL");
227-
}
228-
config.put("otel.jmx.service.url", serviceUrl);
220+
List<String> cmd = createCommand(config);
229221

230-
// always use a very short export interval for testing
231-
config.put("otel.metric.export.interval", "1s");
222+
if (configSource != ConfigSource.STDIN) {
223+
this.withCommand(cmd.toArray(new String[0]));
224+
} else {
225+
Path script = generateShellScript(cmd, config);
232226

233-
if (user != null) {
234-
config.put("otel.jmx.username", user);
235-
}
236-
if (password != null) {
237-
config.put("otel.jmx.password", password);
227+
this.withCopyFileToContainer(MountableFile.forHostPath(script, 500), "/scraper.sh");
228+
this.withCommand("/scraper.sh");
238229
}
239230

240-
addSecureStore(keyStore, /* isKeyStore= */ true, config);
241-
addSecureStore(trustStore, /* isKeyStore= */ false, config);
231+
logger().info("Starting scraper with command: " + String.join(" ", this.getCommandParts()));
232+
super.start();
233+
}
242234

243-
if (sslRmiRegistry) {
244-
config.put("otel.jmx.remote.registry.ssl", "true");
235+
private Path generateShellScript(List<String> cmd, Map<String, String> config) {
236+
// generate shell script to feed standard input with config
237+
List<String> lines = new ArrayList<>();
238+
lines.add("#!/bin/bash");
239+
lines.add(String.join(" ", cmd) + "<<EOF");
240+
lines.addAll(toKeyValueString(config));
241+
lines.add("EOF");
242+
243+
Path script;
244+
try {
245+
script = Files.createTempFile("scraper", ".sh");
246+
Files.write(script, lines);
247+
} catch (IOException e) {
248+
throw new IllegalStateException(e);
245249
}
246250

247-
if (!customYamlFiles.isEmpty()) {
248-
for (String yaml : customYamlFiles) {
249-
this.withCopyFileToContainer(MountableFile.forClasspathResource(yaml), yaml);
250-
}
251-
config.put("otel.jmx.config", String.join(",", customYamlFiles));
251+
logger().info("Scraper executed with /scraper.sh shell script");
252+
for (int i = 0; i < lines.size(); i++) {
253+
logger().info("/scrapper.sh:{} {}", i, lines.get(i));
252254
}
255+
return script;
256+
}
253257

258+
private List<String> createCommand(Map<String, String> config) {
254259
List<String> cmd = new ArrayList<>();
255260
cmd.add("java");
256261

@@ -328,36 +333,47 @@ public void start() {
328333
Wait.forLogMessage(".*JMX scraping started.*", 1)
329334
.withStartupTimeout(Duration.ofSeconds(10)));
330335
}
336+
return cmd;
337+
}
331338

332-
if (configSource != ConfigSource.STDIN) {
333-
this.withCommand(cmd.toArray(new String[0]));
334-
} else {
335-
// generate shell script to feed standard input with config
336-
List<String> lines = new ArrayList<>();
337-
lines.add("#!/bin/bash");
338-
lines.add(String.join(" ", cmd) + "<<EOF");
339-
lines.addAll(toKeyValueString(config));
340-
lines.add("EOF");
341-
342-
Path script;
343-
try {
344-
script = Files.createTempFile("scraper", ".sh");
345-
Files.write(script, lines);
346-
} catch (IOException e) {
347-
throw new IllegalStateException(e);
348-
}
339+
private @NotNull Map<String, String> initConfig() {
340+
Map<String, String> config = new HashMap<>();
341+
config.put("otel.metrics.exporter", "otlp");
342+
config.put("otel.exporter.otlp.endpoint", endpoint);
349343

350-
logger().info("Scraper executed with /scraper.sh shell script");
351-
for (int i = 0; i < lines.size(); i++) {
352-
logger().info("/scrapper.sh:{} {}", i, lines.get(i));
353-
}
344+
if (!targetSystems.isEmpty()) {
345+
config.put("otel.jmx.target.system", String.join(",", targetSystems));
346+
}
354347

355-
this.withCopyFileToContainer(MountableFile.forHostPath(script, 500), "/scraper.sh");
356-
this.withCommand("/scraper.sh");
348+
if (serviceUrl == null) {
349+
throw new IllegalStateException("Missing service URL");
357350
}
351+
config.put("otel.jmx.service.url", serviceUrl);
358352

359-
logger().info("Starting scraper with command: " + String.join(" ", this.getCommandParts()));
360-
super.start();
353+
// always use a very short export interval for testing
354+
config.put("otel.metric.export.interval", "1s");
355+
356+
if (user != null) {
357+
config.put("otel.jmx.username", user);
358+
}
359+
if (password != null) {
360+
config.put("otel.jmx.password", password);
361+
}
362+
363+
addSecureStore(keyStore, /* isKeyStore= */ true, config);
364+
addSecureStore(trustStore, /* isKeyStore= */ false, config);
365+
366+
if (sslRmiRegistry) {
367+
config.put("otel.jmx.remote.registry.ssl", "true");
368+
}
369+
370+
if (!customYamlFiles.isEmpty()) {
371+
for (String yaml : customYamlFiles) {
372+
this.withCopyFileToContainer(MountableFile.forClasspathResource(yaml), yaml);
373+
}
374+
config.put("otel.jmx.config", String.join(",", customYamlFiles));
375+
}
376+
return config;
361377
}
362378

363379
private void addSecureStore(

0 commit comments

Comments
 (0)