Skip to content

Commit

Permalink
HPCC-586 WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Rodrigo Pastrana <[email protected]>
  • Loading branch information
rpastrana committed May 31, 2024
1 parent 45f0071 commit 60cf384
Show file tree
Hide file tree
Showing 5 changed files with 370 additions and 69 deletions.
42 changes: 42 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<groups>org.hpccsystems.commons.annotations.BaseTests</groups>
<codehaus.template.version>1.0.0</codehaus.template.version>
<project.benchmarking>false</project.benchmarking>
<opentelemetry.version>2.4.0-alpha</opentelemetry.version>
</properties>

<scm>
Expand Down Expand Up @@ -99,7 +100,48 @@
</repository>
</repositories>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-bom</artifactId>
<version>1.38.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-api</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-logging</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-autoconfigure-spi</artifactId>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-exporter-otlp</artifactId>
</dependency>
<dependency>
<!-- Not managed by opentelemetry-bom -->
<groupId>io.opentelemetry.semconv</groupId>
<artifactId>opentelemetry-semconv</artifactId>
<version>1.25.0-alpha</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
202 changes: 185 additions & 17 deletions wsclient/src/main/java/org/hpccsystems/ws/client/BaseHPCCWsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.rmi.RemoteException;
import java.text.Format;
import java.util.HashMap;
import java.util.Map;
import java.util.function.IntConsumer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
Expand All @@ -27,6 +32,11 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hpccsystems.ws.client.gen.axis2.wsworkunits.latest.ECLWorkunit;
import org.hpccsystems.ws.client.gen.axis2.wsworkunits.latest.EspSoapFault;
import org.hpccsystems.ws.client.gen.axis2.wsworkunits.latest.WUQuery;
import org.hpccsystems.ws.client.gen.axis2.wsworkunits.latest.WUQueryResponse;
import org.hpccsystems.ws.client.gen.axis2.wsworkunits.latest.WsWorkunits;
import org.hpccsystems.ws.client.platform.Version;
import org.hpccsystems.ws.client.utils.Connection;
import org.hpccsystems.ws.client.utils.DataSingleton;
Expand All @@ -36,17 +46,45 @@
import org.hpccsystems.ws.client.wrappers.ArrayOfECLExceptionWrapper;
import org.hpccsystems.ws.client.wrappers.ArrayOfEspExceptionWrapper;
import org.hpccsystems.ws.client.wrappers.EspSoapFaultWrapper;
import org.hpccsystems.ws.client.wrappers.WUState;
import org.hpccsystems.ws.client.wrappers.wsworkunits.WorkunitWrapper;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.opentelemetry.semconv.HttpAttributes;
import io.opentelemetry.semconv.ServerAttributes;

/**
* Defines functionality common to all HPCC Systmes web service clients.
*
* Typically implemented by specialized HPCC Web service clients.
*/
public abstract class BaseHPCCWsClient extends DataSingleton
{
public static final String INSTRUMENTED_LIBRARY_NAME = "WsClient";
private Tracer tracer = null;
/** Constant <code>log</code> */
protected static final Logger log = LogManager.getLogger(BaseHPCCWsClient.class);
/** Constant <code>DEAFULTECLWATCHPORT="8010"</code> */
Expand Down Expand Up @@ -164,6 +202,89 @@ private String getTargetHPCCBuildVersionString() throws Exception

}

public SpanBuilder getSpanBuilder(String spanName)
{
Tracer tracer = getTracer(INSTRUMENTED_LIBRARY_NAME);
SpanBuilder spanBuilder = tracer.spanBuilder(spanName)
.setAttribute(ServerAttributes.SERVER_ADDRESS, wsconn.getHost())
.setAttribute(ServerAttributes.SERVER_PORT, Long.getLong(wsconn.getPort()))
.setAttribute(HttpAttributes.HTTP_REQUEST_METHOD, HttpAttributes.HttpRequestMethodValues.GET)
.setSpanKind(SpanKind.CLIENT);
//.startSpan();

return spanBuilder;
}

public void injectTraceParentHeader(Span currentSpan)
{
injectTraceParentHeader(stub._getServiceClient().getOptions(), currentSpan);
}

static public void injectTraceParentHeader(Options options, Span currentSpan)
{
if (options != null)
{
if (currentSpan != null && currentSpan.getSpanContext().isValid())
W3CTraceContextPropagator.getInstance().inject(Context.current(), options, Options::setProperty);
}
}

static public void injectCurrentSpanTraceParentHeader(Stub clientStub)
{
if (clientStub != null)
{
injectCurrentSpanTraceParentHeader(clientStub._getServiceClient().getOptions());
}
}

static public void injectCurrentSpanTraceParentHeader(Options options)
{
if (options != null)
{
injectTraceParentHeader(options, Span.current());
}
}

static public String getTraceParentHeader()
{
String traceparent = null;
Span currentSpan = Span.current();
if (currentSpan != null && currentSpan.getSpanContext().isValid())
{
Map<String, String> carrier = new HashMap<>();
TextMapSetter<Map<String, String>> setter = Map::put;
W3CTraceContextPropagator.getInstance().inject(Context.current(), carrier, setter);

traceparent = carrier.getOrDefault("traceparent", "00-" + currentSpan.getSpanContext().getTraceId() + "-" + currentSpan.getSpanContext().getSpanId() + "-00");
carrier.clear();
}

return traceparent;
}

