• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016-17, 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.Provider;
21 import io.opencensus.trace.ContextHandle;
22 import io.opencensus.trace.ContextManager;
23 import io.opencensus.trace.Span;
24 import java.util.logging.Level;
25 import java.util.logging.Logger;
26 import javax.annotation.Nullable;
27 
28 public class ContextHandleUtils {
29 
30   // No instance of this class.
ContextHandleUtils()31   private ContextHandleUtils() {}
32 
33   private static final Logger LOGGER = Logger.getLogger(ContextHandleUtils.class.getName());
34   private static final ContextManager CONTEXT_MANAGER =
35       loadContextManager(ContextManager.class.getClassLoader());
36 
loadContextManager(@ullable ClassLoader classLoader)37   private static ContextManager loadContextManager(@Nullable ClassLoader classLoader) {
38     try {
39       return Provider.createInstance(
40           Class.forName(
41               "io.opentelemetry.opencensusshim.OpenTelemetryContextManager",
42               /*initialize=*/ true,
43               classLoader),
44           ContextManager.class);
45     } catch (ClassNotFoundException e) {
46       LOGGER.log(
47           Level.FINE,
48           "Couldn't load full implementation for OpenTelemetry context manager, now loading "
49               + "original implementation.",
50           e);
51     }
52     return new ContextManagerImpl();
53   }
54 
currentContext()55   public static ContextHandle currentContext() {
56     return CONTEXT_MANAGER.currentContext();
57   }
58 
59   /**
60    * Creates a new {@code ContextHandle} with the given value set.
61    *
62    * @param context the parent {@code ContextHandle}.
63    * @param span the value to be set.
64    * @return a new context with the given value set.
65    */
withValue( ContextHandle context, @javax.annotation.Nullable Span span)66   public static ContextHandle withValue(
67       ContextHandle context, @javax.annotation.Nullable Span span) {
68     return CONTEXT_MANAGER.withValue(context, span);
69   }
70 
71   /**
72    * Returns the value from the specified {@code ContextHandle}.
73    *
74    * @param context the specified {@code ContextHandle}.
75    * @return the value from the specified {@code ContextHandle}.
76    */
getValue(ContextHandle context)77   public static Span getValue(ContextHandle context) {
78     return CONTEXT_MANAGER.getValue(context);
79   }
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
tryExtractGrpcContext(ContextHandle handle)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   }
94 }
95