1616
1717package io .opencensus .trace ;
1818
19- import io .grpc .Context ;
2019import io .opencensus .common .Scope ;
21- import io .opencensus .trace .unsafe .ContextUtils ;
20+ import io .opencensus .trace .unsafe .ContextHandleUtils ;
2221import java .util .concurrent .Callable ;
2322import javax .annotation .Nullable ;
2423
2524/** Util methods/functionality to interact with the {@link Span} in the {@link io.grpc.Context}. */
2625final class CurrentSpanUtils {
26+
2727 // No instance of this class.
2828 private CurrentSpanUtils () {}
2929
@@ -34,7 +34,7 @@ private CurrentSpanUtils() {}
3434 */
3535 @ Nullable
3636 static Span getCurrentSpan () {
37- return ContextUtils .getValue (Context . current ());
37+ return ContextHandleUtils .getValue (ContextHandleUtils . currentContext ());
3838 }
3939
4040 /**
@@ -78,7 +78,8 @@ static <C> Callable<C> withSpan(Span span, boolean endSpan, Callable<C> callable
7878
7979 // Defines an arbitrary scope of code as a traceable operation. Supports try-with-resources idiom.
8080 private static final class ScopeInSpan implements Scope {
81- private final Context origContext ;
81+
82+ private final ContextHandle origContext ;
8283 private final Span span ;
8384 private final boolean endSpan ;
8485
@@ -90,19 +91,21 @@ private static final class ScopeInSpan implements Scope {
9091 private ScopeInSpan (Span span , boolean endSpan ) {
9192 this .span = span ;
9293 this .endSpan = endSpan ;
93- origContext = ContextUtils .withValue (Context .current (), span ).attach ();
94+ origContext =
95+ ContextHandleUtils .withValue (ContextHandleUtils .currentContext (), span ).attach ();
9496 }
9597
9698 @ Override
9799 public void close () {
98- Context . current ().detach (origContext );
100+ ContextHandleUtils . currentContext ().detach (origContext );
99101 if (endSpan ) {
100102 span .end ();
101103 }
102104 }
103105 }
104106
105107 private static final class RunnableInSpan implements Runnable {
108+
106109 // TODO(bdrutu): Investigate if the extra private visibility increases the generated bytecode.
107110 private final Span span ;
108111 private final Runnable runnable ;
@@ -116,7 +119,8 @@ private RunnableInSpan(Span span, Runnable runnable, boolean endSpan) {
116119
117120 @ Override
118121 public void run () {
119- Context origContext = ContextUtils .withValue (Context .current (), span ).attach ();
122+ ContextHandle origContext =
123+ ContextHandleUtils .withValue (ContextHandleUtils .currentContext (), span ).attach ();
120124 try {
121125 runnable .run ();
122126 } catch (Throwable t ) {
@@ -128,7 +132,7 @@ public void run() {
128132 }
129133 throw new RuntimeException ("unexpected" , t );
130134 } finally {
131- Context . current ().detach (origContext );
135+ ContextHandleUtils . currentContext ().detach (origContext );
132136 if (endSpan ) {
133137 span .end ();
134138 }
@@ -137,6 +141,7 @@ public void run() {
137141 }
138142
139143 private static final class CallableInSpan <V > implements Callable <V > {
144+
140145 private final Span span ;
141146 private final Callable <V > callable ;
142147 private final boolean endSpan ;
@@ -149,7 +154,8 @@ private CallableInSpan(Span span, Callable<V> callable, boolean endSpan) {
149154
150155 @ Override
151156 public V call () throws Exception {
152- Context origContext = ContextUtils .withValue (Context .current (), span ).attach ();
157+ ContextHandle origContext =
158+ ContextHandleUtils .withValue (ContextHandleUtils .currentContext (), span ).attach ();
153159 try {
154160 return callable .call ();
155161 } catch (Exception e ) {
@@ -162,7 +168,7 @@ public V call() throws Exception {
162168 }
163169 throw new RuntimeException ("unexpected" , t );
164170 } finally {
165- Context . current ().detach (origContext );
171+ ContextHandleUtils . currentContext ().detach (origContext );
166172 if (endSpan ) {
167173 span .end ();
168174 }
0 commit comments