diff --git a/ejb3/src/main/java/org/jboss/as/ejb3/remote/AssociationImpl.java b/ejb3/src/main/java/org/jboss/as/ejb3/remote/AssociationImpl.java index 8f3b95932d53..5ee8210c351c 100644 --- a/ejb3/src/main/java/org/jboss/as/ejb3/remote/AssociationImpl.java +++ b/ejb3/src/main/java/org/jboss/as/ejb3/remote/AssociationImpl.java @@ -37,6 +37,7 @@ import org.jboss.as.ejb3.deployment.EjbDeploymentInformation; import org.jboss.as.ejb3.deployment.ModuleDeployment; import org.jboss.as.ejb3.logging.EjbLogger; +import org.jboss.as.ejb3.remote.tracing.TracingHelper; import org.jboss.as.network.ClientMapping; import org.jboss.as.security.remoting.RemoteConnection; import org.jboss.ejb.client.Affinity; @@ -91,7 +92,6 @@ import io.opentracing.Span; import io.opentracing.SpanContext; import io.opentracing.propagation.Format; -import io.opentracing.propagation.TextMapAdapter; import io.opentracing.util.GlobalTracer; /** @@ -208,7 +208,7 @@ public SecurityIdentity getSecurityIdentity() { } Span s = wrapContextAroundNewSpan(retrieveContextFromAttachments(attachments)); - try(Scope _s = GlobalTracer.get().activateSpan(s)) { + try(Scope _s = TracingHelper.activateSpan(s)) { final Map contextDataHolder = new HashMap<>(); result = invokeMethod(componentView, invokedMethod, invocationRequest, requestContent, cancellationFlag, contextDataHolder); attachments.putAll(contextDataHolder); @@ -272,7 +272,7 @@ private SpanContext retrieveContextFromAttachments(Map attachmen stringAttachments.put(e.getKey(), (String) e.getValue()); } } - return GlobalTracer.get().extract(Format.Builtin.TEXT_MAP, new TextMapAdapter(stringAttachments)); + return GlobalTracer.get().extract(Format.Builtin.TEXT_MAP, TracingHelper.textMapAdaptor(stringAttachments)); } private void updateAffinities(InvocationRequest invocationRequest, Map attachments, EJBLocator ejbLocator, ComponentView componentView) { diff --git a/ejb3/src/main/java/org/jboss/as/ejb3/remote/tracing/TracingHelper.java b/ejb3/src/main/java/org/jboss/as/ejb3/remote/tracing/TracingHelper.java new file mode 100644 index 000000000000..1628bd7fd3e2 --- /dev/null +++ b/ejb3/src/main/java/org/jboss/as/ejb3/remote/tracing/TracingHelper.java @@ -0,0 +1,66 @@ +package org.jboss.as.ejb3.remote.tracing; + +import io.opentracing.Scope; +import io.opentracing.ScopeManager; +import io.opentracing.Span; +import io.opentracing.propagation.TextMap; +import io.opentracing.propagation.TextMapExtractAdapter; +import io.opentracing.util.GlobalTracer; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.Map; + +public class TracingHelper { + + private static boolean usesDeprecatedApi = false; + private static Constructor textMapAdapterCtor; + private static Method scopeManagerActivateMethod; + + static { + try { + for(Method m : ScopeManager.class.getDeclaredMethods()) { + if(m.getName().equals("activate")) { + scopeManagerActivateMethod = m; + break; + } + } + if(scopeManagerActivateMethod == null) { + throw new IllegalStateException("Could not find ScopeManager#activate method."); + } + if(scopeManagerActivateMethod.getParameterCount() == 2) { + usesDeprecatedApi = true; + } + if(!usesDeprecatedApi) { + textMapAdapterCtor = ClassLoader.getSystemClassLoader() + .loadClass("io.opentracing.propagation.TextMapAdapter") + .getConstructors()[0]; + } else { + textMapAdapterCtor = TextMapExtractAdapter.class.getConstructors()[0]; + } + } catch (ClassNotFoundException e) { + throw new RuntimeException("WildFly does not use deprecated OpenTracing API but the class " + + "io.opentracing.propagation.TextMapAdapter was not found on the classpath.", e); + } + } + + public static Scope activateSpan(Span s) { + try { + if(usesDeprecatedApi) { + return (Scope) scopeManagerActivateMethod.invoke(GlobalTracer.get().scopeManager(), s, false); + } + return (Scope) scopeManagerActivateMethod.invoke(GlobalTracer.get().scopeManager(), s); + } catch (IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } + + public static TextMap textMapAdaptor(Map stringAttachments) { + try { + return (TextMap) textMapAdapterCtor.newInstance(stringAttachments); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } +} diff --git a/feature-pack/src/main/resources/modules/system/layers/base/org/jboss/jts/main/module.xml b/feature-pack/src/main/resources/modules/system/layers/base/org/jboss/jts/main/module.xml index ed82f7ac33a8..9de6f6558b81 100644 --- a/feature-pack/src/main/resources/modules/system/layers/base/org/jboss/jts/main/module.xml +++ b/feature-pack/src/main/resources/modules/system/layers/base/org/jboss/jts/main/module.xml @@ -57,7 +57,7 @@ - + diff --git a/pom.xml b/pom.xml index aadd90807767..42e152a1d78a 100644 --- a/pom.xml +++ b/pom.xml @@ -288,7 +288,7 @@ 1.3 0.34.3 4.1.48.Final - 0.32.0 + 0.31.0 0.2.1 0.0.4 0.4.1 diff --git a/testsuite/integration/basic/src/test/java/org/wildfly/test/integration/microprofile/opentracing/SimpleRestClientTestCase.java b/testsuite/integration/basic/src/test/java/org/wildfly/test/integration/microprofile/opentracing/SimpleRestClientTestCase.java index 73e7e532342e..fae80916f3f6 100644 --- a/testsuite/integration/basic/src/test/java/org/wildfly/test/integration/microprofile/opentracing/SimpleRestClientTestCase.java +++ b/testsuite/integration/basic/src/test/java/org/wildfly/test/integration/microprofile/opentracing/SimpleRestClientTestCase.java @@ -3,6 +3,7 @@ import static org.jboss.as.test.shared.integration.ejb.security.PermissionUtils.createPermissionsXmlAsset; import io.opentracing.Scope; +import io.opentracing.Span; import io.opentracing.Tracer; import io.opentracing.contrib.tracerresolver.TracerFactory; import io.opentracing.mock.MockSpan; @@ -15,6 +16,7 @@ import org.wildfly.test.integration.microprofile.opentracing.application.OpenTracingApplication; import org.wildfly.test.integration.microprofile.opentracing.application.TracedEndpoint; import org.jboss.as.test.shared.TestSuiteEnvironment; +import org.jboss.as.ejb3.remote.tracing.TracingHelper; import org.jboss.shrinkwrap.api.Archive; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.EmptyAsset; @@ -75,7 +77,8 @@ public void clientRequestSpanJoinsServer() { // test // the first span - try (Scope ignored = tracer.buildSpan("existing-span").startActive(true)) { + Span s = tracer.buildSpan("existing-span").start(); + try (Scope ignored = TracingHelper.activateSpan(s)) { // the second span is the client request, as a child of `existing-span` Client restClient = ClientTracingRegistrar.configure(ClientBuilder.newBuilder()).build(); @@ -88,6 +91,8 @@ public void clientRequestSpanJoinsServer() { // just a sanity check Assert.assertEquals(200, response.getStatus()); } + } finally { + s.finish(); } // verify