Skip to content

Commit

Permalink
Merge pull request #2598 from gmarcy/2566-mpOpenTracing-fixes
Browse files Browse the repository at this point in the history
fix annotation issue in opentracing
  • Loading branch information
ewhoch committed Mar 9, 2018
2 parents 90c45d3 + 14cc885 commit 127d9b5
Show file tree
Hide file tree
Showing 22 changed files with 343 additions and 23 deletions.
7 changes: 6 additions & 1 deletion dev/com.ibm.ws.microprofile.opentracing/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ Bundle-Description: MicroProfile.OpenTracing 1.0, version ${bVersion}

WS-TraceGroup: OPENTRACING

-dsannotations: com.ibm.ws.microprofile.opentracing.OpenTracingFilterHelper
-dsannotations: \
com.ibm.ws.microprofile.opentracing.OpenTracingFilterHelper, \
com.ibm.ws.microprofile.opentracing.cdi.OpenTracingCDIInjectionExtension

Import-Package: *

Expand All @@ -30,6 +32,9 @@ Export-Package: \

Require-Capability: osgi.ee;filter:="(&(osgi.ee=JavaSE)(version=1.8))"

Include-Resource: \
META-INF=resources/META-INF

-buildpath: \
io.opentracing:opentracing-api;version=0.30.0, \
com.ibm.ws.opentracing;version=latest, \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--
Copyright (c) 2017 IBM Corporation and others.
All rights reserved. This program and the accompanying materials
are made available under the terms of the Eclipse Public License v1.0
which accompanies this distribution, and is available at
http://www.eclipse.org/legal/epl-v10.html
Contributors:
IBM Corporation - initial API and implementation
-->
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
version="1.1"
bean-discovery-mode="all">
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.ibm.ws.microprofile.opentracing.cdi.OpenTracingCDIInjectionExtension
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Context;

import org.osgi.service.component.annotations.Component;

