40
40
import org .w3c .dom .Node ;
41
41
import org .w3c .dom .NodeList ;
42
42
43
+ import io .opentelemetry .api .GlobalOpenTelemetry ;
44
+ import io .opentelemetry .api .OpenTelemetry ;
45
+ import io .opentelemetry .api .trace .Span ;
46
+ import io .opentelemetry .api .trace .SpanBuilder ;
47
+ import io .opentelemetry .api .trace .SpanKind ;
48
+ import io .opentelemetry .api .trace .StatusCode ;
49
+ import io .opentelemetry .api .trace .Tracer ;
50
+ import io .opentelemetry .api .trace .propagation .W3CTraceContextPropagator ;
51
+ import io .opentelemetry .context .Context ;
52
+ import io .opentelemetry .context .Scope ;
53
+ import io .opentelemetry .semconv .HttpAttributes ;
54
+ import io .opentelemetry .semconv .ServerAttributes ;
55
+
43
56
/**
44
- * Defines functionality common to all HPCC Systmes web service clients.
57
+ * Defines functionality common to all HPCC Systems web service clients.
45
58
*
46
59
* Typically implemented by specialized HPCC Web service clients.
47
60
*/
48
61
public abstract class BaseHPCCWsClient extends DataSingleton
49
62
{
63
+ public static final String PROJECT_NAME = "WsClient" ;
64
+ private static OpenTelemetry globalOTel = null ;
50
65
/** Constant <code>log</code> */
51
66
protected static final Logger log = LogManager .getLogger (BaseHPCCWsClient .class );
52
67
/** Constant <code>DEAFULTECLWATCHPORT="8010"</code> */
@@ -164,6 +179,53 @@ private String getTargetHPCCBuildVersionString() throws Exception
164
179
165
180
}
166
181
182
+ public SpanBuilder getWsClientSpanBuilder (String spanName )
183
+ {
184
+ SpanBuilder spanBuilder = getWsClientTracer ().spanBuilder (spanName )
185
+ .setAttribute (ServerAttributes .SERVER_ADDRESS , wsconn .getHost ())
186
+ .setAttribute (ServerAttributes .SERVER_PORT , Long .getLong (wsconn .getPort ()))
187
+ .setAttribute (HttpAttributes .HTTP_REQUEST_METHOD , HttpAttributes .HttpRequestMethodValues .GET )
188
+ .setSpanKind (SpanKind .CLIENT );
189
+
190
+ return spanBuilder ;
191
+ }
192
+
193
+ static public void injectCurrentSpanTraceParentHeader (Stub clientStub )
194
+ {
195
+ if (clientStub != null )
196
+ {
197
+ injectCurrentSpanTraceParentHeader (clientStub ._getServiceClient ().getOptions ());
198
+ }
199
+ }
200
+
201
+ static public void injectCurrentSpanTraceParentHeader (Options options )
202
+ {
203
+ if (options != null )
204
+ {
205
+ W3CTraceContextPropagator .getInstance ().inject (Context .current (), options , Options ::setProperty );
206
+ }
207
+ }
208
+
209
+ /**
210
+ * Performs all Otel initialization
211
+ */
212
+ private void initOTel ()
213
+ {
214
+ /*
215
+ * If using the OpenTelemetry SDK, you may want to instantiate the OpenTelemetry toprovide configuration, for example of Resource or Sampler. See OpenTelemetrySdk and OpenTelemetrySdk.builder for information on how to construct theSDK's OpenTelemetry implementation.
216
+ * WARNING: Due to the inherent complications around initialization order involving this classand its single global instance, we strongly recommend *not* using GlobalOpenTelemetry unless youhave a use-case that absolutely requires it. Please favor using instances of OpenTelemetrywherever possible.
217
+ * If you are using the OpenTelemetry javaagent, it is generally best to only callGlobalOpenTelemetry.get() once, and then pass the resulting reference where you need to use it.
218
+ */
219
+ globalOTel = GlobalOpenTelemetry .get ();
220
+ }
221
+
222
+ public Tracer getWsClientTracer ()
223
+ {
224
+ if (globalOTel == null )
225
+ initOTel ();
226
+
227
+ return globalOTel .getTracer (PROJECT_NAME );
228
+ }
167
229
/**
168
230
* All instances of HPCCWsXYZClient should utilize this init function
169
231
* Attempts to establish the target HPCC build version and its container mode
@@ -175,36 +237,55 @@ private String getTargetHPCCBuildVersionString() throws Exception
175
237
*/
176
238
protected boolean initBaseWsClient (Connection connection , boolean fetchVersionAndContainerMode )
177
239
{
240
+ initOTel ();
241
+
178
242
boolean success = true ;
179
243
initErrMessage = "" ;
180
244
setActiveConnectionInfo (connection );
181
245
182
246
if (fetchVersionAndContainerMode )
183
247
{
184
- try
248
+ Span fetchHPCCVerSpan = getWsClientSpanBuilder ("FetchHPCCVersion" ).setSpanKind (SpanKind .INTERNAL ).startSpan ();
249
+ try (Scope scope = fetchHPCCVerSpan .makeCurrent ())
185
250
{
186
- targetHPCCBuildVersion = new Version (getTargetHPCCBuildVersionString ());
251
+ try
252
+ {
253
+ targetHPCCBuildVersion = new Version (getTargetHPCCBuildVersionString ());
254
+ }
255
+ catch (Exception e )
256
+ {
257
+ initErrMessage = "BaseHPCCWsClient: Could not stablish target HPCC bulid version, review all HPCC connection values" ;
258
+ if (!e .getLocalizedMessage ().isEmpty ())
259
+ initErrMessage = initErrMessage + "\n " + e .getLocalizedMessage ();
260
+ success = false ;
261
+ }
187
262
}
188
- catch ( Exception e )
263
+ finally
189
264
{
190
- initErrMessage = "BaseHPCCWsClient: Could not stablish target HPCC bulid version, review all HPCC connection values" ;
191
- if (!e .getLocalizedMessage ().isEmpty ())
192
- initErrMessage = initErrMessage + "\n " + e .getLocalizedMessage ();
193
-
194
- success = false ;
265
+ fetchHPCCVerSpan .setStatus (success ? StatusCode .OK : StatusCode .ERROR , initErrMessage );
266
+ fetchHPCCVerSpan .end ();
195
267
}
196
268
197
- try
269
+ Span fetchHPCCContainerMode = getWsClientSpanBuilder ("FetchHPCCContainerMode" ).startSpan ();
270
+ try (Scope scope = fetchHPCCContainerMode .makeCurrent ())
198
271
{
199
- targetsContainerizedHPCC = getTargetHPCCIsContainerized (wsconn );
272
+ try
273
+ {
274
+ targetsContainerizedHPCC = getTargetHPCCIsContainerized (wsconn );
275
+ }
276
+ catch (Exception e )
277
+ {
278
+ initErrMessage = initErrMessage + "\n BaseHPCCWsClient: Could not determine target HPCC Containerization mode, review all HPCC connection values" ;
279
+ if (!e .getLocalizedMessage ().isEmpty ())
280
+ initErrMessage = initErrMessage + "\n " + e .getLocalizedMessage ();
281
+
282
+ success = false ;
283
+ }
200
284
}
201
- catch ( Exception e )
285
+ finally
202
286
{
203
- initErrMessage = initErrMessage + "\n BaseHPCCWsClient: Could not determine target HPCC Containerization mode, review all HPCC connection values" ;
204
- if (!e .getLocalizedMessage ().isEmpty ())
205
- initErrMessage = initErrMessage + "\n " + e .getLocalizedMessage ();
206
-
207
- success = false ;
287
+ fetchHPCCContainerMode .setStatus (success ? StatusCode .OK : StatusCode .ERROR , initErrMessage );
288
+ fetchHPCCContainerMode .end ();
208
289
}
209
290
}
210
291
if (!initErrMessage .isEmpty ())
@@ -401,7 +482,10 @@ public String getInitError()
401
482
protected Stub verifyStub () throws Exception
402
483
{
403
484
if (stub != null )
485
+ {
486
+ injectCurrentSpanTraceParentHeader (stub );
404
487
return stub ;
488
+ }
405
489
else
406
490
throw new Exception ("WS Client Stub not available." + (hasInitError () ? "\n " + initErrMessage : "" ));
407
491
}
@@ -687,6 +771,7 @@ protected void handleEspExceptions(ArrayOfEspExceptionWrapper exp, String messag
687
771
if (message != null && !message .isEmpty ()) exp .setWsClientMessage (message );
688
772
689
773
log .error (exp .toString ());
774
+
690
775
throw exp ;
691
776
}
692
777
0 commit comments