Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial implementation #4

Merged
merged 4 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ image:https://img.shields.io/github/license/smallrye/smallrye-opentracing.svg["L

SmallRye OpenTracing is an implementation of https://github.com/eclipse/microprofile-opentracing.

== Develop

Debug can be enabled in `arquillian.xml` configuration file.

```bash
mvn clean install
```
46 changes: 46 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,50 @@
<url>https://github.com/smallrye/smallrye-opentracing/</url>
<tag>HEAD</tag>
</scm>

<modules>
<module>smallrye-opentracing</module>
<module>tck</module>
</modules>

<properties>
<version.microprofile.opentracing>1.1</version.microprofile.opentracing>
<version.opentracing>0.31.0</version.opentracing>
<verion.opentracing.jaxrs>0.1.6</verion.opentracing.jaxrs>
<version.javax.ws.rs-api>2.0</version.javax.ws.rs-api>
<version.resteasy>3.0.24.Final</version.resteasy>
<version.weld>2.4.7.Final</version.weld>
<version.wildfly>13.0.0.Final</version.wildfly>
<version.arquillian>1.4.0.Final</version.arquillian>
<version.arquillian.wildfly>2.1.0.Final</version.arquillian.wildfly>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-opentracing</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-opentracing-tck</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.jboss.arquillian</groupId>
<artifactId>arquillian-bom</artifactId>
<version>${version.arquillian}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.wildfly.arquillian</groupId>
<artifactId>wildfly-arquillian-container-managed</artifactId>
<version>${version.arquillian.wildfly}</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
45 changes: 45 additions & 0 deletions smallrye-opentracing/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>smallrye-opentracing-parent</artifactId>
<groupId>io.smallrye</groupId>
<version>0.1.0-SNAPSHOT</version>
</parent>

<artifactId>smallrye-opentracing</artifactId>

<dependencies>
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-jaxrs2</artifactId>
<version>${verion.opentracing.jaxrs}</version>
</dependency>

<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>${version.javax.ws.rs-api}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package io.smallrye.opentracing;

import io.opentracing.Scope;
import io.opentracing.Tracer;
import java.lang.reflect.Method;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.ws.rs.Path;
import org.eclipse.microprofile.opentracing.Traced;