private void initOtel(String tracerName)
{
if (GlobalOpenTelemetry.get() == null)
{
GlobalOpenTelemetry.set(AutoConfiguredOpenTelemetrySdk.initialize().getOpenTelemetrySdk());
/*if we want to overwrite auto configuration:
openTelemetry = AutoConfiguredOpenTelemetrySdk.builder()
.addTracerProviderCustomizer(
(sdkTracerProviderBuilder, configProperties) ->
sdkTracerProviderBuilder.addSpanProcessor(
new SpanProcessor() { // implementation omitted for brevity
})).build();
*/
}
tracer = GlobalOpenTelemetry.getTracer(tracerName);
}

public Tracer getTracer(String tracerName)
{
if (tracer == null)
initOtel(tracerName);
return tracer;
}
/**
* All instances of HPCCWsXYZClient should utilize this init function
* Attempts to establish the target HPCC build version and its container mode
Expand All @@ -175,36 +296,54 @@ private String getTargetHPCCBuildVersionString() throws Exception
*/
protected boolean initBaseWsClient(Connection connection, boolean fetchVersionAndContainerMode)
{
initOtel(INSTRUMENTED_LIBRARY_NAME);

boolean success = true;
initErrMessage = "";
setActiveConnectionInfo(connection);

if (fetchVersionAndContainerMode)
{
try
Span fetchHPCCVerSpan = getSpanBuilder("FetchHPCCVersion").startSpan();
try (Scope scope = fetchHPCCVerSpan.makeCurrent())
{
targetHPCCBuildVersion = new Version(getTargetHPCCBuildVersionString());
try
{
targetHPCCBuildVersion = new Version(getTargetHPCCBuildVersionString());
}
catch (Exception e)
{
initErrMessage = "BaseHPCCWsClient: Could not stablish target HPCC bulid version, review all HPCC connection values";
if (!e.getLocalizedMessage().isEmpty())
initErrMessage = initErrMessage + "\n" + e.getLocalizedMessage();
success = false;
}
}
catch (Exception e)
finally
{
initErrMessage = "BaseHPCCWsClient: Could not stablish target HPCC bulid version, review all HPCC connection values";
if (!e.getLocalizedMessage().isEmpty())
initErrMessage = initErrMessage + "\n" + e.getLocalizedMessage();

success = false;
fetchHPCCVerSpan.setStatus(success ? StatusCode.OK : StatusCode.ERROR, initErrMessage);
fetchHPCCVerSpan.end();
}

try
Span fetchHPCCContainerMode = getSpanBuilder("FetchHPCCContainerMode").startSpan();
try (Scope scope = fetchHPCCContainerMode.makeCurrent())
{
targetsContainerizedHPCC = getTargetHPCCIsContainerized(wsconn);
try
{
targetsContainerizedHPCC = getTargetHPCCIsContainerized(wsconn);
}
catch (Exception e)
{
initErrMessage = initErrMessage + "\nBaseHPCCWsClient: Could not determine target HPCC Containerization mode, review all HPCC connection values";
if (!e.getLocalizedMessage().isEmpty())
initErrMessage = initErrMessage + "\n" + e.getLocalizedMessage();

success = false;
}
}
catch (Exception e)
finally
{
initErrMessage = initErrMessage + "\nBaseHPCCWsClient: Could not determine target HPCC Containerization mode, review all HPCC connection values";
if (!e.getLocalizedMessage().isEmpty())
initErrMessage = initErrMessage + "\n" + e.getLocalizedMessage();

success = false;
fetchHPCCContainerMode.setStatus(success ? StatusCode.OK : StatusCode.ERROR, initErrMessage);
fetchHPCCContainerMode.end();
}
}
if (!initErrMessage.isEmpty())
Expand Down Expand Up @@ -401,7 +540,12 @@ public String getInitError()
protected Stub verifyStub() throws Exception
{
if (stub != null)
{
Span currentSpan = Span.current();
if (currentSpan.getSpanContext().isValid())
injectTraceParentHeader(currentSpan);
return stub;
}
else
throw new Exception("WS Client Stub not available." + (hasInitError() ? "\n" + initErrMessage : ""));
}
Expand Down Expand Up @@ -582,6 +726,10 @@ static public Stub setStubOptions(Stub thestub, Connection connection) throws Ax

opt.setProperty(HTTPConstants.CHUNKED, Boolean.FALSE);

//only do this if tracing enabled?
//injectCurrentSpanTraceParentHeader(opt);
//opt.setProperty("traceparent", getTraceParentHeader());

if (connection.getPreemptiveHTTPAuthenticate())
{
//Axis2 now forces connection authenticate, even if target is not secure
Expand Down Expand Up @@ -676,12 +824,32 @@ protected void handleEspSoapFaults(EspSoapFaultWrapper e, String message) throws
* the array of esp exception wrapper
*/
protected void handleEspExceptions(ArrayOfEspExceptionWrapper exp, String message) throws ArrayOfEspExceptionWrapper
{
handleEspExceptions(exp, message, null);
}

/**
* Handle esp exceptions.
*
* @param exp
* the exp
* @param message
* the message
* @param span
* optional span. Resulting exception reported on span
* @throws org.hpccsystems.ws.client.wrappers.ArrayOfEspExceptionWrapper
* the array of esp exception wrapper
*/
protected void handleEspExceptions(ArrayOfEspExceptionWrapper exp, String message, Span span) throws ArrayOfEspExceptionWrapper
{
if (exp == null || exp.getExceptions() == null || exp.getExceptions().size() <= 0) return;

if (message != null && !message.isEmpty()) exp.setWsClientMessage(message);

log.error(exp.toString());
if (span != null && span.getSpanContext().isValid())
span.recordException(exp);

throw exp;
}

Expand Down
Loading

0 comments on commit 60cf384

Please sign in to comment.