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

This commit is contained in:
Trask Stalnaker 2021-03-31 22:53:22 -07:00 committed by GitHub
parent 84e7306370
commit af13e7d6e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 17 deletions

View File

@ -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'

View File

@ -0,0 +1,36 @@
/*
* 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
}
}
}
}

View File

@ -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

View File

@ -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)