From e29d98bcf8ca7db1f4820a3a17ccca0e842c767d Mon Sep 17 00:00:00 2001 From: Pastrana Date: Fri, 6 Sep 2024 12:14:50 -0400 Subject: [PATCH] HPCC4J-612 WIP - Adds util functions to detect use of otel javaagent - Adds logic to optionally bypass manual initialization of otelsdk Signed-off-by: Pastrana --- pom.xml | 5 +++ .../hpccsystems/ws/client/utils/Utils.java | 44 +++++++++++++++++++ .../client/platform/test/PlatformTester.java | 20 +++++---- 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/pom.xml b/pom.xml index 76111f10a..9dc4b6b2d 100644 --- a/pom.xml +++ b/pom.xml @@ -135,6 +135,11 @@ io.opentelemetry opentelemetry-exporter-otlp + + io.opentelemetry.instrumentation + opentelemetry-instrumentation-annotations + 2.6.0 + io.opentelemetry.semconv 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..92d3e6ec0 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,47 @@ static public String getTraceParentHeader(Span span) return traceparent; } + + static public boolean containsVMArgument(String vmArgName) + { + List argslist = ManagementFactory.getRuntimeMXBean().getInputArguments(); + for (String string : argslist) + { + if(string.matches("(?i)(" + vmArgName + "):.*")) + { + return true; + } + } + return false; + } + + 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 ""; + } + + static public boolean isOtelJavaagentUsed() + { + String javaAgentPath = fetchVMArgument("-javaagent"); + if (!javaAgentPath.isEmpty()) + { + System.out.println("javaagent VM argument detected: " + javaAgentPath); + if (javaAgentPath.contains("opentelemetry")) + { + System.out.println("Otel javaagent argument detected: " + javaAgentPath); + return true; + } + } + return false; + } } 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..55e218350 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,25 @@ 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(); - } - else - { - globalOTel = GlobalOpenTelemetry.get(); + if (Utils.isOtelJavaagentUsed()) + { + System.out.println("Otel javaagent enabled, will not initialize Otel SDK"); + globalOTel = GlobalOpenTelemetry.get(); + } + else + { + globalOTel = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk(); + } } - 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);