From 1e63cf6155b631d213cd3b33cdfe49eeb7a3382b Mon Sep 17 00:00:00 2001 From: Pastrana Date: Fri, 6 Sep 2024 12:14:50 -0400 Subject: [PATCH] HPCC4J-612 Ensure proper OTel SDK initialization - Adds util functions to detect use of otel javaagent - Adds logic to optionally bypass manual initialization of otelsdk Signed-off-by: Pastrana --- .../hpccsystems/ws/client/utils/Utils.java | 75 +++++++++++++++++++ .../hpccsystems/ws/client/BaseRemoteTest.java | 9 ++- .../client/platform/test/PlatformTester.java | 16 ++-- 3 files changed, 91 insertions(+), 9 deletions(-) diff --git a/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Utils.java b/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Utils.java index 9db3f3145..96239616f 100644 --- a/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Utils.java +++ b/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Utils.java @@ -8,6 +8,7 @@ import java.io.OutputStream; import java.io.PrintStream; import java.io.StringWriter; +import java.lang.management.ManagementFactory; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; @@ -1261,4 +1262,78 @@ static public String getTraceParentHeader(Span span) return traceparent; } + + /** + * Checks if a specified VM argument is present. + * + * This method retrieves the list of VM arguments and searches for the specified argument name. + * If the argument is found, it returns true. If the argument is not found, it returns false. + * + * @param vmArgName the name of the VM argument to search for + * @return {@code true} if the specified VM argument is present, {@code false} otherwise + */ + static public boolean containsVMArgument(String vmArgName) + { + List argslist = ManagementFactory.getRuntimeMXBean().getInputArguments(); + for (String string : argslist) + { + if(string.matches("(?i)(" + vmArgName + "):.*")) + { + return true; + } + } + return false; + } + + /** + * Fetches the value of a specified VM argument. + * + * This method retrieves the list of VM arguments and searches for the specified argument name. + * If the argument is found, it returns the value associated with it. If the argument is not found, + * it returns an empty string. + * + * @param vmArgName the name of the VM argument to search for + * @return the value of the specified VM argument, or an empty string if the argument is not found + */ + static public String fetchVMArgument(String vmArgName) + { + List argslist = ManagementFactory.getRuntimeMXBean().getInputArguments(); + for (String string : argslist) + { + if(string.matches("(?i)(" + vmArgName + "):.*")) + { + String[] keyval = string.split(vmArgName+":"); + + return keyval[1]; + } + } + return ""; + } + + /** + * Checks if the OpenTelemetry Java agent is used by inspecting the VM arguments. + * + * This method fetches the VM argument specified by "-javaagent" and checks if it contains + * the term "opentelemetry". If the argument is found and contains "opentelemetry", it returns true. + * Otherwise, it returns false. + * + * @return {@code true} if the OpenTelemetry Java agent is detected, {@code false} otherwise. + */ + static public boolean isOtelJavaagentUsed() + { + String javaAgentPath = fetchVMArgument("-javaagent"); + if (!javaAgentPath.isEmpty()) + { + System.out.println("javaagent VM argument detected: " + javaAgentPath); + + File jaFile = new File(javaAgentPath); + + if (jaFile.getName().contains("opentelemetry") || jaFile.getName().contains("otel")) + { + System.out.println("Otel javaagent argument detected: " + javaAgentPath); + return true; + } + } + return false; + } } diff --git a/wsclient/src/test/java/org/hpccsystems/ws/client/BaseRemoteTest.java b/wsclient/src/test/java/org/hpccsystems/ws/client/BaseRemoteTest.java index 077fba31e..9718e7215 100644 --- a/wsclient/src/test/java/org/hpccsystems/ws/client/BaseRemoteTest.java +++ b/wsclient/src/test/java/org/hpccsystems/ws/client/BaseRemoteTest.java @@ -33,6 +33,7 @@ HPCC SYSTEMS software Copyright (C) 2019 HPCC Systems®. import org.hpccsystems.ws.client.HPCCWsTopologyClient.TopologyGroupQueryKind; import org.hpccsystems.ws.client.platform.Platform; import org.hpccsystems.ws.client.utils.Connection; +import org.hpccsystems.ws.client.utils.Utils; import org.hpccsystems.ws.client.wrappers.gen.wstopology.TpGroupWrapper; import org.hpccsystems.ws.client.wrappers.wsworkunits.WorkunitWrapper; import org.junit.Assert; @@ -135,9 +136,13 @@ public static void initialize() throws Exception System.out.println(" otel.metrics.exporter: "+ System.getProperty("otel.metrics.exporter")); System.out.println(" OTEL_METRICS_EXPORTER Env var: " + System.getenv("OTEL_METRICS_EXPORTER")); - globalOTel = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); + if (!Utils.isOtelJavaagentUsed()) + { + globalOTel = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); + } } - else + + if (globalOTel == null) { globalOTel = GlobalOpenTelemetry.get(); } diff --git a/wsclient/src/test/java/org/hpccsystems/ws/client/platform/test/PlatformTester.java b/wsclient/src/test/java/org/hpccsystems/ws/client/platform/test/PlatformTester.java index 8596fb40b..d9d412152 100644 --- a/wsclient/src/test/java/org/hpccsystems/ws/client/platform/test/PlatformTester.java +++ b/wsclient/src/test/java/org/hpccsystems/ws/client/platform/test/PlatformTester.java @@ -190,21 +190,23 @@ else if (currentParam.matches(WSSQLPORTPATTERN)) System.out.println("If missing dependancies arise, test will halt!"); System.out.println(" otel.traces.exporter sys property: "+System.getProperty("otel.traces.exporter")); System.out.println(" OTEL_TRACES_EXPORTER Env var: " + System.getenv("OTEL_TRACES_EXPORTER")); - System.out.println(" OTEL_TRACES_SAMPLER Env var: " + System.getenv("OTEL_TRACES_SAMPLER")); - System.out.println(" otel.traces.sampler sys property: "+System.getProperty("otel.traces.sampler")); + System.out.println(" OTEL_TRACES_SAMPLER Env var: " + System.getenv("OTEL_TRACES_SAMPLER")); + System.out.println(" otel.traces.sampler sys property: "+System.getProperty("otel.traces.sampler")); System.out.println(" otel.logs.exporter: "+ System.getProperty("otel.logs.exporter")); System.out.println(" OTEL_LOGS_EXPORTER Env var: " + System.getenv("OTEL_LOGS_EXPORTER")); System.out.println(" otel.metrics.exporter: "+ System.getProperty("otel.metrics.exporter")); System.out.println(" OTEL_METRICS_EXPORTER Env var: " + System.getenv("OTEL_METRICS_EXPORTER")); - globalOTel = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); + if (!Utils.isOtelJavaagentUsed()) + { + globalOTel = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); + } } - else - { + + if (globalOTel == null) globalOTel = GlobalOpenTelemetry.get(); - } - Span rootSpan = globalOTel.getTracer("PlatformTester").spanBuilder("rootspan").startSpan(); + Span rootSpan = globalOTel.getTracer("PlatformTester").spanBuilder("PlatformTest").startSpan(); try (Scope scope = rootSpan.makeCurrent()) { Platform platform = Platform.get(prot, hpccServer, port, user, pass);