Skip to content

Commit

Permalink
WFLY-10847 Set one tracer instance per WAR sub-deployment in EAR depl…
Browse files Browse the repository at this point in the history
…oyments

Signed-off-by: Juraci Paixão Kröhling <juraci@kroehling.de>
  • Loading branch information
jpkrohling committed Aug 21, 2018
1 parent a8e0bc8 commit f56413f
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 73 deletions.
Expand Up @@ -22,6 +22,8 @@


package org.wildfly.extension.microprofile.opentracing; package org.wildfly.extension.microprofile.opentracing;


import org.jboss.as.ee.structure.DeploymentType;
import org.jboss.as.ee.structure.DeploymentTypeMarker;
import org.jboss.as.server.deployment.Attachments; import org.jboss.as.server.deployment.Attachments;
import org.jboss.as.server.deployment.DeploymentPhaseContext; import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit; import org.jboss.as.server.deployment.DeploymentUnit;
Expand All @@ -43,6 +45,10 @@ public void deploy(DeploymentPhaseContext phaseContext) {
} }


private void addDependencies(DeploymentUnit deploymentUnit) { private void addDependencies(DeploymentUnit deploymentUnit) {
if (DeploymentTypeMarker.isType(DeploymentType.EAR, deploymentUnit)) {
return;
}

ModuleSpecification moduleSpecification = deploymentUnit.getAttachment(Attachments.MODULE_SPECIFICATION); ModuleSpecification moduleSpecification = deploymentUnit.getAttachment(Attachments.MODULE_SPECIFICATION);
ModuleLoader moduleLoader = Module.getBootModuleLoader(); ModuleLoader moduleLoader = Module.getBootModuleLoader();


Expand Down
Expand Up @@ -22,18 +22,17 @@


package org.wildfly.extension.microprofile.opentracing; package org.wildfly.extension.microprofile.opentracing;


import io.smallrye.opentracing.SmallRyeTracingDynamicFeature; import org.jboss.as.ee.structure.DeploymentType;
import org.jboss.as.ee.structure.DeploymentTypeMarker;
import org.jboss.as.ee.weld.WeldDeploymentMarker; import org.jboss.as.ee.weld.WeldDeploymentMarker;
import org.jboss.as.server.deployment.DeploymentPhaseContext; import org.jboss.as.server.deployment.DeploymentPhaseContext;
import org.jboss.as.server.deployment.DeploymentUnit; import org.jboss.as.server.deployment.DeploymentUnit;
import org.jboss.as.server.deployment.DeploymentUnitProcessor; import org.jboss.as.server.deployment.DeploymentUnitProcessor;
import org.jboss.as.web.common.WarMetaData; import org.jboss.as.web.common.WarMetaData;
import org.jboss.as.weld.deployment.WeldPortableExtensions;
import org.jboss.metadata.javaee.spec.ParamValueMetaData; import org.jboss.metadata.javaee.spec.ParamValueMetaData;
import org.jboss.metadata.web.jboss.JBossWebMetaData; import org.jboss.metadata.web.jboss.JBossWebMetaData;
import org.jboss.metadata.web.spec.ListenerMetaData; import org.jboss.metadata.web.spec.ListenerMetaData;
import org.wildfly.microprofile.opentracing.smallrye.TracerInitializer; import org.wildfly.microprofile.opentracing.smallrye.TracerInitializer;
import org.wildfly.microprofile.opentracing.smallrye.TracingCDIExtension;
import org.wildfly.security.manager.WildFlySecurityManager; import org.wildfly.security.manager.WildFlySecurityManager;


import java.util.ArrayList; import java.util.ArrayList;
Expand All @@ -45,19 +44,18 @@ public void deploy(DeploymentPhaseContext deploymentPhaseContext) {
TracingExtensionLogger.ROOT_LOGGER.processingDeployment(); TracingExtensionLogger.ROOT_LOGGER.processingDeployment();
DeploymentUnit deploymentUnit = deploymentPhaseContext.getDeploymentUnit(); DeploymentUnit deploymentUnit = deploymentPhaseContext.getDeploymentUnit();


if (DeploymentTypeMarker.isType(DeploymentType.EAR, deploymentUnit)) {
return;
}

if (!WeldDeploymentMarker.isPartOfWeldDeployment(deploymentUnit)) { if (!WeldDeploymentMarker.isPartOfWeldDeployment(deploymentUnit)) {
// SmallRye JAX-RS requires CDI. Without CDI, there's no integration needed // SmallRye JAX-RS requires CDI. Without CDI, there's no integration needed
TracingExtensionLogger.ROOT_LOGGER.noCdiDeployment(); TracingExtensionLogger.ROOT_LOGGER.noCdiDeployment();
return; return;
} }


setServiceName(deploymentUnit);
addListeners(deploymentUnit); addListeners(deploymentUnit);
addJaxRsIntegration(deploymentUnit);
addCDIExtension(deploymentUnit);
}

@Override
public void undeploy(DeploymentUnit deploymentUnit) {
} }


private void addListeners(DeploymentUnit deploymentUnit) { private void addListeners(DeploymentUnit deploymentUnit) {
Expand All @@ -69,12 +67,6 @@ private void addListeners(DeploymentUnit deploymentUnit) {


TracingExtensionLogger.ROOT_LOGGER.registeringTracerInitializer(); TracingExtensionLogger.ROOT_LOGGER.registeringTracerInitializer();


String serviceName = getServiceName(deploymentUnit);
ParamValueMetaData serviceNameContextParameter = new ParamValueMetaData();
serviceNameContextParameter.setParamName(TracerInitializer.SMALLRYE_OPENTRACING_SERVICE_NAME);
serviceNameContextParameter.setParamValue(serviceName);
addContextParameter(jbossWebMetaData, serviceNameContextParameter);

ListenerMetaData listenerMetaData = new ListenerMetaData(); ListenerMetaData listenerMetaData = new ListenerMetaData();
listenerMetaData.setListenerClass(TracerInitializer.class.getName()); listenerMetaData.setListenerClass(TracerInitializer.class.getName());


Expand All @@ -86,26 +78,18 @@ private void addListeners(DeploymentUnit deploymentUnit) {
jbossWebMetaData.setListeners(listeners); jbossWebMetaData.setListeners(listeners);
} }


private void addJaxRsIntegration(DeploymentUnit deploymentUnit) { private void setServiceName(DeploymentUnit deploymentUnit) {
JBossWebMetaData jbossWebMetaData = getJBossWebMetaData(deploymentUnit); JBossWebMetaData jbossWebMetaData = getJBossWebMetaData(deploymentUnit);
if (null == jbossWebMetaData) { if (null == jbossWebMetaData) {
// nothing to do here // nothing to do here
return; return;
} }


TracingExtensionLogger.ROOT_LOGGER.registeringJaxRs(); String serviceName = getServiceName(deploymentUnit);

ParamValueMetaData serviceNameContextParameter = new ParamValueMetaData();
ParamValueMetaData restEasyProvider = new ParamValueMetaData(); serviceNameContextParameter.setParamName(TracerInitializer.SMALLRYE_OPENTRACING_SERVICE_NAME);
restEasyProvider.setParamName("resteasy.providers"); serviceNameContextParameter.setParamValue(serviceName);
restEasyProvider.setParamValue(SmallRyeTracingDynamicFeature.class.getName()); addContextParameter(jbossWebMetaData, serviceNameContextParameter);
addContextParameter(jbossWebMetaData, restEasyProvider);
}

private void addCDIExtension(DeploymentUnit deploymentUnit) {
TracingExtensionLogger.ROOT_LOGGER.registeringCDIExtension();

WeldPortableExtensions extensions = WeldPortableExtensions.getPortableExtensions(deploymentUnit);
extensions.registerExtensionInstance(new TracingCDIExtension(), deploymentUnit);
} }


private JBossWebMetaData getJBossWebMetaData(DeploymentUnit deploymentUnit) { private JBossWebMetaData getJBossWebMetaData(DeploymentUnit deploymentUnit) {
Expand Down Expand Up @@ -134,10 +118,22 @@ private String getServiceName(DeploymentUnit deploymentUnit) {
} }


if (null == serviceName || serviceName.isEmpty()) { if (null == serviceName || serviceName.isEmpty()) {
if (null != deploymentUnit.getParent()) {
// application.ear!module.war
serviceName = deploymentUnit.getParent().getServiceName().getSimpleName()
+ "!"
+ deploymentUnit.getServiceName().getSimpleName();
} else {
serviceName = deploymentUnit.getServiceName().getSimpleName();
}

TracingExtensionLogger.ROOT_LOGGER.serviceNameDerivedFromDeploymentUnit(serviceName); TracingExtensionLogger.ROOT_LOGGER.serviceNameDerivedFromDeploymentUnit(serviceName);
serviceName = deploymentUnit.getServiceName().getSimpleName();
} }


return serviceName; return serviceName;
} }

@Override
public void undeploy(DeploymentUnit deploymentUnit) {
}
} }
Expand Up @@ -43,18 +43,10 @@ public interface TracingExtensionLogger extends BasicLogger {
@Message(id = 2, value = "MicroProfile OpenTracing Subsystem is processing deployment") @Message(id = 2, value = "MicroProfile OpenTracing Subsystem is processing deployment")
void processingDeployment(); void processingDeployment();


@LogMessage(level = DEBUG)
@Message(id = 3, value = "Registering SmallRye CDI Extension")
void registeringCDIExtension();

@LogMessage(level = DEBUG) @LogMessage(level = DEBUG)
@Message(id = 4, value = "The deployment does not have CDI enabled. Skipping MicroProfile OpenTracing integration.") @Message(id = 4, value = "The deployment does not have CDI enabled. Skipping MicroProfile OpenTracing integration.")
void noCdiDeployment(); void noCdiDeployment();


@LogMessage(level = DEBUG)
@Message(id = 5, value = "Registering MicroProfile OpenTracing JAX-RS integration")
void registeringJaxRs();

@LogMessage(level = DEBUG) @LogMessage(level = DEBUG)
@Message(id = 6, value = "Deriving service name based on the deployment unit's name: %s") @Message(id = 6, value = "Deriving service name based on the deployment unit's name: %s")
void serviceNameDerivedFromDeploymentUnit(String serviceName); void serviceNameDerivedFromDeploymentUnit(String serviceName);
Expand Down
4 changes: 4 additions & 0 deletions microprofile/opentracing-smallrye/pom.xml
Expand Up @@ -39,6 +39,10 @@
<name>WildFly: MicroProfile OpenTracing with SmallRye</name> <name>WildFly: MicroProfile OpenTracing with SmallRye</name>


<dependencies> <dependencies>
<dependency>
<groupId>org.jboss.spec.javax.ws.rs</groupId>
<artifactId>jboss-jaxrs-api_2.1_spec</artifactId>
</dependency>
<dependency> <dependency>
<groupId>io.opentracing.contrib</groupId> <groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-jaxrs2</artifactId> <artifactId>opentracing-jaxrs2</artifactId>
Expand Down
@@ -0,0 +1,63 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2018, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.wildfly.microprofile.opentracing.smallrye;

import io.opentracing.Tracer;
import io.opentracing.contrib.jaxrs2.server.OperationNameProvider;
import io.opentracing.contrib.jaxrs2.server.ServerTracingDynamicFeature;

import javax.servlet.ServletContext;
import javax.ws.rs.container.DynamicFeature;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;

@Provider
public class TracerDynamicFeature implements DynamicFeature {
@Context
ServletContext servletContext;

@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
Tracer tracer;

Object tracerObject = servletContext.getAttribute(TracerInitializer.SMALLRYE_OPENTRACING_TRACER);
if (tracerObject instanceof Tracer) {
tracer = (Tracer) tracerObject;
} else {
// should never happen, but if it does, there's something really wrong
// we log a warn-level message here then
TracingLogger.ROOT_LOGGER.noTracerAvailable();
return;
}

ServerTracingDynamicFeature delegate = new ServerTracingDynamicFeature.Builder(tracer)
.withOperationNameProvider(OperationNameProvider.ClassNameOperationName.newBuilder())
.withTraceSerialization(false)
.build();

delegate.configure(resourceInfo, context);
}

}
Expand Up @@ -29,10 +29,9 @@
import io.opentracing.noop.NoopTracerFactory; import io.opentracing.noop.NoopTracerFactory;
import io.opentracing.util.GlobalTracer; import io.opentracing.util.GlobalTracer;


