From e44c71f47377f3e165c609ea7a0e387317c66c3e Mon Sep 17 00:00:00 2001
From: Lauri Tulmin <ltulmin@splunk.com>
Date: Wed, 17 Apr 2024 11:25:26 +0300
Subject: [PATCH] Fix jmx-metrics on wildfly

---
 .../jmx/JmxMetricInsightInstaller.java         | 11 +++++------
 .../instrumentation/jmx/engine/BeanFinder.java | 18 ++++++++++++------
 2 files changed, 17 insertions(+), 12 deletions(-)

diff --git a/instrumentation/jmx-metrics/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/jmx/JmxMetricInsightInstaller.java b/instrumentation/jmx-metrics/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/jmx/JmxMetricInsightInstaller.java
index 8dbe6c37bd14..74a6f75ffb4f 100644
--- a/instrumentation/jmx-metrics/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/jmx/JmxMetricInsightInstaller.java
+++ b/instrumentation/jmx-metrics/javaagent/src/main/java/io/opentelemetry/instrumentation/javaagent/jmx/JmxMetricInsightInstaller.java
@@ -33,23 +33,22 @@ public void afterAgent(AutoConfiguredOpenTelemetrySdk autoConfiguredSdk) {
 
     if (config.getBoolean("otel.jmx.enabled", true)) {
       JmxMetricInsight service =
-          JmxMetricInsight.createService(GlobalOpenTelemetry.get(), beanDiscoveryDelay(config));
+          JmxMetricInsight.createService(
+              GlobalOpenTelemetry.get(), beanDiscoveryDelay(config).toMillis());
       MetricConfiguration conf = buildMetricConfiguration(config);
       service.start(conf);
     }
   }
 
-  private static long beanDiscoveryDelay(ConfigProperties configProperties) {
-    Long discoveryDelay = configProperties.getLong("otel.jmx.discovery.delay");
+  private static Duration beanDiscoveryDelay(ConfigProperties configProperties) {
+    Duration discoveryDelay = configProperties.getDuration("otel.jmx.discovery.delay");
     if (discoveryDelay != null) {
       return discoveryDelay;
     }
 
     // If discovery delay has not been configured, have a peek at the metric export interval.
     // It makes sense for both of these values to be similar.
-    return configProperties
-        .getDuration("otel.metric.export.interval", Duration.ofMinutes(1))
-        .toMillis();
+    return configProperties.getDuration("otel.metric.export.interval", Duration.ofMinutes(1));
   }
 
   private static String resourceFor(String platform) {
diff --git a/instrumentation/jmx-metrics/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/BeanFinder.java b/instrumentation/jmx-metrics/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/BeanFinder.java
index 8b0fd636bed3..f3da0cef7efa 100644
--- a/instrumentation/jmx-metrics/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/BeanFinder.java
+++ b/instrumentation/jmx-metrics/library/src/main/java/io/opentelemetry/instrumentation/jmx/engine/BeanFinder.java
@@ -39,12 +39,18 @@ class BeanFinder {
   void discoverBeans(MetricConfiguration conf) {
     this.conf = conf;
 
-    if (!conf.isEmpty()) {
-      // Issue 9336: Corner case: PlatformMBeanServer will remain unitialized until a direct
-      // reference to it is made. This call makes sure that the PlatformMBeanServer will be in
-      // the set of MBeanServers reported by MBeanServerFactory.
-      ManagementFactory.getPlatformMBeanServer();
-    }
+    exec.schedule(
+        () -> {
+          // Issue 9336: Corner case: PlatformMBeanServer will remain unitialized until a direct
+          // reference to it is made. This call makes sure that the PlatformMBeanServer will be in
+          // the set of MBeanServers reported by MBeanServerFactory.
+          // Issue 11143: This call initializes java.util.logging.LogManager. We should not call it
+          // before application has had a chance to configure custom log manager. This is needed for
+          // wildfly.
+          ManagementFactory.getPlatformMBeanServer();
+        },
+        discoveryDelay,
+        TimeUnit.MILLISECONDS);
 
     exec.schedule(
         new Runnable() {