Skip to content

Commit

Permalink
Update to OpenTracing 0.33
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored and Ladicek committed Jan 15, 2021
1 parent 7b251b8 commit 7b13710
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,44 @@
import io.opentracing.Tracer;
import io.opentracing.util.GlobalTracer;

/**
* @author Michal Szynkiewicz, michal.l.szynkiewicz@gmail.com
*/
public class TracingContextProvider implements ThreadContextProvider {

private static final ThreadContextController DO_NOTHING = () -> {
};

@Override
public ThreadContextSnapshot currentContext(Map<String, String> props) {
Tracer tracer = GlobalTracer.get();
ScopeManager scopeManager = tracer.scopeManager();
Scope activeScope = scopeManager.active();
Span span = scopeManager.activeSpan();

if (activeScope != null) {
Span span = activeScope.span();
if (span != null) {
return () -> {
Scope propagated = scopeManager.activate(span, false);
Scope propagated = scopeManager.activate(span);
return propagated::close;
};
}

return () -> DO_NOTHING;
}

@Override
public ThreadContextSnapshot clearedContext(Map<String, String> props) {
return () -> {
Tracer tracer = GlobalTracer.get();
ScopeManager scopeManager = tracer.scopeManager();
Scope activeScope = scopeManager.active();
if (activeScope != null) {
activeScope.close();
}
return () -> {
// TODO: we should bring back the span here
};
};
// The OpenTracing API is apparently meant to be used in a way that
// never leaves scopes active. That is generally fine, but the API
// also doesn't provide a way to guard against misuse. That's because
// since OpenTracing 0.33, it is impossible to find "current" scope.
// So here, we just assume that the API is used correctly and there
// are no "dangling" scopes.
//
// Note that "active scope" is different from "active span". Span
// can be active for a long time, but once finished, it can't be
// reactivated. During the time a span is active, there are generally
// several slices of time when you actually work with it, and these
// slices are delimited by scopes. (In other words, creating a scope
// is akin to resuming a "paused" span, and closing a scope is akin
// to suspending a "running" span.)

return () -> DO_NOTHING;
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<version.smallrye-context-propagation>1.0.20</version.smallrye-context-propagation>
<version.smallrye-common>1.5.0</version.smallrye-common>
<version.smallrye-reactive-utils>1.4.0</version.smallrye-reactive-utils>
<version.opentracing>0.31.0</version.opentracing>
<version.opentracing>0.33.0</version.opentracing>

<version.junit-pioneer>1.1.0</version.junit-pioneer>
<version.mutiny>0.13.0</version.mutiny>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.junit.jupiter.api.Test;

import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.mock.MockSpan;
import io.opentracing.util.GlobalTracerTestUtil;
import io.smallrye.faulttolerance.util.FaultToleranceIntegrationTest;
Expand All @@ -43,8 +44,11 @@ public static void reset() {

@Test
public void testCircuitBreakerOpens(Service service) {
try (Scope ignored = Service.mockTracer.buildSpan("parent").startActive(true)) {
Span span = Service.mockTracer.buildSpan("parent").start();
try (Scope ignored = Service.mockTracer.scopeManager().activate(span)) {
assertThat(service.foo()).isEqualTo("fallback");
} finally {
span.finish();
}

List<MockSpan> mockSpans = Service.mockTracer.finishedSpans();
Expand All @@ -61,8 +65,11 @@ public void testCircuitBreakerOpens(Service service) {

@Test
public void testAsyncCircuitBreakerOpens(Service service) throws ExecutionException, InterruptedException {
try (Scope ignored = Service.mockTracer.buildSpan("parent").startActive(true)) {
Span span = Service.mockTracer.buildSpan("parent").start();
try (Scope ignored = Service.mockTracer.scopeManager().activate(span)) {
assertThat(service.asyncFoo().toCompletableFuture().get()).isEqualTo("asyncFallback");
} finally {
span.finish();
}

List<MockSpan> mockSpans = Service.mockTracer.finishedSpans();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.Test;

import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.mock.MockSpan;
import io.opentracing.util.GlobalTracerTestUtil;
import io.smallrye.faulttolerance.util.FaultToleranceIntegrationTest;
Expand All @@ -47,8 +48,11 @@ public void test(Service service) throws ExecutionException, InterruptedExceptio
}

private void doTest(Service service) throws ExecutionException, InterruptedException {
try (Scope ignored = Service.tracer.buildSpan("parent").startActive(true)) {
Span span = Service.tracer.buildSpan("parent").start();
try (Scope ignored = Service.tracer.scopeManager().activate(span)) {
assertThat(service.hello().toCompletableFuture().get()).isEqualTo("fallback");
} finally {
span.finish();
}

await().atMost(5, TimeUnit.SECONDS).untilAsserted(() -> {
Expand Down

0 comments on commit 7b13710

Please sign in to comment.