Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 7ddc4cc

Browse files
authored
Mark ContextUtils as public (but deprecated) (#2072)
* Mark ContextUtils as public (but deprecated) to fix bincompat issues with 0.17.x->0.18.x * Add an access mechanism for grpc context and update deprecation denotation.
1 parent e3bf35f commit 7ddc4cc

File tree

4 files changed

+30
-5
lines changed

4 files changed

+30
-5
lines changed

api/src/main/java/io/opencensus/trace/unsafe/ContextHandleUtils.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package io.opencensus.trace.unsafe;
1818

19+
import io.grpc.Context;
1920
import io.opencensus.internal.Provider;
2021
import io.opencensus.trace.ContextHandle;
2122
import io.opencensus.trace.ContextManager;
@@ -76,4 +77,18 @@ public static ContextHandle withValue(
7677
public static Span getValue(ContextHandle context) {
7778
return CONTEXT_MANAGER.getValue(context);
7879
}
80+
81+
/**
82+
* Attempts to pull the {@link io.grpc.Context} out of an OpenCensus {@code ContextHandle}.
83+
*
84+
* @return The context, or null if not a GRPC backed context handle.
85+
*/
86+
@Nullable
87+
public static Context tryExtractGrpcContext(ContextHandle handle) {
88+
if (handle instanceof ContextHandleImpl) {
89+
return ((ContextHandleImpl) handle).getContext();
90+
}
91+
// TODO: see if we can do something for the OpenTelemetry shim.
92+
return null;
93+
}
7994
}

api/src/main/java/io/opencensus/trace/unsafe/ContextManagerImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ public ContextHandle currentContext() {
3131
}
3232

3333
@Override
34+
@SuppressWarnings({"deprecation"})
3435
public ContextHandle withValue(ContextHandle contextHandle, @Nullable Span span) {
3536
return wrapContext(ContextUtils.withValue(unwrapContext(contextHandle), span));
3637
}
3738

3839
@Override
40+
@SuppressWarnings({"deprecation"})
3941
public Span getValue(ContextHandle contextHandle) {
4042
return ContextUtils.getValue(unwrapContext(contextHandle));
4143
}

api/src/main/java/io/opencensus/trace/unsafe/ContextUtils.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@
2626
*/
2727

2828
/**
29-
* Util methods/functionality to interact with the {@link io.grpc.Context}.
30-
*
31-
* <p>Users must interact with the current Context via the public APIs in {@link
32-
* io.opencensus.trace.Tracer} and avoid usages of the {@link #CONTEXT_SPAN_KEY} directly.
29+
* Utilities for grabbing manipulating current context and grabbing current span.
3330
*
31+
* @deprecated Please use {@link io.opencensus.trace.unsafe.ContextHandleUtils} Util
32+
* methods/functionality to interact with the {@link io.grpc.Context}. Users must interact with
33+
* the current Context via the public APIs in {@link io.opencensus.trace.Tracer} and avoid
34+
* usages of the {@link #CONTEXT_SPAN_KEY} directly.
3435
* @since 0.5
3536
*/
36-
final class ContextUtils {
37+
@Deprecated()
38+
public final class ContextUtils {
3739
// No instance of this class.
3840
private ContextUtils() {}
3941

api/src/test/java/io/opencensus/trace/unsafe/ContextUtilsTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,10 @@ public void testGetCurrentSpan_ContextSetToNull() {
4747
ContextHandleUtils.currentContext().detach(orig);
4848
}
4949
}
50+
51+
@Test
52+
public void testTryExtractGrpcContext_WillNotThrow() {
53+
assertThat(ContextHandleUtils.tryExtractGrpcContext(ContextHandleUtils.currentContext()))
54+
.isNotNull();
55+
}
5056
}

0 commit comments

Comments
 (0)