Skip to content

Commit db55e33

Browse files
authored
Bump OpenTracing to 0.32.0 (#14)
* Bump OpenTracing to 0.32.0 Signed-off-by: Pavol Loffay <ploffay@redhat.com> * Fix Signed-off-by: Pavol Loffay <ploffay@redhat.com>
1 parent c186dcd commit db55e33

File tree

12 files changed

+139
-103
lines changed

12 files changed

+139
-103
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
4040

4141
<version.junit>4.12</version.junit>
42-
<version.io.opentracing>0.31.0</version.io.opentracing>
42+
<version.io.opentracing>0.32.0</version.io.opentracing>
4343

4444
<!-- plugins -->
4545
<version.maven-deploy-plugin>2.8.2</version.maven-deploy-plugin>

src/main/java/io/opentracing/contrib/concurrent/TracedCallable.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,18 @@ public class TracedCallable<V> implements Callable<V> {
1616
private final Tracer tracer;
1717

1818
public TracedCallable(Callable<V> delegate, Tracer tracer) {
19+
this(delegate, tracer, tracer.activeSpan());
20+
}
21+
22+
public TracedCallable(Callable<V> delegate, Tracer tracer, Span span) {
1923
this.delegate = delegate;
2024
this.tracer = tracer;
21-
this.span = tracer.activeSpan();
25+
this.span = span;
2226
}
2327

2428
@Override
2529
public V call() throws Exception {
26-
Scope scope = span == null ? null : tracer.scopeManager().activate(span, false);
30+
Scope scope = span == null ? null : tracer.scopeManager().activate(span);
2731
try {
2832
return delegate.call();
2933
} finally {

src/main/java/io/opentracing/contrib/concurrent/TracedExecutor.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.opentracing.contrib.concurrent;
22

3-
import io.opentracing.Scope;
3+
import io.opentracing.Span;
44
import io.opentracing.Tracer;
55
import java.util.concurrent.Executor;
66

@@ -28,20 +28,21 @@ public TracedExecutor(Executor executor, Tracer tracer, boolean traceWithActiveS
2828

2929
@Override
3030
public void execute(Runnable runnable) {
31-
Scope scope = createScope("execute");
31+
Span span = createSpan("execute");
3232
try {
33-
delegate.execute(tracer.activeSpan() == null ? runnable :
34-
new TracedRunnable(runnable, tracer));
33+
Span toActivate = span != null ? span : tracer.activeSpan();
34+
delegate.execute(toActivate == null ? runnable : new TracedRunnable(runnable, tracer, toActivate));
3535
} finally {
36-
if (scope != null) {
37-
scope.close();
36+
// close the span if created
37+
if (span != null) {
38+
span.finish();
3839
}
3940
}
4041
}
4142

42-
Scope createScope(String operationName) {
43+
Span createSpan(String operationName) {
4344
if (tracer.activeSpan() == null && !traceWithActiveSpanOnly) {
44-
return tracer.buildSpan(operationName).startActive(true);
45+
return tracer.buildSpan(operationName).start();
4546
}
4647
return null;
4748
}

src/main/java/io/opentracing/contrib/concurrent/TracedExecutorService.java

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.opentracing.contrib.concurrent;
22

3-
import io.opentracing.Scope;
3+
import io.opentracing.Span;
44
import io.opentracing.Tracer;
55
import java.util.ArrayList;
66
import java.util.Collection;
@@ -59,101 +59,108 @@ public boolean awaitTermination(long l, TimeUnit timeUnit) throws InterruptedExc
5959

6060
@Override
6161
public <T> Future<T> submit(Callable<T> callable) {
62-
Scope scope = createScope("submit");
62+
Span span = createSpan("submit");
6363
try {
64-
return delegate.submit(tracer.activeSpan() == null ? callable :
65-
new TracedCallable<T>(callable, tracer));
64+
Span toActivate = span != null ? span : tracer.activeSpan();
65+
return delegate.submit(toActivate == null ? callable :
66+
new TracedCallable<T>(callable, tracer, toActivate));
6667
} finally {
67-
if (scope != null) {
68-
scope.close();
68+
if (span != null) {
69+
span.finish();
6970
}
7071
}
7172
}
7273

7374
@Override
7475
public <T> Future<T> submit(Runnable runnable, T t) {
75-
Scope scope = createScope("submit");
76+
Span span = createSpan("submit");
7677
try {
77-
return delegate.submit(tracer.activeSpan() == null ? runnable :
78-
new TracedRunnable(runnable, tracer), t);
78+
Span toActivate = span != null ? span : tracer.activeSpan();
79+
return delegate.submit(toActivate == null ? runnable :
80+
new TracedRunnable(runnable, tracer, toActivate), t);
7981
} finally {
80-
if (scope != null) {
81-
scope.close();
82+
if (span != null) {
83+
span.finish();
8284
}
8385
}
8486
}
8587

8688
@Override
8789
public Future<?> submit(Runnable runnable) {
88-
Scope scope = createScope("submit");
90+
Span span = createSpan("submit");
8991
try {
90-
return delegate.submit(tracer.activeSpan() == null ? runnable :
91-
new TracedRunnable(runnable, tracer));
92+
Span toActivate = span != null ? span : tracer.activeSpan();
93+
return delegate.submit(toActivate == null ? runnable :
94+
new TracedRunnable(runnable, tracer, toActivate));
9295
} finally {
93-
if (scope != null) {
94-
scope.close();
96+
if (span != null) {
97+
span.finish();
9598
}
9699
}
97100
}
98101

99102
@Override
100103
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> collection)
101104
throws InterruptedException {
102-
Scope scope = createScope("invokeAll");
105+
Span span = createSpan("invokeAll");
103106
try {
104-
return delegate.invokeAll(toTraced(collection));
107+
Span toActivate = span != null ? span : tracer.activeSpan();
108+
return delegate.invokeAll(toTraced(collection, toActivate));
105109
} finally {
106-
if (scope != null) {
107-
scope.close();
110+
if (span != null) {
111+
span.finish();
108112
}
109113
}
110114
}
111115

112116
@Override
113117
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> collection, long l,
114118
TimeUnit timeUnit) throws InterruptedException {
115-
Scope scope = createScope("invokeAll");
119+
Span span = createSpan("invokeAll");
116120
try {
117-
return delegate.invokeAll(toTraced(collection), l, timeUnit);
121+
Span toActivate = span != null ? span : tracer.activeSpan();
122+
return delegate.invokeAll(toTraced(collection, toActivate), l, timeUnit);
118123
} finally {
119-
if (scope != null) {
120-
scope.close();
124+
if (span != null) {
125+
span.finish();
121126
}
122127
}
123128
}
124129

125130
@Override
126131
public <T> T invokeAny(Collection<? extends Callable<T>> collection)
127132
throws InterruptedException, ExecutionException {
128-
Scope scope = createScope("invokeAny");
133+
Span span = createSpan("invokeAny");
129134
try {
130-
return delegate.invokeAny(toTraced(collection));
135+
Span toActivate = span != null ? span : tracer.activeSpan();
136+
return delegate.invokeAny(toTraced(collection, toActivate));
131137
} finally {
132-
if (scope != null) {
133-
scope.close();
138+
if (span != null) {
139+
span.finish();
134140
}
135141
}
136142
}
137143

138144
@Override
139145
public <T> T invokeAny(Collection<? extends Callable<T>> collection, long l, TimeUnit timeUnit)
140146
throws InterruptedException, ExecutionException, TimeoutException {
141-
Scope scope = createScope("invokeAny");
147+
Span span = createSpan("invokeAny");
142148
try {
143-
return delegate.invokeAny(toTraced(collection), l, timeUnit);
149+
Span toActivate = span != null ? span : tracer.activeSpan();
150+
return delegate.invokeAny(toTraced(collection, toActivate), l, timeUnit);
144151
} finally {
145-
if (scope != null) {
146-
scope.close();
152+
if (span != null) {
153+
span.finish();
147154
}
148155
}
149156
}
150157

151-
private <T> Collection<? extends Callable<T>> toTraced(Collection<? extends Callable<T>> delegate) {
158+
private <T> Collection<? extends Callable<T>> toTraced(Collection<? extends Callable<T>> delegate, Span toActivate) {
152159
List<Callable<T>> tracedCallables = new ArrayList<Callable<T>>(delegate.size());
153160

154161
for (Callable<T> callable: delegate) {
155-
tracedCallables.add(tracer.activeSpan() == null ? callable :
156-
new TracedCallable<T>(callable, tracer));
162+
tracedCallables.add(toActivate == null ? callable :
163+
new TracedCallable<T>(callable, tracer, toActivate));
157164
}
158165

159166
return tracedCallables;

src/main/java/io/opentracing/contrib/concurrent/TracedRunnable.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ public class TracedRunnable implements Runnable {
1414
private final Tracer tracer;
1515

1616
public TracedRunnable(Runnable delegate, Tracer tracer) {
17+
this(delegate, tracer, tracer.activeSpan());
18+
}
19+
20+
public TracedRunnable(Runnable delegate, Tracer tracer, Span span) {
1721
this.delegate = delegate;
1822
this.tracer = tracer;
19-
this.span = tracer.activeSpan();
23+
this.span = span;
2024
}
2125

2226
@Override
2327
public void run() {
24-
Scope scope = span == null ? null : tracer.scopeManager().activate(span, false);
28+
Scope scope = span == null ? null : tracer.scopeManager().activate(span);
2529
try {
2630
delegate.run();
2731
} finally {
Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.opentracing.contrib.concurrent;
22

3-
import io.opentracing.Scope;
3+
import io.opentracing.Span;
44
import io.opentracing.Tracer;
55
import java.util.concurrent.Callable;
66
import java.util.concurrent.ScheduledExecutorService;
@@ -15,7 +15,7 @@
1515
*/
1616
public class TracedScheduledExecutorService extends TracedExecutorService implements ScheduledExecutorService {
1717

18-
private final ScheduledExecutorService delegate;
18+
private final ScheduledExecutorService delegate;
1919

2020
public TracedScheduledExecutorService(ScheduledExecutorService delegate, Tracer tracer) {
2121
this(delegate, tracer, true);
@@ -29,55 +29,59 @@ public TracedScheduledExecutorService(ScheduledExecutorService delegate, Tracer
2929

3030
@Override
3131
public ScheduledFuture<?> schedule(Runnable runnable, long delay, TimeUnit timeUnit) {
32-
Scope scope = createScope("schedule");
32+
Span span = createSpan("schedule");
3333
try {
34+
Span toActivate = span != null ? span : tracer.activeSpan();
3435
return delegate.schedule(tracer.activeSpan() == null ? runnable :
35-
new TracedRunnable(runnable, tracer), delay, timeUnit);
36+
new TracedRunnable(runnable, tracer, toActivate), delay, timeUnit);
3637
} finally {
37-
if (scope != null) {
38-
scope.close();
38+
if (span != null) {
39+
span.finish();
3940
}
4041
}
4142
}
4243

4344
@Override
4445
public <T> ScheduledFuture<T> schedule(Callable<T> callable, long delay, TimeUnit timeUnit) {
45-
Scope scope = createScope("schedule");
46+
Span span = createSpan("schedule");
4647
try {
48+
Span toActivate = span != null ? span : tracer.activeSpan();
4749
return delegate.schedule(tracer.activeSpan() == null ? callable :
48-
new TracedCallable<T>(callable, tracer), delay, timeUnit);
50+
new TracedCallable<T>(callable, tracer, toActivate), delay, timeUnit);
4951
} finally {
50-
if (scope != null) {
51-
scope.close();
52+
if (span != null) {
53+
span.finish();
5254
}
5355
}
5456
}
5557

5658
@Override
5759
public ScheduledFuture<?> scheduleAtFixedRate(Runnable runnable, long initialDelay, long period,
5860
TimeUnit timeUnit) {
59-
Scope scope = createScope("scheduleAtFixedRate");
61+
Span span = createSpan("scheduleAtFixedRate");
6062
try {
63+
Span toActivate = span != null ? span : tracer.activeSpan();
6164
return delegate.scheduleAtFixedRate(tracer.activeSpan() == null ? runnable :
62-
new TracedRunnable(runnable, tracer), initialDelay, period, timeUnit);
65+
new TracedRunnable(runnable, tracer, toActivate), initialDelay, period, timeUnit);
6366
} finally {
64-
if (scope != null) {
65-
scope.close();
67+
if (span != null) {
68+
span.finish();
6669
}
6770
}
6871
}
6972

7073
@Override
7174
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable runnable, long initialDelay, long delay,
7275
TimeUnit timeUnit) {
73-
Scope scope = createScope("scheduleWithFixedDelay");
76+
Span span = createSpan("scheduleWithFixedDelay");
7477
try {
78+
Span toActivate = span != null ? span : tracer.activeSpan();
7579
return delegate.scheduleWithFixedDelay(tracer.activeSpan() == null ? runnable :
76-
new TracedRunnable(runnable, tracer), initialDelay, delay, timeUnit);
80+
new TracedRunnable(runnable, tracer, toActivate), initialDelay, delay, timeUnit);
7781
} finally {
78-
if (scope != null) {
79-
scope.close();
82+
if (span != null) {
83+
span.finish();
8084
}
8185
}
8286
}
83-
}
87+
}

src/test/java/io/opentracing/contrib/concurrent/AbstractConcurrentTest.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ class TestRunnable implements Runnable {
2121
@Override
2222
public void run() {
2323
try {
24-
mockTracer.buildSpan("childRunnable")
25-
.startActive(true)
26-
.close();
24+
mockTracer.buildSpan("childRunnable").start().finish();
2725
} finally {
2826
countDownLatch.countDown();
2927
}
@@ -34,9 +32,7 @@ class TestCallable implements Callable<Void> {
3432
@Override
3533
public Void call() throws Exception {
3634
try {
37-
mockTracer.buildSpan("childCallable")
38-
.startActive(true)
39-
.close();
35+
mockTracer.buildSpan("childCallable").start().finish();
4036
} finally {
4137
countDownLatch.countDown();
4238
}

src/test/java/io/opentracing/contrib/concurrent/TracedCallableTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.assertEquals;
44

5+
import io.opentracing.Scope;
56
import java.util.concurrent.Callable;
67
import java.util.concurrent.ExecutionException;
78
import java.util.concurrent.FutureTask;
@@ -21,14 +22,15 @@ protected <V> Callable<V> toTraced(Callable<V> callable) {
2122

2223
@Test
2324
public void testTracedCallable() throws InterruptedException, ExecutionException {
24-
MockSpan parent = mockTracer.buildSpan("foo").startManual();
25-
mockTracer.scopeManager().activate(parent, true);
25+
MockSpan parent = mockTracer.buildSpan("foo").start();
26+
Scope scope = mockTracer.scopeManager().activate(parent);
2627

2728
FutureTask<Void> futureTask = new FutureTask<Void>(toTraced(new TestCallable()));
2829
Thread thread = createThread(futureTask);
2930
thread.start();
3031
futureTask.get();
3132
thread.join();
33+
scope.close();
3234

3335
assertParentSpan(parent);
3436
assertEquals(1, mockTracer.finishedSpans().size());

0 commit comments

Comments
 (0)