2424import io .opentelemetry .api .trace .Tracer ;
2525import io .opentelemetry .context .Context ;
2626import io .opentelemetry .context .Scope ;
27- import java .io .IOException ;
2827import java .util .List ;
28+ import java .util .concurrent .Callable ;
2929import java .util .concurrent .CompletableFuture ;
3030import java .util .function .Supplier ;
3131import org .apache .hadoop .hbase .Version ;
@@ -84,7 +84,7 @@ public static <T> CompletableFuture<T> tracedFuture(
8484 Supplier <Span > spanSupplier
8585 ) {
8686 Span span = spanSupplier .get ();
87- try (Scope scope = span .makeCurrent ()) {
87+ try (Scope ignored = span .makeCurrent ()) {
8888 CompletableFuture <T > future = action .get ();
8989 endSpan (future , span );
9090 return future ;
@@ -97,7 +97,7 @@ public static <T> CompletableFuture<T> tracedFuture(
9797 public static <T > CompletableFuture <T > tracedFuture (Supplier <CompletableFuture <T >> action ,
9898 String spanName ) {
9999 Span span = createSpan (spanName );
100- try (Scope scope = span .makeCurrent ()) {
100+ try (Scope ignored = span .makeCurrent ()) {
101101 CompletableFuture <T > future = action .get ();
102102 endSpan (future , span );
103103 return future ;
@@ -113,7 +113,7 @@ public static <T> List<CompletableFuture<T>> tracedFutures(
113113 Supplier <Span > spanSupplier
114114 ) {
115115 Span span = spanSupplier .get ();
116- try (Scope scope = span .makeCurrent ()) {
116+ try (Scope ignored = span .makeCurrent ()) {
117117 List <CompletableFuture <T >> futures = action .get ();
118118 endSpan (CompletableFuture .allOf (futures .toArray (new CompletableFuture [0 ])), span );
119119 return futures ;
@@ -139,29 +139,29 @@ private static void endSpan(CompletableFuture<?> future, Span span) {
139139 });
140140 }
141141
142- public static void trace (Runnable action , String spanName ) {
143- trace (action , () -> createSpan (spanName ));
142+ /**
143+ * A {@link Runnable} that may also throw.
144+ * @param <T> the type of {@link Throwable} that can be produced.
145+ */
146+ @ FunctionalInterface
147+ public interface ThrowingRunnable <T extends Throwable > {
148+ void run () throws T ;
144149 }
145150
146- public static void trace (Runnable action , Supplier <Span > creator ) {
147- Span span = creator .get ();
148- try (Scope scope = span .makeCurrent ()) {
149- action .run ();
150- span .setStatus (StatusCode .OK );
151- } catch (Throwable e ) {
152- setError (span , e );
153- throw e ;
154- } finally {
155- span .end ();
156- }
151+ public static <T extends Throwable > void trace (
152+ final ThrowingRunnable <T > runnable ,
153+ final String spanName ) throws T {
154+ trace (runnable , () -> createSpan (spanName ));
157155 }
158156
159- public static <T > T trace (Supplier <T > action , String spanName ) {
160- Span span = createSpan (spanName );
161- try (Scope scope = span .makeCurrent ()) {
162- T ret = action .get ();
157+ public static <T extends Throwable > void trace (
158+ final ThrowingRunnable <T > runnable ,
159+ final Supplier <Span > spanSupplier
160+ ) throws T {
161+ Span span = spanSupplier .get ();
162+ try (Scope ignored = span .makeCurrent ()) {
163+ runnable .run ();
163164 span .setStatus (StatusCode .OK );
164- return ret ;
165165 } catch (Throwable e ) {
166166 setError (span , e );
167167 throw e ;
@@ -170,20 +170,30 @@ public static <T> T trace(Supplier<T> action, String spanName) {
170170 }
171171 }
172172
173+ /**
174+ * A {@link Callable} that may also throw.
175+ * @param <R> the result type of method call.
176+ * @param <T> the type of {@link Throwable} that can be produced.
177+ */
173178 @ FunctionalInterface
174- public interface IOExceptionCallable < V > {
175- V call () throws IOException ;
179+ public interface ThrowingCallable < R , T extends Throwable > {
180+ R call () throws T ;
176181 }
177182
178- public static <T > T trace (IOExceptionCallable <T > callable , String spanName ) throws IOException {
183+ public static <R , T extends Throwable > R trace (
184+ final ThrowingCallable <R , T > callable ,
185+ final String spanName
186+ ) throws T {
179187 return trace (callable , () -> createSpan (spanName ));
180188 }
181189
182- public static <T > T trace (IOExceptionCallable <T > callable , Supplier <Span > creator )
183- throws IOException {
184- Span span = creator .get ();
185- try (Scope scope = span .makeCurrent ()) {
186- T ret = callable .call ();
190+ public static <R , T extends Throwable > R trace (
191+ final ThrowingCallable <R , T > callable ,
192+ final Supplier <Span > spanSupplier
193+ ) throws T {
194+ Span span = spanSupplier .get ();
195+ try (Scope ignored = span .makeCurrent ()) {
196+ final R ret = callable .call ();
187197 span .setStatus (StatusCode .OK );
188198 return ret ;
189199 } catch (Throwable e ) {
0 commit comments