Skip to content

Commit

Permalink
HPCC4J-612 Ensure proper OTel SDK initialization
Browse files Browse the repository at this point in the history
- Adds util functions to detect use of otel javaagent
- Adds logic to optionally bypass manual initialization of otelsdk

Signed-off-by: Pastrana <[email protected]>
  • Loading branch information
Pastrana authored and Pastrana committed Sep 10, 2024
1 parent 1c5a18d commit 1e63cf6
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 9 deletions.
75 changes: 75 additions & 0 deletions wsclient/src/main/java/org/hpccsystems/ws/client/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> 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<String> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1e63cf6

Please sign in to comment.