Skip to content

Commit

Permalink
HPCC4J-612 WIP
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 6, 2024
1 parent 1c5a18d commit e29d98b
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 8 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-annotations</artifactId>
<version>2.6.0</version>
</dependency>
<dependency>
<!-- Not managed by opentelemetry-bom -->
<groupId>io.opentelemetry.semconv</groupId>
Expand Down
44 changes: 44 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,47 @@ static public String getTraceParentHeader(Span span)

return traceparent;
}

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;
}

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 "";
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit e29d98b

Please sign in to comment.