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

Pass and close scope directly #88

Merged
merged 1 commit into from
Jun 4, 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.opentracing.Tracer;
import io.opentracing.contrib.jaxrs2.internal.CastUtils;
import io.opentracing.contrib.jaxrs2.internal.SpanWrapper;
import io.opentracing.noop.NoopScopeManager.NoopScope;
import io.opentracing.propagation.Format;
import io.opentracing.tag.Tags;
import java.io.IOException;
Expand Down Expand Up @@ -61,7 +62,10 @@ public void filter(ClientRequestContext requestContext) throws IOException {
.asChildOf(parentSpanContext);
}

Span span = spanBuilder.start();
/**
* Do not create Scope - there is no way to deactivate it on UnknownHostException
*/
final Span span = spanBuilder.start();

if (spanDecorators != null) {
for (ClientSpanDecorator decorator: spanDecorators) {
Expand All @@ -74,7 +78,16 @@ public void filter(ClientRequestContext requestContext) throws IOException {
}

tracer.inject(span.context(), Format.Builtin.HTTP_HEADERS, new ClientHeadersInjectTextMap(requestContext.getHeaders()));
requestContext.setProperty(PROPERTY_NAME, new SpanWrapper(span));
requestContext.setProperty(PROPERTY_NAME, new SpanWrapper(new NoopScope() {
@Override
public void close() {
}

@Override
public Span span() {
return span;
}
}));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.opentracing.contrib.jaxrs2.internal;

import io.opentracing.Scope;
import io.opentracing.Span;
import java.sql.Wrapper;
import java.util.concurrent.atomic.AtomicBoolean;

/**
Expand All @@ -12,21 +14,25 @@ public class SpanWrapper {

public static final String PROPERTY_NAME = SpanWrapper.class.getName() + ".activeSpanWrapper";

private Span span;
private Scope scope;
private AtomicBoolean finished = new AtomicBoolean();

public SpanWrapper(Span span) {
this.span = span;
public SpanWrapper(Scope scope) {
this.scope = scope;
}

public Span get() {
return span;
return scope.span();
}

public Scope getScope() {
return scope;
}

public synchronized void finish() {
if (!finished.get()) {
finished.set(true);
span.finish();
scope.span().finish();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.opentracing.contrib.jaxrs2.server;

import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
Expand Down Expand Up @@ -70,27 +71,33 @@ public void filter(ContainerRequestContext requestContext) {
spanBuilder.asChildOf(parentSpanContext);
}

Span span = spanBuilder.startActive(false).span();
Scope scope = spanBuilder.startActive(false);

if (spanDecorators != null) {
for (ServerSpanDecorator decorator: spanDecorators) {
decorator.decorateRequest(requestContext, span);
decorator.decorateRequest(requestContext, scope.span());
}
}

// override operation name set by @Traced
if (this.operationName != null) {
span.setOperationName(operationName);
scope.span().setOperationName(operationName);
}

if (log.isLoggable(Level.FINEST)) {
log.finest("Creating server span: " + operationName);
}

requestContext.setProperty(PROPERTY_NAME, new SpanWrapper(span));
requestContext.setProperty(PROPERTY_NAME, new SpanWrapper(scope));
}
}

/**
* Returns a parent for a span created by this filter (jax-rs span).
* The context from the active span takes precedence over context in the request.
* The current active span should be child-of extracted context and for example
* created at a lower level e.g. jersey filter.
*/
private SpanContext parentSpanContext(ContainerRequestContext requestContext) {
Span activeSpan = tracer.activeSpan();
if (activeSpan != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,9 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
}
throw ex;
} finally {
Scope scope = tracer.scopeManager().active();
if (scope != null) {
scope.close();
}
SpanWrapper spanWrapper = getSpanWrapper(httpRequest);
if (spanWrapper != null) {
spanWrapper.getScope().close();
if (request.isAsyncStarted()) {
request.getAsyncContext().addListener(new SpanFinisher(spanWrapper), request, response);
} else {
Expand Down