Skip to content

Commit

Permalink
Use active span as parent if exists (#74)
Browse files Browse the repository at this point in the history
* using active span (probably initialized in preceeding filter) if exists instead of extracting from http headers

* readbility improvement of parentSpan method

* test for resolving parent span in case it already exists

* added tests to cxf and resteasy providers
  • Loading branch information
tmszdmsk authored and pavolloffay committed Feb 6, 2018
1 parent f3d52a4 commit 1ead39f
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.opentracing.contrib.jaxrs2.itest.cxf;

import io.opentracing.contrib.jaxrs2.itest.common.AbstractParentSpanResolutionTest;
import org.eclipse.jetty.servlet.ServletContextHandler;

public class ApacheCXFParentSpanResolutionTest extends AbstractParentSpanResolutionTest {

@Override
protected void initServletContext(ServletContextHandler context) {
super.initServletContext(context);
ApacheCXFHelper.initServletContext(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package io.opentracing.contrib.jaxrs2.itest.common;

import static org.awaitility.Awaitility.await;

import io.opentracing.Scope;
import io.opentracing.mock.MockSpan;
import io.opentracing.tag.Tags;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.junit.Assert;
import org.junit.Test;

import java.io.IOException;
import java.util.EnumSet;
import java.util.List;

import javax.servlet.*;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.core.Response;

public abstract class AbstractParentSpanResolutionTest extends AbstractJettyTest {

@Override
protected void initServletContext(ServletContextHandler context) {
context.addFilter(new FilterHolder(new FilterThatInitsSpan()), "/*",
EnumSet.of(
DispatcherType.REQUEST,
DispatcherType.FORWARD,
// TODO CXF does not call AsyncListener#onComplete() without this (it calls only onStartAsync)
DispatcherType.ASYNC,
DispatcherType.ERROR,
DispatcherType.INCLUDE
)
);
}

@Test
public void testUseActiveSpanIfSet() {
Client client = ClientBuilder.newClient();
Response response = client.target(url("/hello/1"))
.request()
.get();
response.close();
await().until(finishedSpansSizeEquals(2));

List<MockSpan> spans = mockTracer.finishedSpans();

Assert.assertEquals(2, spans.size());

MockSpan preceding = getSpanWithTag(spans, new ImmutableTag(Tags.COMPONENT.getKey(), "preceding-opentracing-filter"));
MockSpan original = getSpanWithTag(spans, new ImmutableTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER));

Assert.assertEquals(preceding.context().spanId(), original.parentId());
}

class FilterThatInitsSpan implements Filter {


@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
Scope scope = mockTracer.buildSpan("initializing-span")
.withTag(Tags.COMPONENT.getKey(), "preceding-opentracing-filter")
.startActive(true);
chain.doFilter(request, response);
scope.close();
}

@Override
public void destroy() {

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.opentracing.contrib.jaxrs2.itest.jersey;

import io.opentracing.contrib.jaxrs2.itest.common.AbstractParentSpanResolutionTest;
import org.eclipse.jetty.servlet.ServletContextHandler;

public class JerseyParentSpanResolutionTest extends AbstractParentSpanResolutionTest{
@Override
protected void initServletContext(ServletContextHandler context) {
super.initServletContext(context);
JerseyHelper.initServletContext(context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.opentracing.contrib.jaxrs2.itest.resteasy;

import io.opentracing.contrib.jaxrs2.itest.common.AbstractParentSpanResolutionTest;
import org.eclipse.jetty.servlet.ServletContextHandler;

public class RestEasyParentSpanResolutionTest extends AbstractParentSpanResolutionTest {

protected void initServletContext(ServletContextHandler context) {
super.initServletContext(context);
RestEasyHelper.initServletContext(context);
}

}
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package io.opentracing.contrib.jaxrs2.server;

import static io.opentracing.contrib.jaxrs2.internal.SpanWrapper.PROPERTY_NAME;

import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.contrib.jaxrs2.internal.CastUtils;
import io.opentracing.contrib.jaxrs2.internal.SpanWrapper;
import io.opentracing.propagation.Format;
import io.opentracing.tag.Tags;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import javax.annotation.Priority;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Priorities;
Expand All @@ -23,6 +16,13 @@
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.Context;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import static io.opentracing.contrib.jaxrs2.internal.SpanWrapper.PROPERTY_NAME;

/**
* @author Pavol Loffay
Expand Down Expand Up @@ -54,21 +54,20 @@ protected ServerTracingFilter(
private HttpServletRequest httpServletRequest;

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
public void filter(ContainerRequestContext requestContext) {
// return in case filter if registered twice
if (requestContext.getProperty(PROPERTY_NAME) != null || matchesSkipPattern(requestContext)) {
return;
}

if (tracer != null) {
SpanContext extractedSpanContext = tracer.extract(Format.Builtin.HTTP_HEADERS,
new ServerHeadersExtractTextMap(requestContext.getHeaders()));

Tracer.SpanBuilder spanBuilder = tracer.buildSpan(operationNameProvider.operationName(requestContext))
.withTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER);

if (extractedSpanContext != null) {
spanBuilder.asChildOf(extractedSpanContext);
SpanContext parentSpanContext = parentSpanContext(requestContext);
if (parentSpanContext != null) {
spanBuilder.asChildOf(parentSpanContext);
}

Span span = spanBuilder.startActive(false).span();
Expand All @@ -92,9 +91,21 @@ public void filter(ContainerRequestContext requestContext) throws IOException {
}
}

private SpanContext parentSpanContext(ContainerRequestContext requestContext) {
Span activeSpan = tracer.activeSpan();
if (activeSpan != null) {
return activeSpan.context();
} else {
return tracer.extract(
Format.Builtin.HTTP_HEADERS,
new ServerHeadersExtractTextMap(requestContext.getHeaders())
);
}
}

@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
public void filter(ContainerRequestContext requestContext,
ContainerResponseContext responseContext) {
SpanWrapper spanWrapper = CastUtils.cast(requestContext.getProperty(PROPERTY_NAME), SpanWrapper.class);
if (spanWrapper == null) {
return;
Expand Down

0 comments on commit 1ead39f

Please sign in to comment.