import javax.enterprise.event.Event;
import javax.inject.Inject;
import javax.servlet.DispatcherType; import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration; import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener; import javax.servlet.annotation.WebListener;
Expand All @@ -41,18 +40,10 @@
@WebListener @WebListener
public class TracerInitializer implements ServletContextListener { public class TracerInitializer implements ServletContextListener {
public static final String SMALLRYE_OPENTRACING_SERVICE_NAME = "smallrye.opentracing.serviceName"; public static final String SMALLRYE_OPENTRACING_SERVICE_NAME = "smallrye.opentracing.serviceName";

public static final String SMALLRYE_OPENTRACING_TRACER = "smallrye.opentracing.tracer";
@Inject
Event<Tracer> tracerInitialized;


@Override @Override
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
if (null == tracerInitialized) {
// Weld integration problems? This happens with the test org.jboss.as.test.integration.ws.injection.ejb.basic.InjectionTestCase
TracingLogger.ROOT_LOGGER.noCdiEventSupport();
return;
}

if (GlobalTracer.isRegistered()) { if (GlobalTracer.isRegistered()) {
TracingLogger.ROOT_LOGGER.alreadyRegistered(); TracingLogger.ROOT_LOGGER.alreadyRegistered();
return; return;
Expand All @@ -72,12 +63,16 @@ public void contextInitialized(ServletContextEvent sce) {
} }


TracingLogger.ROOT_LOGGER.registeringTracer(tracer.getClass().getName()); TracingLogger.ROOT_LOGGER.registeringTracer(tracer.getClass().getName());
tracerInitialized.fire(tracer); sce.getServletContext().setAttribute(SMALLRYE_OPENTRACING_TRACER, tracer);
addJaxRsIntegration(sce.getServletContext(), tracer);


TracingLogger.ROOT_LOGGER.initializing(tracer.toString()); TracingLogger.ROOT_LOGGER.initializing(tracer.toString());
}


FilterRegistration.Dynamic filterRegistration = sce.getServletContext() private void addJaxRsIntegration(ServletContext servletContext, Tracer tracer) {
.addFilter(SpanFinishingFilter.class.getName(), new SpanFinishingFilter(tracer)); servletContext.setInitParameter("resteasy.providers", TracerDynamicFeature.class.getName());
FilterRegistration.Dynamic filterRegistration = servletContext.addFilter(SpanFinishingFilter.class.getName(),
new SpanFinishingFilter(tracer));
filterRegistration.setAsyncSupported(true); filterRegistration.setAsyncSupported(true);
filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "*"); filterRegistration.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), false, "*");
} }
Expand Down
Expand Up @@ -25,19 +25,21 @@
import io.opentracing.Tracer; import io.opentracing.Tracer;