Expand All @@ -36,9 +35,6 @@
public class OpenTracingFilterHelper implements OpentracingFilterHelper {
private static final TraceComponent tc = Tr.register(OpenTracingFilterHelper.class);

@Context
protected ResourceInfo resourceInfo;

//
@Override
public String getBuildSpanName(ClientRequestContext clientRequestContext) {
Expand All @@ -50,8 +46,8 @@ public String getBuildSpanName(ClientRequestContext clientRequestContext) {

//
@Override
public String getBuildSpanName(ContainerRequestContext incomingRequestContext) {
String methodName = "filter(incoming)";
public String getBuildSpanName(ContainerRequestContext incomingRequestContext, ResourceInfo resourceInfo) {
String methodName = "getBuildSpanName";

String methodOperationName = OpenTracingService.getMethodOperationName(resourceInfo.getResourceMethod());
String classOperationName = OpenTracingService.getClassOperationName(resourceInfo.getResourceMethod());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*******************************************************************************
* Copyright (c) 2017 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.ws.microprofile.opentracing.cdi;

import javax.enterprise.inject.spi.Extension;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;

import com.ibm.ws.cdi.extension.WebSphereCDIExtension;

@Component(service = WebSphereCDIExtension.class, configurationPolicy = ConfigurationPolicy.IGNORE, immediate = true)
public class OpenTracingCDIInjectionExtension implements WebSphereCDIExtension, Extension {}
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ public class OpentracingClientFilter implements ClientRequestFilter, ClientRespo

public static final String CLIENT_SPAN_SKIPPED_ID = OpentracingClientFilter.class.getName() + ".Skipped";

private final OpentracingFilterHelper helper;
private OpentracingFilterHelper helper;

OpentracingClientFilter(OpentracingFilterHelper helper) {
setFilterHelper(helper);
}

void setFilterHelper(OpentracingFilterHelper helper) {
this.helper = helper;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
Expand Down Expand Up @@ -51,9 +53,16 @@ public class OpentracingContainerFilter implements ContainerRequestFilter, Conta

public static final String EXCEPTION_KEY = OpentracingContainerFilter.class.getName() + ".Exception";

private final OpentracingFilterHelper helper;
@Context
protected ResourceInfo resourceInfo;

private OpentracingFilterHelper helper;

OpentracingContainerFilter(OpentracingFilterHelper helper) {
setFilterHelper(helper);
}

void setFilterHelper(OpentracingFilterHelper helper) {
this.helper = helper;
}

Expand Down Expand Up @@ -94,7 +103,7 @@ public void filter(ContainerRequestContext incomingRequestContext) throws IOExce

String buildSpanName;
if (helper != null) {
buildSpanName = helper.getBuildSpanName(incomingRequestContext);
buildSpanName = helper.getBuildSpanName(incomingRequestContext, resourceInfo);
if (buildSpanName == null) {
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
Tr.debug(tc, methodName + " skipping not traced method");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ResourceInfo;

/**
* Service interface to provide operation names for building spans in filters
Expand All @@ -20,5 +21,5 @@ public interface OpentracingFilterHelper {

String getBuildSpanName(ClientRequestContext clientRequestContext);

String getBuildSpanName(ContainerRequestContext incomingRequestContext);
String getBuildSpanName(ContainerRequestContext incomingRequestContext, ResourceInfo resourceInfo);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,22 @@ protected void deactivate(ComponentContext context) {
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
protected void setOpentracingFilterHelper(OpentracingFilterHelper helper) {
this.helper = helper;
if (containerFilter != null) {
containerFilter.setFilterHelper(helper);
}
if (clientFilter != null) {
clientFilter.setFilterHelper(helper);
}
}

protected void unsetOpentracingFilterHelper(OpentracingFilterHelper helper) {
this.helper = null;
if (containerFilter != null) {
containerFilter.setFilterHelper(null);
}
if (clientFilter != null) {
clientFilter.setFilterHelper(null);
}
}

@Trivial
Expand Down
1 change: 1 addition & 0 deletions dev/com.ibm.ws.opentracing_fat/.classpath
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="fat/src"/>
<classpathentry kind="src" path="test-applications/mpOpenTracing/src"/>
<classpathentry kind="src" path="test-applications/serviceApp/src"/>
<classpathentry kind="src" path="test-bundles/opentracing.mock/src"/>
<classpathentry kind="src" path="test-applications/jaxrsHelloWorld/src"/>
Expand Down
1 change: 1 addition & 0 deletions dev/com.ibm.ws.opentracing_fat/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ src: \
fat/src,\
test-applications/serviceApp/src, \
test-applications/jaxrsHelloWorld/src, \
test-applications/mpOpenTracing/src, \
test-bundles/opentracing.mock/src

-sub: *.bnd
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*******************************************************************************
* Copyright (c) 2017 IBM Corpo<ration and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package com.ibm.ws.testing.opentracing.test;

import java.io.File;
import java.util.List;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.ibm.websphere.simplicity.ShrinkHelper;

import componenttest.annotation.MinimumJavaLevel;
import componenttest.custom.junit.runner.Mode;
import componenttest.custom.junit.runner.Mode.TestMode;
import componenttest.topology.impl.LibertyServer;
import componenttest.topology.impl.LibertyServerFactory;
import junit.framework.Assert;

/**
* <p>Test that a JAXRS application with @Traced(false) works.</p>
*
* <p>The test suite:</p>
*
* <ul>
* <li>{@link #testHelloWorld()}</li>
* </ul>
*/
@Mode(TestMode.FULL)
@MinimumJavaLevel(javaLevel = 1.8)
public class FATMPOpenTracing {
/**
* For tracing.
*/
private static final Class<?> CLASS = FATMPOpenTracing.class;

/**
* Set to the generated server before any tests are run.
*/
private static LibertyServer server;

/**
* Deploy the application and start the server.
*
* @throws Exception Errors deploying the application.
*/
@BeforeClass
public static void setUp() throws Exception {
server = LibertyServerFactory.getLibertyServer("opentracingFATServer3");
WebArchive serviceWar = ShrinkWrap.create(WebArchive.class, "mpOpenTracing.war");
serviceWar.addPackages(true, "com.ibm.ws.testing.mpOpenTracing");
serviceWar.addAsWebInfResource(
new File("test-applications/mpOpenTracing/resources/beans.xml"));
ShrinkHelper.exportAppToServer(server, serviceWar);
server.startServer();
}

/**
* Stop the server.
*
* @throws Exception Errors stopping the server.
*/
@AfterClass
public static void tearDown() throws Exception {
server.stopServer();
}

/**
* Execute the Hello World JAXRS service and ensure it returns the expected response.
*
* @throws Exception Errors executing the service.
*/
@Test
public void testHelloWorld() throws Exception {
String methodName = "testHelloWorld";

List<String> actualResponseLines = executeWebService("helloWorld");

FATLogging.info(CLASS, methodName, "Actual Response", actualResponseLines);

Assert.assertEquals(1, actualResponseLines.size());
Assert.assertEquals("Hello World", actualResponseLines.get(0));

actualResponseLines = executeWebService("getTracerState");

String tracerState = "";
for (String actualResponseLine : actualResponseLines) {
tracerState += actualResponseLine;
}

int expectedSpans = 2;
int spanCount = getSpanCount(tracerState);
if (spanCount != expectedSpans) {
Assert.assertEquals("Expected " + expectedSpans + " spans but found " + spanCount + ":", tracerState);
}
}

protected List<String> executeWebService(String method) throws Exception {
String requestUrl = "http://" +
server.getHostname() + ":" +
server.getHttpDefaultPort() +
"/mpOpenTracing/rest/ws/" + method;

return FATUtilsServer.gatherHttpRequest(FATUtilsServer.HttpRequestMethod.GET, requestUrl);
}

protected int getSpanCount(String tracerState) {
int result = 0;
int i = 0;
i = tracerState.indexOf("spanId", i);
while (i != -1) {
result++;
i = tracerState.indexOf("spanId", i + 1);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

/**
* <p>Test that a JAXRS application works even if the opentracing
* features is enabled without a Tracer.</p>
* feature is enabled without a Tracer.</p>
*
* <p>The test suite:</p>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,21 @@
*
* <p>This class *must* be named "FATSuite", since the test code is hard coded
* to look for just that class.</p>
*
* <p>See the file "open-liberty/dev/com.ibm.ws.opentracing_fat/generated/autoFVT/src/ant/launch.xml",
* which has property "filesToFind" coded to "FATSuiteLite.class;FATSuite.class;FATTest.class".</p>
*/
@RunWith(Suite.class)
@SuiteClasses({
TestSpanUtils.class,
FATOpentracing.class,
FATOpentracingHelloWorld.class
FATOpentracingHelloWorld.class,
FATMPOpenTracing.class
})
public class FATSuite implements FATOpentracingConstants {
// Logging ...

private static final Class<? extends FATSuite> CLASS = FATSuite.class;

private static void info(String methodName, String text) {
FATLogging.info(CLASS, methodName, text);
}

//

@BeforeClass
public static void setUp() throws Exception {
String methodName = "setUp";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<featureManager>
<feature>servlet-3.1</feature>
<feature>mpOpentracing-1.0</feature>
<feature>opentracing-1.0</feature>
<feature>usr:opentracingMock-0.30</feature>
<feature>componenttest-1.0</feature>
</featureManager>
Expand All @@ -15,7 +15,7 @@

<!-- Turn this on for the FAT: That provides good diagnostics in case -->
<!-- of a failure, and means that trace code is tested. -->
<logging traceSpecification="*=audit:com.ibm.ws.opentracing.*=all"/>
<logging traceSpecification="*=info:com.ibm.ws.opentracing*=all:com.ibm.ws.microprofile.opentracing*=all" maxFileSize="100" maxFiles="1" />

<opentracing>
<!-- Test excluding exact relative and absolute URIs -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@

<application location="jaxrsHelloWorld.war" />

</server>
<logging traceSpecification="*=info:com.ibm.ws.opentracing*=all:com.ibm.ws.microprofile.opentracing*=all" maxFileSize="100" maxFiles="1" />

</server>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
apps/
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bootstrap.include=../testports.properties
Loading

0 comments on commit 127d9b5

Please sign in to comment.