|
20 | 20 | import java.util.Map; |
21 | 21 | import java.util.Set; |
22 | 22 | import java.util.stream.Collectors; |
| 23 | +import org.jetbrains.annotations.NotNull; |
23 | 24 | import org.testcontainers.containers.GenericContainer; |
24 | 25 | import org.testcontainers.containers.wait.strategy.Wait; |
25 | 26 | import org.testcontainers.utility.MountableFile; |
@@ -214,43 +215,47 @@ public JmxScraperContainer withConfigSource(ConfigSource source) { |
214 | 215 | @Override |
215 | 216 | public void start() { |
216 | 217 |
|
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(); |
224 | 219 |
|
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); |
229 | 221 |
|
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); |
232 | 226 |
|
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"); |
238 | 229 | } |
239 | 230 |
|
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 | + } |
242 | 234 |
|
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); |
245 | 249 | } |
246 | 250 |
|
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)); |
252 | 254 | } |
| 255 | + return script; |
| 256 | + } |
253 | 257 |
|
| 258 | + private List<String> createCommand(Map<String, String> config) { |
254 | 259 | List<String> cmd = new ArrayList<>(); |
255 | 260 | cmd.add("java"); |
256 | 261 |
|
@@ -328,36 +333,47 @@ public void start() { |
328 | 333 | Wait.forLogMessage(".*JMX scraping started.*", 1) |
329 | 334 | .withStartupTimeout(Duration.ofSeconds(10))); |
330 | 335 | } |
| 336 | + return cmd; |
| 337 | + } |
331 | 338 |
|
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); |
349 | 343 |
|
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 | + } |
354 | 347 |
|
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"); |
357 | 350 | } |
| 351 | + config.put("otel.jmx.service.url", serviceUrl); |
358 | 352 |
|
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; |
361 | 377 | } |
362 | 378 |
|
363 | 379 | private void addSecureStore( |
|
0 commit comments