1616
1717package com .palantir .tracing ;
1818
19+ import com .google .errorprone .annotations .CompileTimeConstant ;
1920import java .util .Optional ;
2021import java .util .concurrent .Callable ;
2122import java .util .concurrent .ExecutorService ;
@@ -104,52 +105,75 @@ public static Runnable wrap(Runnable delegate) {
104105 return new TracingAwareRunnable (delegate );
105106 }
106107
108+ /**
109+ * Like {@link #wrapWithNewTrace(String, ExecutorService)}, but with a default initial span operation.
110+ */
111+ public static ExecutorService wrapWithNewTrace (ExecutorService executorService ) {
112+ return wrapWithNewTrace (ROOT_SPAN_OPERATION , executorService );
113+ }
114+
107115 /**
108116 * Wraps the provided executor service to make submitted tasks traceable with a fresh {@link Trace trace}
109- * for each execution, see {@link #wrapWithNewTrace(ExecutorService)}. This method should not be used to
117+ * for each execution, see {@link #wrapWithNewTrace(String, ExecutorService)}. This method should not be used to
110118 * wrap a ScheduledExecutorService that has already been {@link #wrap(ExecutorService) wrapped}. If this is
111119 * done, a new trace will be generated for each execution, effectively bypassing the intent of the previous
112- * wrapping.
120+ * wrapping. The given {@link String operation} is used to create the initial span.
113121 */
114- public static ExecutorService wrapWithNewTrace (ExecutorService executorService ) {
122+ public static ExecutorService wrapWithNewTrace (@ CompileTimeConstant String operation ,
123+ ExecutorService executorService ) {
115124 return new WrappingExecutorService (executorService ) {
116125 @ Override
117126 protected <T > Callable <T > wrapTask (Callable <T > callable ) {
118- return wrapWithNewTrace (callable );
127+ return wrapWithNewTrace (operation , callable );
119128 }
120129 };
121130 }
122131
123132 /**
124- * Wraps the provided scheduled executor service to make submitted tasks traceable with a fresh {@link Trace trace}
125- * for each execution, see {@link #wrapWithNewTrace(ScheduledExecutorService)}. This method should not be used to
126- * wrap a ScheduledExecutorService that has already been {@link #wrap(ScheduledExecutorService) wrapped}. If this is
127- * done, a new trace will be generated for each execution, effectively bypassing the intent of the previous
128- * wrapping.
133+ * Like {@link #wrapWithNewTrace(String, ScheduledExecutorService)}, but with a default initial span operation.
129134 */
130135 public static ScheduledExecutorService wrapWithNewTrace (ScheduledExecutorService executorService ) {
136+ return wrapWithNewTrace (ROOT_SPAN_OPERATION , executorService );
137+ }
138+
139+ /**
140+ * Wraps the provided scheduled executor service to make submitted tasks traceable with a fresh {@link Trace trace}
141+ * for each execution, see {@link #wrapWithNewTrace(String, ScheduledExecutorService)}. This method should not be
142+ * used to wrap a ScheduledExecutorService that has already been {@link #wrap(ScheduledExecutorService) wrapped}.
143+ * If this is done, a new trace will be generated for each execution, effectively bypassing the intent of the
144+ * previous wrapping. The given {@link String operation} is used to create the initial span.
145+ */
146+ public static ScheduledExecutorService wrapWithNewTrace (@ CompileTimeConstant String operation ,
147+ ScheduledExecutorService executorService ) {
131148 return new WrappingScheduledExecutorService (executorService ) {
132149 @ Override
133150 protected <T > Callable <T > wrapTask (Callable <T > callable ) {
134- return wrapWithNewTrace (callable );
151+ return wrapWithNewTrace (operation , callable );
135152 }
136153 };
137154 }
138155
156+ /**
157+ * Like {@link #wrapWithNewTrace(String, Callable)}, but with a default initial span operation.
158+ */
159+ public static <V > Callable <V > wrapWithNewTrace (Callable <V > delegate ) {
160+ return wrapWithNewTrace (ROOT_SPAN_OPERATION , delegate );
161+ }
162+
139163 /**
140164 * Wraps the given {@link Callable} such that it creates a fresh {@link Trace tracing state} for its execution.
141165 * That is, the trace during its {@link Callable#call() execution} is entirely separate from the trace at
142166 * construction or any trace already set on the thread used to execute the callable. Each execution of the callable
143- * will have a fresh trace.
167+ * will have a fresh trace. The given {@link String operation} is used to create the initial span.
144168 */
145- public static <V > Callable <V > wrapWithNewTrace (Callable <V > delegate ) {
169+ public static <V > Callable <V > wrapWithNewTrace (@ CompileTimeConstant String operation , Callable <V > delegate ) {
146170 return () -> {
147171 // clear the existing trace and keep it around for restoration when we're done
148172 Optional <Trace > originalTrace = Tracer .getAndClearTraceIfPresent ();
149173
150174 try {
151175 Tracer .initTrace (Optional .empty (), Tracers .randomId ());
152- Tracer .startSpan (ROOT_SPAN_OPERATION );
176+ Tracer .startSpan (operation );
153177 return delegate .call ();
154178 } finally {
155179 Tracer .fastCompleteSpan ();
@@ -159,16 +183,23 @@ public static <V> Callable<V> wrapWithNewTrace(Callable<V> delegate) {
159183 }
160184
161185 /**
162- * Like {@link #wrapWithNewTrace(Callable )}, but for Runnables .
186+ * Like {@link #wrapWithAlternateTraceId(String, Runnable )}, but with a default initial span operation .
163187 */
164188 public static Runnable wrapWithNewTrace (Runnable delegate ) {
189+ return wrapWithNewTrace (ROOT_SPAN_OPERATION , delegate );
190+ }
191+
192+ /**
193+ * Like {@link #wrapWithNewTrace(String, Callable)}, but for Runnables.
194+ */
195+ public static Runnable wrapWithNewTrace (@ CompileTimeConstant String operation , Runnable delegate ) {
165196 return () -> {
166197 // clear the existing trace and keep it around for restoration when we're done
167198 Optional <Trace > originalTrace = Tracer .getAndClearTraceIfPresent ();
168199
169200 try {
170201 Tracer .initTrace (Optional .empty (), Tracers .randomId ());
171- Tracer .startSpan (ROOT_SPAN_OPERATION );
202+ Tracer .startSpan (operation );
172203 delegate .run ();
173204 } finally {
174205 Tracer .fastCompleteSpan ();
@@ -177,20 +208,29 @@ public static Runnable wrapWithNewTrace(Runnable delegate) {
177208 };
178209 }
179210
211+ /**
212+ * Like {@link #wrapWithAlternateTraceId(String, String, Runnable)}, but with a default initial span operation.
213+ */
214+ public static Runnable wrapWithAlternateTraceId (String traceId , Runnable delegate ) {
215+ return wrapWithAlternateTraceId (traceId , ROOT_SPAN_OPERATION , delegate );
216+ }
217+
180218 /**
181219 * Wraps the given {@link Runnable} such that it creates a fresh {@link Trace tracing state with the given traceId}
182220 * for its execution. That is, the trace during its {@link Runnable#run() execution} will use the traceId provided
183221 * instead of any trace already set on the thread used to execute the runnable. Each execution of the runnable
184- * will use a new {@link Trace tracing state} with the same given traceId.
222+ * will use a new {@link Trace tracing state} with the same given traceId. The given {@link String operation} is
223+ * used to create the initial span.
185224 */
186- public static Runnable wrapWithAlternateTraceId (String traceId , Runnable delegate ) {
225+ public static Runnable wrapWithAlternateTraceId (String traceId , @ CompileTimeConstant String operation , Runnable
226+ delegate ) {
187227 return () -> {
188228 // clear the existing trace and keep it around for restoration when we're done
189229 Optional <Trace > originalTrace = Tracer .getAndClearTraceIfPresent ();
190230
191231 try {
192232 Tracer .initTrace (Optional .empty (), traceId );
193- Tracer .startSpan (ROOT_SPAN_OPERATION );
233+ Tracer .startSpan (operation );
194234 delegate .run ();
195235 } finally {
196236 Tracer .fastCompleteSpan ();
0 commit comments