import javax.enterprise.context.ApplicationScoped; import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.Produces; import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.servlet.ServletContext;


@ApplicationScoped @ApplicationScoped
public class TracerProducer { public class TracerProducer {
private Tracer tracer; @Inject
ServletContext servletContext;


@Produces @Produces
public Tracer getTracer() { public Tracer getTracer() {
return this.tracer; Object tracerObject = servletContext.getAttribute(TracerInitializer.SMALLRYE_OPENTRACING_TRACER);
} if (tracerObject instanceof Tracer) {

return (Tracer) tracerObject;
public void onTracerInitialized(@Observes Tracer tracer) { }
this.tracer = tracer; return null;
} }
} }
Expand Up @@ -51,15 +51,11 @@ public interface TracingLogger extends BasicLogger {
@Message(id = 4, value = "Registering %s as the OpenTracing Tracer") @Message(id = 4, value = "Registering %s as the OpenTracing Tracer")
void registeringTracer(String message); void registeringTracer(String message);


@LogMessage(level = DEBUG) @LogMessage(level = WARN)
@Message(id = 5, value = "CDI events are not supported for this deployment. Possible configuration issues? Skipping MicroProfile OpenTracing integration.") @Message(id = 5, value = "No tracer available to JAX-RS. Skipping MicroProfile OpenTracing configuration for JAX-RS")
void noCdiEventSupport(); void noTracerAvailable();


@LogMessage(level = DEBUG) @LogMessage(level = DEBUG)
@Message(id = 6, value = "Extra Tracer bean found: %s. Vetoing it, please use TracerResolver to specify a custom tracer to use.") @Message(id = 6, value = "Extra Tracer bean found: %s. Vetoing it, please use TracerResolver to specify a custom tracer to use.")
void extraTracerBean(String clazz); void extraTracerBean(String clazz);

@LogMessage(level = WARN)
@Message(id = 7, value = "Multiple tracer resolvers found. Cannot properly determine which one to use.")
void multipleTracerResolvers();
} }
@@ -0,0 +1 @@
org.wildfly.microprofile.opentracing.smallrye.TracingCDIExtension
Expand Up @@ -21,10 +21,6 @@
*/ */
package org.jboss.as.test.integration.microprofile.opentracing; package org.jboss.as.test.integration.microprofile.opentracing;


