Skip to content

Commit b75b467

Browse files
pkoenig10bulldozer-bot[bot]
authored andcommitted
Add methods to create wrapped executor with new span (#71)
Fixes #63 ~This PR preserves the existing executor wrapping methods and their behavior. So users must explicitly opt-in to the new span-creating behavior.~ This is no longer true. The existing executor methods will now create a new span for tasks they run.
1 parent 3c30134 commit b75b467

File tree

3 files changed

+285
-139
lines changed

3 files changed

+285
-139
lines changed

tracing/src/main/java/com/palantir/tracing/DeferredTracer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,19 @@
3939
*/
4040
public final class DeferredTracer {
4141
private final Optional<Trace> trace;
42+
private final Optional<String> operation;
4243

4344
public DeferredTracer() {
45+
this(Optional.empty());
46+
}
47+
48+
public DeferredTracer(String operation) {
49+
this(Optional.of(operation));
50+
}
51+
52+
DeferredTracer(Optional<String> operation) {
4453
this.trace = Tracer.copyTrace();
54+
this.operation = operation;
4555
}
4656

4757
/**
@@ -54,9 +64,11 @@ public <T, E extends Throwable> T withTrace(Tracers.ThrowingCallable<T, E> inner
5464
}
5565
Optional<Trace> originalTrace = Tracer.copyTrace();
5666
Tracer.setTrace(trace.get());
67+
operation.ifPresent(Tracer::startSpan);
5768
try {
5869
return inner.call();
5970
} finally {
71+
operation.ifPresent(op -> Tracer.fastCompleteSpan());
6072
if (originalTrace.isPresent()) {
6173
Tracer.setTrace(originalTrace.get());
6274
} else {

tracing/src/main/java/com/palantir/tracing/Tracers.java

Lines changed: 78 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
public final class Tracers {
2727
/** The key under which trace ids are inserted into SLF4J {@link org.slf4j.MDC MDCs}. */
2828
public static final String TRACE_ID_KEY = "traceId";
29-
private static final String ROOT_SPAN_OPERATION = "root";
29+
private static final String DEFAULT_ROOT_SPAN_OPERATION = "root";
30+
private static final String DEFAULT_EXECUTOR_SPAN_OPERATION = "executor";
3031
private static final char[] HEX_DIGITS =
3132
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
3233

@@ -68,10 +69,18 @@ static String longToPaddedHex(long number) {
6869
* #wrap wrapped} in order to be trace-aware.
6970
*/
7071
public static ExecutorService wrap(ExecutorService executorService) {
72+
return wrap(DEFAULT_EXECUTOR_SPAN_OPERATION, executorService);
73+
}
74+
75+
/**
76+
* Like {@link #wrap(ExecutorService)}, but using the given {@link String operation} is used to create a span for
77+
* submitted tasks.
78+
*/
79+
public static ExecutorService wrap(String operation, ExecutorService executorService) {
7180
return new WrappingExecutorService(executorService) {
7281
@Override
7382
protected <T> Callable<T> wrapTask(Callable<T> callable) {
74-
return wrap(callable);
83+
return wrap(operation, callable);
7584
}
7685
};
7786
}
@@ -83,10 +92,19 @@ protected <T> Callable<T> wrapTask(Callable<T> callable) {
8392
* trace will be generated for each execution, effectively bypassing the intent of this method.
8493
*/
8594
public static ScheduledExecutorService wrap(ScheduledExecutorService executorService) {
95+
return wrap(DEFAULT_EXECUTOR_SPAN_OPERATION, executorService);
96+
}
97+
98+
/**
99+
* Like {@link #wrap(ScheduledExecutorService)}, but using the given {@link String operation} is used to create a
100+
* span for submitted tasks.
101+
*/
102+
public static ScheduledExecutorService wrap(String operation,
103+
ScheduledExecutorService executorService) {
86104
return new WrappingScheduledExecutorService(executorService) {
87105
@Override
88106
protected <T> Callable<T> wrapTask(Callable<T> callable) {
89-
return wrap(callable);
107+
return wrap(operation, callable);
90108
}
91109
};
92110
}
@@ -96,27 +114,49 @@ protected <T> Callable<T> wrapTask(Callable<T> callable) {
96114
* it's construction during its {@link Callable#call() execution}.
97115
*/
98116
public static <V> Callable<V> wrap(Callable<V> delegate) {
99-
return new TracingAwareCallable<>(delegate);
117+
return new TracingAwareCallable<>(Optional.empty(), delegate);
118+
}
119+
120+
/**
121+
* Like {@link #wrap(Callable)}, but using the given {@link String operation} is used to create a span for the
122+
* execution.
123+
*/
124+
public static <V> Callable<V> wrap(String operation, Callable<V> delegate) {
125+
return new TracingAwareCallable<>(Optional.of(operation), delegate);
100126
}
101127

102-
/** Like {@link #wrap(Callable)}, but for Runnables. */
128+
/**
129+
* Wraps the given {@link Runnable} such that it uses the thread-local {@link Trace tracing state} at the time of
130+
* it's construction during its {@link Runnable#run()} execution}.
131+
*/
103132
public static Runnable wrap(Runnable delegate) {
104-
return new TracingAwareRunnable(delegate);
133+
return new TracingAwareRunnable(Optional.empty(), delegate);
105134
}
106135

107136
/**
108-
* Like {@link #wrapWithNewTrace(String, ExecutorService)}, but with a default initial span operation.
137+
* Like {@link #wrap(Runnable)}, but using the given {@link String operation} is used to create a span for the
138+
* execution.
109139
*/
140+
public static Runnable wrap(String operation, Runnable delegate) {
141+
return new TracingAwareRunnable(Optional.of(operation), delegate);
142+
}
143+
144+
/**
145+
* Deprecated.
146+
*
147+
* @deprecated Use {@link #wrapWithNewTrace(String, ExecutorService)}
148+
*/
149+
@Deprecated
110150
public static ExecutorService wrapWithNewTrace(ExecutorService executorService) {
111-
return wrapWithNewTrace(ROOT_SPAN_OPERATION, executorService);
151+
return wrapWithNewTrace(DEFAULT_ROOT_SPAN_OPERATION, executorService);
112152
}
113153

114154
/**
115155
* Wraps the provided executor service to make submitted tasks traceable with a fresh {@link Trace trace}
116156
* for each execution, see {@link #wrapWithNewTrace(String, ExecutorService)}. This method should not be used to
117157
* wrap a ScheduledExecutorService that has already been {@link #wrap(ExecutorService) wrapped}. If this is
118158
* done, a new trace will be generated for each execution, effectively bypassing the intent of the previous
119-
* wrapping. The given {@link String operation} is used to create the initial span.
159+
* wrapping. The given {@link String operation} is used to create the initial span.
120160
*/
121161
public static ExecutorService wrapWithNewTrace(String operation, ExecutorService executorService) {
122162
return new WrappingExecutorService(executorService) {
@@ -128,18 +168,21 @@ protected <T> Callable<T> wrapTask(Callable<T> callable) {
128168
}
129169

130170
/**
131-
* Like {@link #wrapWithNewTrace(String, ScheduledExecutorService)}, but with a default initial span operation.
171+
* Deprecated.
172+
*
173+
* @deprecated Use {@link #wrapWithNewTrace(String, ScheduledExecutorService)}
132174
*/
175+
@Deprecated
133176
public static ScheduledExecutorService wrapWithNewTrace(ScheduledExecutorService executorService) {
134-
return wrapWithNewTrace(ROOT_SPAN_OPERATION, executorService);
177+
return wrapWithNewTrace(DEFAULT_ROOT_SPAN_OPERATION, executorService);
135178
}
136179

137180
/**
138181
* Wraps the provided scheduled executor service to make submitted tasks traceable with a fresh {@link Trace trace}
139182
* for each execution, see {@link #wrapWithNewTrace(String, ScheduledExecutorService)}. This method should not be
140183
* used to wrap a ScheduledExecutorService that has already been {@link #wrap(ScheduledExecutorService) wrapped}.
141184
* If this is done, a new trace will be generated for each execution, effectively bypassing the intent of the
142-
* previous wrapping. The given {@link String operation} is used to create the initial span.
185+
* previous wrapping. The given {@link String operation} is used to create the initial span.
143186
*/
144187
public static ScheduledExecutorService wrapWithNewTrace(String operation,
145188
ScheduledExecutorService executorService) {
@@ -152,10 +195,13 @@ protected <T> Callable<T> wrapTask(Callable<T> callable) {
152195
}
153196

154197
/**
155-
* Like {@link #wrapWithNewTrace(String, Callable)}, but with a default initial span operation.
198+
* Deprecated.
199+
*
200+
* @deprecated Use {@link #wrapWithNewTrace(String, Callable)}
156201
*/
202+
@Deprecated
157203
public static <V> Callable<V> wrapWithNewTrace(Callable<V> delegate) {
158-
return wrapWithNewTrace(ROOT_SPAN_OPERATION, delegate);
204+
return wrapWithNewTrace(DEFAULT_ROOT_SPAN_OPERATION, delegate);
159205
}
160206

161207
/**
@@ -181,14 +227,20 @@ public static <V> Callable<V> wrapWithNewTrace(String operation, Callable<V> del
181227
}
182228

183229
/**
184-
* Like {@link #wrapWithAlternateTraceId(String, Runnable)}, but with a default initial span operation.
230+
* Deprecated.
231+
*
232+
* @deprecated Use {@link #wrapWithNewTrace(String, Runnable)}
185233
*/
234+
@Deprecated
186235
public static Runnable wrapWithNewTrace(Runnable delegate) {
187-
return wrapWithNewTrace(ROOT_SPAN_OPERATION, delegate);
236+
return wrapWithNewTrace(DEFAULT_ROOT_SPAN_OPERATION, delegate);
188237
}
189238

190239
/**
191-
* Like {@link #wrapWithNewTrace(String, Callable)}, but for Runnables.
240+
* Wraps the given {@link Runnable} such that it creates a fresh {@link Trace tracing state} for its execution.
241+
* That is, the trace during its {@link Runnable#run() execution} is entirely separate from the trace at
242+
* construction or any trace already set on the thread used to execute the runnable. Each execution of the runnable
243+
* will have a fresh trace. The given {@link String operation} is used to create the initial span.
192244
*/
193245
public static Runnable wrapWithNewTrace(String operation, Runnable delegate) {
194246
return () -> {
@@ -207,10 +259,13 @@ public static Runnable wrapWithNewTrace(String operation, Runnable delegate) {
207259
}
208260

209261
/**
210-
* Like {@link #wrapWithAlternateTraceId(String, String, Runnable)}, but with a default initial span operation.
262+
* Deprecated.
263+
*
264+
* @deprecated Use {@link #wrapWithAlternateTraceId(String, String, Runnable)}
211265
*/
266+
@Deprecated
212267
public static Runnable wrapWithAlternateTraceId(String traceId, Runnable delegate) {
213-
return wrapWithAlternateTraceId(traceId, ROOT_SPAN_OPERATION, delegate);
268+
return wrapWithAlternateTraceId(traceId, DEFAULT_ROOT_SPAN_OPERATION, delegate);
214269
}
215270

216271
/**
@@ -264,9 +319,9 @@ private static class TracingAwareCallable<V> implements Callable<V> {
264319
private final Callable<V> delegate;
265320
private final DeferredTracer deferredTracer;
266321

267-
TracingAwareCallable(Callable<V> delegate) {
322+
TracingAwareCallable(Optional<String> operation, Callable<V> delegate) {
268323
this.delegate = delegate;
269-
this.deferredTracer = new DeferredTracer();
324+
this.deferredTracer = new DeferredTracer(operation);
270325
}
271326

272327
@Override
@@ -283,9 +338,9 @@ private static class TracingAwareRunnable implements Runnable {
283338
private final Runnable delegate;
284339
private DeferredTracer deferredTracer;
285340

286-
TracingAwareRunnable(Runnable delegate) {
341+
TracingAwareRunnable(Optional<String> operation, Runnable delegate) {
287342
this.delegate = delegate;
288-
this.deferredTracer = new DeferredTracer();
343+
this.deferredTracer = new DeferredTracer(operation);
289344
}
290345

291346
@Override

0 commit comments

Comments
 (0)