This repository was archived by the owner on Dec 23, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +30
-5
lines changed
main/java/io/opencensus/trace/unsafe
test/java/io/opencensus/trace/unsafe Expand file tree Collapse file tree 4 files changed +30
-5
lines changed Original file line number Diff line number Diff line change 1616
1717package io .opencensus .trace .unsafe ;
1818
19+ import io .grpc .Context ;
1920import io .opencensus .internal .Provider ;
2021import io .opencensus .trace .ContextHandle ;
2122import 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}
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments