Skip to content

Commit 7050cb3

Browse files
rpastranaPastrana
andauthored
HPCC4J-612 Ensure proper OTel SDK initialization (#745)
Signed-off-by: Pastrana <[email protected]> Co-authored-by: Pastrana <[email protected]>
1 parent 1c5a18d commit 7050cb3

File tree

3 files changed

+91
-9
lines changed

3 files changed

+91
-9
lines changed

wsclient/src/main/java/org/hpccsystems/ws/client/utils/Utils.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.OutputStream;
99
import java.io.PrintStream;
1010
import java.io.StringWriter;
11+
import java.lang.management.ManagementFactory;
1112
import java.net.HttpURLConnection;
1213
import java.net.URL;
1314
import java.net.URLConnection;
@@ -1261,4 +1262,78 @@ static public String getTraceParentHeader(Span span)
12611262

12621263
return traceparent;
12631264
}
1265+
1266+
/**
1267+
* Checks if a specified VM argument is present.
1268+
*
1269+
* This method retrieves the list of VM arguments and searches for the specified argument name.
1270+
* If the argument is found, it returns true. If the argument is not found, it returns false.
1271+
*
1272+
* @param vmArgName the name of the VM argument to search for
1273+
* @return {@code true} if the specified VM argument is present, {@code false} otherwise
1274+
*/
1275+
static public boolean containsVMArgument(String vmArgName)
1276+
{
1277+
List<String> argslist = ManagementFactory.getRuntimeMXBean().getInputArguments();
1278+
for (String string : argslist)
1279+
{
1280+
if(string.matches("(?i)(" + vmArgName + "):.*"))
1281+
{
1282+
return true;
1283+
}
1284+
}
1285+
return false;
1286+
}
1287+
1288+
/**
1289+
* Fetches the value of a specified VM argument.
1290+
*
1291+
* This method retrieves the list of VM arguments and searches for the specified argument name.
1292+
* If the argument is found, it returns the value associated with it. If the argument is not found,
1293+
* it returns an empty string.
1294+
*
1295+
* @param vmArgName the name of the VM argument to search for
1296+
* @return the value of the specified VM argument, or an empty string if the argument is not found
1297+
*/
1298+
static public String fetchVMArgument(String vmArgName)
1299+
{
1300+
List<String> argslist = ManagementFactory.getRuntimeMXBean().getInputArguments();
1301+
for (String string : argslist)
1302+
{
1303+
if(string.matches("(?i)(" + vmArgName + "):.*"))
1304+
{
1305+
String[] keyval = string.split(vmArgName+":");
1306+
1307+
return keyval[1];
1308+
}
1309+
}
1310+
return "";
1311+
}
1312+
1313+
/**
1314+
* Checks if the OpenTelemetry Java agent is used by inspecting the VM arguments.
1315+
*
1316+
* This method fetches the VM argument specified by "-javaagent" and checks if it contains
1317+
* the term "opentelemetry". If the argument is found and contains "opentelemetry", it returns true.
1318+
* Otherwise, it returns false.
1319+
*
1320+
* @return {@code true} if the OpenTelemetry Java agent is detected, {@code false} otherwise.
1321+
*/
1322+
static public boolean isOtelJavaagentUsed()
1323+
{
1324+
String javaAgentPath = fetchVMArgument("-javaagent");
1325+
if (!javaAgentPath.isEmpty())
1326+
{
1327+
System.out.println("javaagent VM argument detected: " + javaAgentPath);
1328+
1329+
File jaFile = new File(javaAgentPath);
1330+
1331+
if (jaFile.getName().contains("opentelemetry") || jaFile.getName().contains("otel"))
1332+
{
1333+
System.out.println("Otel javaagent argument detected: " + javaAgentPath);
1334+
return true;
1335+
}
1336+
}
1337+
return false;
1338+
}
12641339
}

wsclient/src/test/java/org/hpccsystems/ws/client/BaseRemoteTest.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ HPCC SYSTEMS software Copyright (C) 2019 HPCC Systems®.
3333
import org.hpccsystems.ws.client.HPCCWsTopologyClient.TopologyGroupQueryKind;
3434
import org.hpccsystems.ws.client.platform.Platform;
3535
import org.hpccsystems.ws.client.utils.Connection;
36+
import org.hpccsystems.ws.client.utils.Utils;
3637
import org.hpccsystems.ws.client.wrappers.gen.wstopology.TpGroupWrapper;
3738
import org.hpccsystems.ws.client.wrappers.wsworkunits.WorkunitWrapper;
3839
import org.junit.Assert;
@@ -135,9 +136,13 @@ public static void initialize() throws Exception
135136
System.out.println(" otel.metrics.exporter: "+ System.getProperty("otel.metrics.exporter"));
136137
System.out.println(" OTEL_METRICS_EXPORTER Env var: " + System.getenv("OTEL_METRICS_EXPORTER"));
137138

138-
globalOTel = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
139+
if (!Utils.isOtelJavaagentUsed())
140+
{
141+
globalOTel = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
142+
}
139143
}
140-
else
144+
145+
if (globalOTel == null)
141146
{
142147
globalOTel = GlobalOpenTelemetry.get();
143148
}

wsclient/src/test/java/org/hpccsystems/ws/client/platform/test/PlatformTester.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,23 @@ else if (currentParam.matches(WSSQLPORTPATTERN))
190190
System.out.println("If missing dependancies arise, test will halt!");
191191
System.out.println(" otel.traces.exporter sys property: "+System.getProperty("otel.traces.exporter"));
192192
System.out.println(" OTEL_TRACES_EXPORTER Env var: " + System.getenv("OTEL_TRACES_EXPORTER"));
193-
System.out.println(" OTEL_TRACES_SAMPLER Env var: " + System.getenv("OTEL_TRACES_SAMPLER"));
194-
System.out.println(" otel.traces.sampler sys property: "+System.getProperty("otel.traces.sampler"));
193+
System.out.println(" OTEL_TRACES_SAMPLER Env var: " + System.getenv("OTEL_TRACES_SAMPLER"));
194+
System.out.println(" otel.traces.sampler sys property: "+System.getProperty("otel.traces.sampler"));
195195
System.out.println(" otel.logs.exporter: "+ System.getProperty("otel.logs.exporter"));
196196
System.out.println(" OTEL_LOGS_EXPORTER Env var: " + System.getenv("OTEL_LOGS_EXPORTER"));
197197
System.out.println(" otel.metrics.exporter: "+ System.getProperty("otel.metrics.exporter"));
198198
System.out.println(" OTEL_METRICS_EXPORTER Env var: " + System.getenv("OTEL_METRICS_EXPORTER"));
199199

200-
globalOTel = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
200+
if (!Utils.isOtelJavaagentUsed())
201+
{
202+
globalOTel = AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk();
203+
}
201204
}
202-
else
203-
{
205+
206+
if (globalOTel == null)
204207
globalOTel = GlobalOpenTelemetry.get();
205-
}
206208

207-
Span rootSpan = globalOTel.getTracer("PlatformTester").spanBuilder("rootspan").startSpan();
209+
Span rootSpan = globalOTel.getTracer("PlatformTester").spanBuilder("PlatformTest").startSpan();
208210
try (Scope scope = rootSpan.makeCurrent())
209211
{
210212
Platform platform = Platform.get(prot, hpccServer, port, user, pass);

0 commit comments

Comments
 (0)