import static org.wildfly.test.integration.microprofile.config.smallrye.HttpUtils.getContent;

import java.net.URL;

import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
Expand All @@ -43,10 +39,13 @@
import org.jboss.shrinkwrap.api.spec.EnterpriseArchive; import org.jboss.shrinkwrap.api.spec.EnterpriseArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive; import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;


import java.net.URL;

import static org.wildfly.test.integration.microprofile.config.smallrye.HttpUtils.getContent;

/** /**
* Test verifying the assumption that different services inside single EAR have different tracers. * Test verifying the assumption that different services inside single EAR have different tracers.
* *
Expand Down Expand Up @@ -75,13 +74,11 @@ public static Archive<?> deploy() {
return ear; return ear;
} }


@Ignore("Unignore when https://issues.jboss.org/browse/WFWIP-104") // TODO
@Test @Test
public void testEarServicesUseDifferentTracers() throws Exception { public void testEarServicesUseDifferentTracers() throws Exception {
testHttpInvokation(); testHttpInvokation();
} }


@Ignore("Unignore when https://issues.jboss.org/browse/WFWIP-104") // TODO
@Test @Test
public void testEarServicesUseDifferentTracersAfterReload() throws Exception { public void testEarServicesUseDifferentTracersAfterReload() throws Exception {
//TODO the tracer instance is same after reload as before it - check whether this is correct or no //TODO the tracer instance is same after reload as before it - check whether this is correct or no
Expand Down

0 comments on commit f56413f

Please sign in to comment.