From a45aaf34fb2f8131aa8614659e87e738f26163ff Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Mon, 3 Jan 2022 08:44:33 +0200 Subject: [PATCH] Fix oshi latest dep test (#4993) --- .../instrumentation/oshi/OshiMetricsInstaller.java | 13 +++++++++++-- .../oshi/SystemInfoInstrumentation.java | 5 ++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/instrumentation/oshi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/oshi/OshiMetricsInstaller.java b/instrumentation/oshi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/oshi/OshiMetricsInstaller.java index 83c706abdde7..382d60f4728e 100644 --- a/instrumentation/oshi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/oshi/OshiMetricsInstaller.java +++ b/instrumentation/oshi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/oshi/OshiMetricsInstaller.java @@ -26,12 +26,21 @@ public void afterAgent(Config config) { // Oshi instrumentation will intercept this call and enable SystemMetrics. Class oshiSystemInfoClass = ClassLoader.getSystemClassLoader().loadClass("oshi.SystemInfo"); - Method getCurrentPlatformEnumMethod = - oshiSystemInfoClass.getMethod("getCurrentPlatformEnum"); + Method getCurrentPlatformEnumMethod = getCurrentPlatformMethod(oshiSystemInfoClass); getCurrentPlatformEnumMethod.invoke(null); } catch (Throwable ex) { // OK } } } + + private static Method getCurrentPlatformMethod(Class oshiSystemInfoClass) + throws NoSuchMethodException { + try { + return oshiSystemInfoClass.getMethod("getCurrentPlatformEnum"); + } catch (NoSuchMethodException exception) { + // renamed in oshi 6.0.0 + return oshiSystemInfoClass.getMethod("getCurrentPlatform"); + } + } } diff --git a/instrumentation/oshi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/oshi/SystemInfoInstrumentation.java b/instrumentation/oshi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/oshi/SystemInfoInstrumentation.java index c57e7f22a1c8..02e8f127a3df 100644 --- a/instrumentation/oshi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/oshi/SystemInfoInstrumentation.java +++ b/instrumentation/oshi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/oshi/SystemInfoInstrumentation.java @@ -32,7 +32,10 @@ public ElementMatcher typeMatcher() { @Override public void transform(TypeTransformer transformer) { transformer.applyAdviceToMethod( - isMethod().and(isPublic()).and(isStatic()).and(named("getCurrentPlatformEnum")), + isMethod() + .and(isPublic()) + .and(isStatic()) + .and(named("getCurrentPlatformEnum").or(named("getCurrentPlatform"))), this.getClass().getName() + "$GetCurrentPlatformEnumAdvice"); }