Skip to content
Open
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
@@ -1,7 +1,9 @@
package io.opentracing.contrib.examples.activate_deactivate;

import io.opentracing.ActiveSpan;
import io.opentracing.ActiveSpan.Continuation;
import io.opentracing.Scope;
import io.opentracing.Scope.Observer;
import io.opentracing.ScopeManager;
import io.opentracing.Span;
import io.opentracing.tag.Tags;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
Expand All @@ -15,27 +17,23 @@ public class Callback implements Runnable {

private static final Logger logger = LoggerFactory.getLogger(Callback.class);

private final Continuation continuation;
private final Span continuation;

Callback(ActiveSpan activeSpan) {
continuation = activeSpan.capture();
Callback(Span activeSpan) {
continuation = activeSpan;
logger.info("Callback created");
}

@Override
public void run() {
logger.info("Callback started");
ActiveSpan activeSpan = continuation.activate();

try {
try (Scope scope = continuation.activate(Observer.FINISH_ON_CLOSE)) {
TimeUnit.SECONDS.sleep(1);
logger.info("Callback finished");
scope.span().setTag(Tags.HTTP_STATUS.getKey(), 200); // we need it to test that finished span has it
} catch (InterruptedException e) {
e.printStackTrace();
}

activeSpan
.setTag(Tags.HTTP_STATUS.getKey(), 200); // we need it to test that finished span has it
activeSpan.deactivate();
logger.info("Callback finished");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;

import io.opentracing.ActiveSpan;
import io.opentracing.Scope;
import io.opentracing.Scope.Observer;
import io.opentracing.mock.MockSpan;
import io.opentracing.mock.MockTracer;
import io.opentracing.mock.MockTracer.Propagator;
import io.opentracing.tag.Tags;
import io.opentracing.util.ThreadLocalActiveSpanSource;
import io.opentracing.util.ThreadLocalScopeManager;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
Expand All @@ -23,12 +24,12 @@ public class TestCallback {

private static final Logger logger = LoggerFactory.getLogger(TestCallback.class);

private final MockTracer tracer = new MockTracer(new ThreadLocalActiveSpanSource(),
Propagator.TEXT_MAP);
private final MockTracer tracer = new MockTracer(Propagator.TEXT_MAP);
private final ScheduledExecutorService service = Executors.newScheduledThreadPool(1);

@Test
public void test() throws Exception {
tracer.setScopeManager(new ThreadLocalScopeManager());
Thread entryThread = entryThread();
entryThread.start();
entryThread.join(10_000);
Expand All @@ -50,13 +51,13 @@ private Thread entryThread() {
@Override
public void run() {
logger.info("Entry thread started");
ActiveSpan activeSpan = tracer.buildSpan("parent").startActive();
Runnable callback = new Callback(activeSpan);
Scope scope = tracer.buildSpan("parent").startActive();
Runnable callback = new Callback(scope.span());

// Callback is executed at some unpredictable time and we are not able to check status of the callback
service.schedule(callback, 500, TimeUnit.MILLISECONDS);

activeSpan.deactivate();
scope.close();
logger.info("Entry thread finished");
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.opentracing.contrib.examples.client_server;

import io.opentracing.ActiveSpan;
import io.opentracing.Scope;
import io.opentracing.Scope.Observer;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format.Builtin;
import io.opentracing.propagation.TextMapInjectAdapter;
Expand All @@ -20,16 +21,16 @@ public Client(ArrayBlockingQueue<Message> queue, Tracer tracer) {
public void send() {
Message message = new Message();

ActiveSpan activeSpan = tracer.buildSpan("send").startActive();
activeSpan.setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);
tracer.inject(activeSpan.context(), Builtin.TEXT_MAP, new TextMapInjectAdapter(message));
Scope scope = tracer.buildSpan("send").startActive(Observer.FINISH_ON_CLOSE);
scope.span().setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_CLIENT);
tracer.inject(scope.span().context(), Builtin.TEXT_MAP, new TextMapInjectAdapter(message));

try {
queue.put(message);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
activeSpan.deactivate();
scope.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.opentracing.contrib.examples.client_server;

import io.opentracing.ActiveSpan;
import io.opentracing.Scope;
import io.opentracing.Scope.Observer;
import io.opentracing.SpanContext;
import io.opentracing.Tracer;
import io.opentracing.propagation.Format.Builtin;
Expand All @@ -20,10 +21,10 @@ public Server(ArrayBlockingQueue<Message> queue, Tracer tracer) {

private void process(Message message) {
SpanContext context = tracer.extract(Builtin.TEXT_MAP, new TextMapExtractAdapter(message));
ActiveSpan activeSpan = tracer.buildSpan("receive").asChildOf(context).startActive();
activeSpan.setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER);

activeSpan.deactivate();
Scope scope = tracer.buildSpan("receive").asChildOf(context).startActive(
Observer.FINISH_ON_CLOSE);
scope.span().setTag(Tags.SPAN_KIND.getKey(), Tags.SPAN_KIND_SERVER);
scope.close();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import io.opentracing.mock.MockTracer;
import io.opentracing.mock.MockTracer.Propagator;
import io.opentracing.tag.Tags;
import io.opentracing.util.ThreadLocalActiveSpanSource;
import io.opentracing.util.ThreadLocalScopeManager;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
Expand All @@ -22,13 +22,13 @@

public class TestClientServer {

private final MockTracer tracer = new MockTracer(new ThreadLocalActiveSpanSource(),
Propagator.TEXT_MAP);
private final MockTracer tracer = new MockTracer(Propagator.TEXT_MAP);
private final ArrayBlockingQueue<Message> queue = new ArrayBlockingQueue<>(10);
private Server server;

@Before
public void before() {
tracer.setScopeManager(new ThreadLocalScopeManager());
server = new Server(queue, tracer);
server.start();
}
Expand All @@ -51,6 +51,6 @@ public void test() {
assertEquals(finished.get(0).context().traceId(), finished.get(1).context().traceId());
assertNotNull(getByTag(finished, Tags.SPAN_KIND, Tags.SPAN_KIND_CLIENT));
assertNotNull(getByTag(finished, Tags.SPAN_KIND, Tags.SPAN_KIND_SERVER));
assertNull(tracer.activeSpan());
assertNull(tracer.scopeManager().active());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import io.opentracing.mock.MockTracer;
import io.opentracing.mock.MockTracer.Propagator;
import io.opentracing.tag.Tags;
import io.opentracing.util.ThreadLocalActiveSpanSource;
import io.opentracing.util.ThreadLocalScopeManager;
import java.util.List;
import org.junit.Test;

Expand All @@ -18,18 +18,18 @@
*/
public class TestListener {

private final MockTracer tracer = new MockTracer(new ThreadLocalActiveSpanSource(),
Propagator.TEXT_MAP);
private final MockTracer tracer = new MockTracer(Propagator.TEXT_MAP);

@Test
public void test() throws Exception {
tracer.setScopeManager(new ThreadLocalScopeManager());
Client client = new Client(tracer);
Object response = client.send("message").get();
assertEquals("message:response", response);

List<MockSpan> finished = tracer.finishedSpans();
assertEquals(1, finished.size());
assertNotNull(getByTag(finished, Tags.SPAN_KIND, Tags.SPAN_KIND_CLIENT));
assertNull(tracer.activeSpan());
assertNull(tracer.scopeManager().active());
}
}