Activate SystemMetrics
ADD
instrumentation/oshi/javaagent/oshi-javaagent.gradle
+apply from: "$rootDir/gradle/instrumentation.gradle"
+
+muzzle {
+ pass {
+ group = "com.github.oshi"
+ module = "oshi-core"
+ versions = "[5.3.1,)"
+ }
+}
+
+dependencies {
+ implementation project(':instrumentation:oshi:library')
+ implementation group: 'io.opentelemetry', name: 'opentelemetry-sdk-metrics'
+
+ library group: 'com.github.oshi', name: 'oshi-core', version: '5.3.1'
+}

ADD
instrumentation/oshi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/oshi/OshiInstrumentation.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.oshi;
+
+import static io.opentelemetry.javaagent.tooling.ClassLoaderMatcher.hasClassesNamed;
+import static net.bytebuddy.matcher.ElementMatchers.isMethod;
+import static net.bytebuddy.matcher.ElementMatchers.isPublic;
+import static net.bytebuddy.matcher.ElementMatchers.isStatic;
+import static net.bytebuddy.matcher.ElementMatchers.named;
+
+import com.google.auto.service.AutoService;
+import io.opentelemetry.instrumentation.oshi.SystemMetrics;
+import io.opentelemetry.javaagent.tooling.Instrumenter;
+import java.util.Collections;
+import java.util.Map;
+import net.bytebuddy.asm.Advice;
+import net.bytebuddy.description.method.MethodDescription;
+import net.bytebuddy.description.type.TypeDescription;
+import net.bytebuddy.matcher.ElementMatcher;
+
+@AutoService(Instrumenter.class)
+public class OshiInstrumentation extends Instrumenter.Default {
+
+ public OshiInstrumentation() {
+ super("oshi");
+ }
+
+ @Override
+ public ElementMatcher<ClassLoader> classLoaderMatcher() {
+ return hasClassesNamed("oshi.SystemInfo");
+ }
+
+ @Override
+ public ElementMatcher<? super TypeDescription> typeMatcher() {
+ return named("oshi.SystemInfo");
+ }
+
+ @Override
+ public final String[] helperClassNames() {
+ return new String[] {
+ "io.opentelemetry.instrumentation.oshi.SystemMetrics",
+ "io.opentelemetry.instrumentation.oshi.SystemMetrics$1",
+ "io.opentelemetry.instrumentation.oshi.SystemMetrics$2",
+ "io.opentelemetry.instrumentation.oshi.SystemMetrics$3",
+ "io.opentelemetry.instrumentation.oshi.SystemMetrics$4",
+ "io.opentelemetry.instrumentation.oshi.SystemMetrics$5",
+ "io.opentelemetry.instrumentation.oshi.SystemMetrics$6",
+ "io.opentelemetry.instrumentation.oshi.SystemMetrics$7"
+ };
+ }
+
+ @Override
+ public Map<? extends ElementMatcher<? super MethodDescription>, String> transformers() {
+ return Collections.singletonMap(
+ isMethod().and(isPublic()).and(isStatic()).and(named("getCurrentPlatformEnum")),
+ OshiInstrumentation.class.getName() + "$OshiInstrumentationAdvice");
+ }
+
+ public static class OshiInstrumentationAdvice {
+ @Advice.OnMethodEnter(suppress = Throwable.class)
+ public static void onEnter() {
+ SystemMetrics.registerObservers();
+ }
+ }
+}

ADD
instrumentation/oshi/javaagent/src/test/groovy/io/opentelemetry/javaagent/instrumentation/oshi/OshiTest.groovy
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.oshi
+
+import io.opentelemetry.instrumentation.test.AgentTestRunner
+import io.opentelemetry.sdk.OpenTelemetrySdk
+import io.opentelemetry.sdk.metrics.data.MetricData
+import oshi.PlatformEnum
+import oshi.SystemInfo
+
+class OshiTest extends AgentTestRunner {
+
+ def "test system metrics is enabled"() {
+ setup:
+ PlatformEnum platform = SystemInfo.getCurrentPlatformEnum()
+
+ expect:
+ Collection<MetricData> metrics = OpenTelemetrySdk.getGlobalMeterProvider().getMetricProducer().collectAllMetrics()
+ platform != null
+ metrics.size() == 7
+ }
+}

MODIFY
javaagent/src/main/java/io/opentelemetry/javaagent/OpenTelemetryAgent.java
javaagent/src/main/java/io/opentelemetry/javaagent/OpenTelemetryAgent.java
@@ -67,6 +67,17 @@ 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)

MODIFY
settings.gradle
settings.gradle
@@ -150,6 +150,7 @@ include ':instrumentation:netty:netty-4.1'
include ':instrumentation:okhttp:okhttp-2.2'
include ':instrumentation:okhttp:okhttp-3.0'
include ':instrumentation:opentelemetry-api-beta'
+include ':instrumentation:oshi:javaagent'
include ':instrumentation:oshi:library'
include ':instrumentation:play:play-2.3'
include ':instrumentation:play:play-2.4'

Activate SystemMetrics (#1530)