Remove oshi-related code from shared agent code
MODIFY
instrumentation/oshi/javaagent/oshi-javaagent.gradle
instrumentation/oshi/javaagent/oshi-javaagent.gradle
@@ -9,6 +9,7 @@ muzzle {
}
dependencies {
+ api project(':javaagent-spi')
implementation project(':instrumentation:oshi:library')
library group: 'com.github.oshi', name: 'oshi-core', version: '5.3.1'

ADD
instrumentation/oshi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/oshi/OshiMetricsInstaller.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.oshi;
+
+import com.google.auto.service.AutoService;
+import io.opentelemetry.instrumentation.api.config.Config;
+import io.opentelemetry.javaagent.spi.ComponentInstaller;
+import java.lang.reflect.Method;
+import java.util.Collections;
+
+/**
+ * {@link ComponentInstaller} to enable oshi metrics during agent startup if oshi is present on the
+ * system classpath.
+ */
+@AutoService(ComponentInstaller.class)
+public class OshiMetricsInstaller implements ComponentInstaller {
+ @Override
+ public void afterByteBuddyAgent(Config config) {
+ if (config.isInstrumentationEnabled(Collections.singleton("oshi"), true)) {
+ try {
+ // Call oshi.SystemInfo.getCurrentPlatformEnum() to activate SystemMetrics.
+ // Oshi instrumentation will intercept this call and enable SystemMetrics.
+ Class<?> oshiSystemInfoClass =
+ ClassLoader.getSystemClassLoader().loadClass("oshi.SystemInfo");
+ Method getCurrentPlatformEnumMethod =
+ oshiSystemInfoClass.getMethod("getCurrentPlatformEnum");
+ getCurrentPlatformEnumMethod.invoke(null);
+ } catch (Throwable ex) {
+ // OK
+ }
+ }
+ }
+}

MODIFY
instrumentation/oshi/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/oshi/OshiTest.groovy
instrumentation/oshi/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/oshi/OshiTest.groovy
@@ -9,17 +9,11 @@ import static java.util.concurrent.TimeUnit.SECONDS
import com.google.common.base.Stopwatch
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
-import oshi.PlatformEnum
-import oshi.SystemInfo
class OshiTest extends AgentInstrumentationSpecification {
def "test system metrics is enabled"() {
- setup:
- PlatformEnum platform = SystemInfo.getCurrentPlatformEnum()
-
expect:
- platform != null
// TODO (trask) is this the instrumentation library name we want?
findMetric("io.opentelemetry.instrumentation.oshi", "system.disk.io") != null
findMetric("io.opentelemetry.instrumentation.oshi", "system.disk.operations") != null

MODIFY
javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/OpenTelemetryAgent.java
javaagent-bootstrap/src/main/java/io/opentelemetry/javaagent/OpenTelemetryAgent.java
@@ -67,17 +67,6 @@ public class OpenTelemetryAgent {
System.err.println("ERROR " + thisClass.getName());
ex.printStackTrace();
}
-
- try {
- // Call oshi.SystemInfo.getCurrentPlatformEnum() to activate SystemMetrics.
- // Oshi instrumentation will intercept this call and enable SystemMetrics.
- Class<?> oshiSystemInfoClass =
- ClassLoader.getSystemClassLoader().loadClass("oshi.SystemInfo");
- Method getCurrentPlatformEnumMethod = oshiSystemInfoClass.getMethod("getCurrentPlatformEnum");
- getCurrentPlatformEnumMethod.invoke(null);
- } catch (Throwable ex) {
- // OK
- }
}
private static synchronized URL installBootstrapJar(Instrumentation inst)

Remove oshi-related code from shared agent code (#2682)