/**
* @author Pavol Loffay
*/
@Traced
@Interceptor
@Priority(value = Interceptor.Priority.LIBRARY_BEFORE + 1)
public class SmallRyeTracingCDIInterceptor {

@Inject
private Tracer tracer;

@AroundInvoke
public Object interceptTraced(InvocationContext ctx) throws Exception {
Scope activeScope = null;
try {
if (!isJaxRs(ctx.getMethod()) && isTraced(ctx.getMethod())) {
activeScope = tracer.buildSpan(getOperationName(ctx.getMethod()))
.startActive(true);
}
return ctx.proceed();
} finally {
if (activeScope != null) {
activeScope.close();
}
}
}

/**
* Determines whether invoked method is jax-rs endpoint
* @param method invoked method
* @return true if invoked method is jax-rs endpoint
*/
protected boolean isJaxRs(Method method) {
if (method.getAnnotation(Path.class) != null ||
method.getDeclaringClass().getAnnotation(Path.class) != null) {
return true;
}
return false;
}

/**
* Determines whether invoked method should be traced or not
* @param method invoked method
* @return true if {@link Traced} defined on method or class has value true
*/
protected boolean isTraced(Method method) {
Traced classTraced = method.getDeclaringClass().getAnnotation(Traced.class);
Traced methodTraced = method.getAnnotation(Traced.class);
if (methodTraced != null) {
return methodTraced.value();
}
return classTraced.value();
}

/**
* Returns operation name for given method
*
* @param method invoked method
* @return operation name
*/
protected String getOperationName(Method method) {
Traced classTraced = method.getDeclaringClass().getAnnotation(Traced.class);
Traced methodTraced = method.getAnnotation(Traced.class);
if (methodTraced != null && methodTraced.operationName().length() > 0) {
return methodTraced.operationName();
} else if (classTraced != null && classTraced.operationName().length() > 0) {
return classTraced.operationName();
}
return String.format("%s.%s", method.getDeclaringClass().getName(), method.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.smallrye.opentracing;

import io.opentracing.Tracer;
import io.opentracing.contrib.jaxrs2.server.OperationNameProvider.ClassNameOperationName;
import io.opentracing.contrib.jaxrs2.server.ServerTracingDynamicFeature;
import io.opentracing.contrib.jaxrs2.server.ServerTracingDynamicFeature.Builder;
import javax.enterprise.inject.Instance;
import javax.enterprise.inject.spi.CDI;
import javax.ws.rs.container.DynamicFeature;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;

/**
* @author Pavol Loffay
*/
@Provider
public class SmallRyeTracingDynamicFeature implements DynamicFeature {

private ServerTracingDynamicFeature delegate;

public SmallRyeTracingDynamicFeature() {
Instance<Tracer> tracerInstance = CDI.current().select(Tracer.class);
this.delegate = new Builder(tracerInstance.get())
.withOperationNameProvider(ClassNameOperationName.newBuilder())
.withTraceSerialization(false)
.build();
}

@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
delegate.configure(resourceInfo, context);
}
}
146 changes: 146 additions & 0 deletions tck/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>smallrye-opentracing-parent</artifactId>
<groupId>io.smallrye</groupId>
<version>0.1.0-SNAPSHOT</version>
</parent>

<artifactId>smallrye-opentracing-tck</artifactId>

<dependencies>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-opentracing</artifactId>
</dependency>

<dependency>
<groupId>org.eclipse.microprofile.opentracing</groupId>
<artifactId>microprofile-opentracing-tck</artifactId>
<version>${version.microprofile.opentracing}</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-mock</artifactId>
<version>${version.opentracing}</version>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>${version.resteasy}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>${version.resteasy}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-cdi</artifactId>
<version>${version.resteasy}</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>${version.resteasy}</version>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.testng</groupId>
<artifactId>arquillian-testng-container</artifactId>
</dependency>

<dependency>
<groupId>org.jboss.arquillian.container</groupId>
<artifactId>arquillian-container-test-spi</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.spec.javax.servlet</groupId>
<artifactId>jboss-servlet-api_3.1_spec</artifactId>
<version>1.0.0.Final</version>
</dependency>

<dependency>
<groupId>org.jboss.weld.servlet</groupId>
<artifactId>weld-servlet-core</artifactId>
<version>${version.weld}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<suiteXmlFiles>
<suiteXmlFile>src/test/tck-suite.xml</suiteXmlFile>
</suiteXmlFiles>
<dependenciesToScan>
<dependency>org.eclipse.microprofile.opentracing:microprofile-opentracing-tck</dependency>
</dependenciesToScan>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>wildfly-servlet</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.wildfly.arquillian</groupId>
<artifactId>wildfly-arquillian-container-managed</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<environmentVariables>
<JBOSS_HOME>${project.build.directory}/wildfly-servlet-${version.wildfly}</JBOSS_HOME>
</environmentVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>process-test-classes</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.wildfly</groupId>
<artifactId>wildfly-servlet-dist</artifactId>
<version>${version.wildfly}</version>
<type>zip</type>
<overWrite>false</overWrite>
<outputDirectory>${project.build.directory}</outputDirectory>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
21 changes: 21 additions & 0 deletions tck/src/test/java/io/smallrye/opentracing/ExceptionMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.smallrye.opentracing;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.Provider;

/**
* Temporal fix to catch exceptions thrown in JAX-RS endpoints
* See https://issues.jboss.org/browse/RESTEASY-1758
*
* @author Pavol Loffay
*/
@Provider
public class ExceptionMapper implements javax.ws.rs.ext.ExceptionMapper<RuntimeException> {

@Override
public Response toResponse(RuntimeException exception) {
return Response.status(Status.INTERNAL_SERVER_ERROR).build();
}
}

Loading