• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017, OpenCensus Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.opencensus.trace.unsafe;
18 
19 import io.grpc.Context;
20 import io.opencensus.internal.Utils;
21 import io.opencensus.trace.BlankSpan;
22 import io.opencensus.trace.Span;
23 
24 /*>>>
25 import org.checkerframework.checker.nullness.qual.Nullable;
26 */
27 
28 /**
29  * Utilities for grabbing manipulating current context and grabbing current span.
30  *
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.
35  * @since 0.5
36  */
37 @Deprecated()
38 public final class ContextUtils {
39   // No instance of this class.
ContextUtils()40   private ContextUtils() {}
41 
42   /** The {@link io.grpc.Context.Key} used to interact with {@link io.grpc.Context}. */
43   private static final Context.Key</*@Nullable*/ Span> CONTEXT_SPAN_KEY =
44       Context.<Span>key("opencensus-trace-span-key");
45 
46   /**
47    * Creates a new {@code Context} with the given value set.
48    *
49    * @param context the parent {@code Context}.
50    * @param span the value to be set.
51    * @return a new context with the given value set.
52    * @since 0.21
53    */
withValue(Context context, @javax.annotation.Nullable Span span)54   public static Context withValue(Context context, @javax.annotation.Nullable Span span) {
55     return Utils.checkNotNull(context, "context").withValue(CONTEXT_SPAN_KEY, span);
56   }
57 
58   /**
59    * Returns the value from the specified {@code Context}.
60    *
61    * @param context the specified {@code Context}.
62    * @return the value from the specified {@code Context}.
63    * @since 0.21
64    */
getValue(Context context)65   public static Span getValue(Context context) {
66     @javax.annotation.Nullable
67     Span span = CONTEXT_SPAN_KEY.get(Utils.checkNotNull(context, "context"));
68     return span == null ? BlankSpan.INSTANCE : span;
69   }
70 }
71