Skip to content

Commit

Permalink
Update to follow suit with core changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bhs committed Apr 4, 2017
1 parent fcab9c0 commit 55794c6
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 84 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.opentracing.mdcdemo;

import io.opentracing.ActiveSpanSource;
import io.opentracing.Span;
import io.opentracing.impl.AbstractActiveSpan;
import org.slf4j.MDC;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
* MDCActiveSpan illustrates the core ActiveSpan concepts and capabilities to a first approximation. Not
* production-quality code.
*/
class MDCActiveSpan extends AbstractActiveSpan {
private MDCActiveSpanSource mdcActiveSpanSource;
private MDCActiveSpan toRestore = null;

MDCActiveSpan(MDCActiveSpanSource mdcActiveSpanSource, Span span, Map<String, String> mdcContext, AtomicInteger refCount) {
super(span, refCount);
this.mdcActiveSpanSource = mdcActiveSpanSource;
this.toRestore = mdcActiveSpanSource.tlsSnapshot.get();
mdcActiveSpanSource.tlsSnapshot.set(this);
MDC.setContextMap(mdcContext);
}

@Override
protected void doDeactivate() {
if (mdcActiveSpanSource.tlsSnapshot.get() != this) {
// This shouldn't happen if users call methods in the expected order. Bail out.
return;
}
mdcActiveSpanSource.tlsSnapshot.set(toRestore);
}

@Override
protected ActiveSpanSource spanSource() {
return mdcActiveSpanSource;
}

static class MDCContinuation extends AbstractActiveSpan.AbstractContinuation {
private MDCActiveSpanSource mdcActiveSpanSource;
private final Map<String, String> mdcContext;
private final Span span;

MDCContinuation(MDCActiveSpanSource source, Span span, AtomicInteger refCount) {
super(refCount);
this.mdcActiveSpanSource = source;
this.mdcContext = MDC.getCopyOfContextMap();
this.span = span;
}

@Override
public MDCActiveSpan activate() {
return new MDCActiveSpan(mdcActiveSpanSource, span, mdcContext, refCount);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,75 +1,30 @@
package io.opentracing.mdcdemo;

import io.opentracing.ActiveSpanSource;
import io.opentracing.Span;
import io.opentracing.impl.AbstractActiveSpan;
import io.opentracing.impl.AbstractActiveSpanSource;
import org.slf4j.MDC;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
* MDCActiveSpanSource illustrates the core ActiveSpanSource concepts and capabilities to a first approximation. Not
* MDCActiveSpanSource illustrates the core Source concepts and capabilities to a first approximation. Not
* production-quality code.
*/
public class MDCActiveSpanSource extends AbstractActiveSpanSource {
private final ThreadLocal<MDCHandle> tlsSnapshot = new ThreadLocal<MDCHandle>();

class MDCHandle extends AbstractHandle {
private final Span span;
private MDCHandle toRestore = null;

MDCHandle(Span span, Map<String, String> mdcContext, AtomicInteger refCount) {
super(refCount);
this.span = span;
this.toRestore = tlsSnapshot.get();
tlsSnapshot.set(this);
MDC.setContextMap(mdcContext);
}

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

@Override
protected void doDeactivate() {
if (tlsSnapshot.get() != this) {
// This shouldn't happen if users call methods in the expected order. Bail out.
return;
}
tlsSnapshot.set(toRestore);
}

@Override
protected ActiveSpanSource spanSource() {
return MDCActiveSpanSource.this;
}

}
class MDCContinuation extends AbstractContinuation {
private final Map<String, String> mdcContext;
private final Span span;

MDCContinuation(Span span, AtomicInteger refCount) {
super(refCount);
this.mdcContext = MDC.getCopyOfContextMap();
this.span = span;
}

@Override
public MDCHandle activate() {
return new MDCHandle(span, mdcContext, refCount);
}
}
final ThreadLocal<MDCActiveSpan> tlsSnapshot = new ThreadLocal<MDCActiveSpan>();

@Override
protected MDCContinuation makeContinuation(Span span, AtomicInteger refCount) {
return new MDCContinuation(span, refCount);
protected MDCActiveSpan.MDCContinuation makeContinuation(Span span, AtomicInteger refCount) {
if (span instanceof AbstractActiveSpan) {
throw new IllegalArgumentException("Should only adopt the wrapped Span");
}
return new MDCActiveSpan.MDCContinuation(this, span, refCount);
}

@Override
public MDCHandle active() {
public MDCActiveSpan active() {
return tlsSnapshot.get();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.opentracing.mdcdemo;

import io.opentracing.ActiveSpanSource;
import io.opentracing.ActiveSpan;
import io.opentracing.Span;
import io.opentracing.Tracer;
import io.opentracing.mock.MockSpan;
Expand All @@ -10,7 +10,10 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class MDCDemo {
Tracer tracer;
Expand All @@ -25,7 +28,7 @@ public void trivialSpan() {
}

public void trivialChild() throws Exception {
try (ActiveSpanSource.Handle c = this.tracer.buildSpan("trivialParent").startAndActivate()) {
try (ActiveSpan c = this.tracer.buildSpan("trivialParent").startAndActivate()) {
// The child will automatically know about the parent.
Span child = this.tracer.buildSpan("trivialChild").start();
child.finish();
Expand All @@ -44,7 +47,7 @@ public void asyncSpans() throws Exception {
final List<Future<?>> subfutures = new ArrayList<>();

// Create a parent Continuation for all of the async activity.
try (final ActiveSpanSource.Handle parentHandle = tracer.buildSpan("parent").startAndActivate();) {
try (final ActiveSpan parentHandle = tracer.buildSpan("parent").startAndActivate();) {

// Create 10 async children.
for (int i = 0; i < 10; i++) {
Expand All @@ -55,14 +58,14 @@ public void asyncSpans() throws Exception {
public void run() {
// START child body

try (final ActiveSpanSource.Handle childHandle =
try (final ActiveSpan childHandle =
tracer.buildSpan("child_" + j).startAndActivate();) {
Thread.currentThread().sleep(1000);
tracer.spanSource().active().span().log("awoke");
tracer.spanSource().active().log("awoke");
Runnable r = new Runnable() {
@Override
public void run() {
Span active = tracer.spanSource().active().span();
Span active = tracer.spanSource().active();
active.log("awoke again");
System.out.println("MDC parent number: " + MDC.get("parent number"));
// Create a grandchild for each child... note that we don't *need* to use the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package io.opentracing.mdcdemo;

import io.opentracing.ActiveSpan;
import io.opentracing.ActiveSpanSource;

import java.util.concurrent.Callable;

public class TracedCallable<T> implements Callable<T> {
private ActiveSpanSource.Continuation continuation;
private ActiveSpan.Continuation continuation;
private Callable<T> callable;

public TracedCallable(Callable<T> callable, ActiveSpanSource activeSpanSource) {
this(callable, activeSpanSource.active());
public TracedCallable(Callable<T> callable, ActiveSpanSource spanSource) {
this(callable, spanSource.active());
}

public TracedCallable(Callable<T> callable, ActiveSpanSource.Handle handle) {
public TracedCallable(Callable<T> callable, ActiveSpan handle) {
if (callable == null) throw new NullPointerException("Callable is <null>.");
this.callable = callable;
this.continuation = handle.defer();
}

public T call() throws Exception {
try (ActiveSpanSource.Handle handle = continuation.activate()) {
try (ActiveSpan handle = continuation.activate()) {
return callable.call();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,42 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class TracedExecutorService implements ExecutorService {
private ExecutorService executor;
private ActiveSpanSource activeSpanSource;
private ActiveSpanSource spanSource;

public TracedExecutorService(ExecutorService executor, ActiveSpanSource activeSpanSource) {
public TracedExecutorService(ExecutorService executor, ActiveSpanSource spanSource) {
if (executor == null) throw new NullPointerException("Executor is <null>.");
if (activeSpanSource == null) throw new NullPointerException("ActiveSpanSource is <null>.");
if (spanSource == null) throw new NullPointerException("Source is <null>.");
this.executor = executor;
this.activeSpanSource = activeSpanSource;
this.spanSource = spanSource;
}

@Override
public void execute(Runnable command) {
executor.execute(new TracedRunnable(command, activeSpanSource));
executor.execute(new TracedRunnable(command, spanSource));
}

@Override
public Future<?> submit(Runnable task) {
return executor.submit(new TracedRunnable(task, activeSpanSource));
return executor.submit(new TracedRunnable(task, spanSource));
}

@Override
public <T> Future<T> submit(Runnable task, T result) {
return executor.submit(new TracedRunnable(task, activeSpanSource), result);
return executor.submit(new TracedRunnable(task, spanSource), result);
}

@Override
public <T> Future<T> submit(Callable<T> task) {
return executor.submit(new TracedCallable(task, activeSpanSource));
return executor.submit(new TracedCallable(task, spanSource));
}

@Override
Expand Down Expand Up @@ -89,7 +94,7 @@ private <T> Collection<? extends Callable<T>> tasksWithTracing(
Collection<? extends Callable<T>> tasks) {
if (tasks == null) throw new NullPointerException("Collection of tasks is <null>.");
Collection<Callable<T>> result = new ArrayList<Callable<T>>(tasks.size());
for (Callable<T> task : tasks) result.add(new TracedCallable(task, activeSpanSource));
for (Callable<T> task : tasks) result.add(new TracedCallable(task, spanSource));
return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package io.opentracing.mdcdemo;

import io.opentracing.ActiveSpan;
import io.opentracing.ActiveSpanSource;

import java.io.IOException;


public class TracedRunnable implements Runnable {
private Runnable runnable;
private ActiveSpanSource.Continuation continuation;
private ActiveSpan.Continuation continuation;

public TracedRunnable(Runnable runnable, ActiveSpanSource spanSource) {
this(runnable, spanSource.active());
}

public TracedRunnable(Runnable runnable, ActiveSpanSource.Handle handle) {
public TracedRunnable(Runnable runnable, ActiveSpan handle) {
if (runnable == null) throw new NullPointerException("Runnable is <null>.");
this.runnable = runnable;
this.continuation = handle.defer();
Expand All @@ -22,10 +20,10 @@ public TracedRunnable(Runnable runnable, ActiveSpanSource.Handle handle) {
@Override
public void run() {
// NOTE: There's no way to be sure about the finishOnDeactivate parameter to activate(), so we play it safe.
try (ActiveSpanSource.Handle handle = this.continuation.activate()) {
try (ActiveSpan handle = this.continuation.activate()) {
runnable.run();
} catch (IOException e) {
// Do nothing?
} catch (Exception e) {
e.printStackTrace();
}
}
}

0 comments on commit 55794c6

Please sign in to comment.