• 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;
18 
19 import io.opencensus.common.Clock;
20 import io.opencensus.internal.DefaultVisibilityForTesting;
21 import io.opencensus.internal.Provider;
22 import io.opencensus.trace.config.TraceConfig;
23 import io.opencensus.trace.export.ExportComponent;
24 import io.opencensus.trace.propagation.PropagationComponent;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27 import javax.annotation.Nullable;
28 
29 /**
30  * Class that manages a global instance of the {@link TraceComponent}.
31  *
32  * @since 0.5
33  */
34 public final class Tracing {
35 
36   private static final Logger logger = Logger.getLogger(Tracing.class.getName());
37   private static final TraceComponent traceComponent =
38       loadTraceComponent(TraceComponent.class.getClassLoader());
39 
40   /**
41    * Returns the global {@link Tracer}.
42    *
43    * @return the global {@code Tracer}.
44    * @since 0.5
45    */
getTracer()46   public static Tracer getTracer() {
47     return traceComponent.getTracer();
48   }
49 
50   /**
51    * Returns the global {@link PropagationComponent}.
52    *
53    * @return the global {@code PropagationComponent}.
54    * @since 0.5
55    */
getPropagationComponent()56   public static PropagationComponent getPropagationComponent() {
57     return traceComponent.getPropagationComponent();
58   }
59 
60   /**
61    * Returns the global {@link Clock}.
62    *
63    * @return the global {@code Clock}.
64    * @since 0.5
65    */
getClock()66   public static Clock getClock() {
67     return traceComponent.getClock();
68   }
69 
70   /**
71    * Returns the global {@link ExportComponent}.
72    *
73    * @return the global {@code ExportComponent}.
74    * @since 0.5
75    */
getExportComponent()76   public static ExportComponent getExportComponent() {
77     return traceComponent.getExportComponent();
78   }
79 
80   /**
81    * Returns the global {@link TraceConfig}.
82    *
83    * @return the global {@code TraceConfig}.
84    * @since 0.5
85    */
getTraceConfig()86   public static TraceConfig getTraceConfig() {
87     return traceComponent.getTraceConfig();
88   }
89 
90   // Any provider that may be used for TraceComponent can be added here.
91   @DefaultVisibilityForTesting
loadTraceComponent(@ullable ClassLoader classLoader)92   static TraceComponent loadTraceComponent(@Nullable ClassLoader classLoader) {
93     try {
94       // Call Class.forName with literal string name of the class to help shading tools.
95       return Provider.createInstance(
96           Class.forName(
97               "io.opentelemetry.opencensusshim.OpenTelemetryTraceComponentImpl",
98               /*initialize=*/ true,
99               classLoader),
100           TraceComponent.class);
101     } catch (ClassNotFoundException e) {
102       logger.log(
103           Level.FINE,
104           "Couldn't load full implementation for OpenTelemetry TraceComponent, now trying to load "
105               + "original implementation.",
106           e);
107     }
108     try {
109       // Call Class.forName with literal string name of the class to help shading tools.
110       return Provider.createInstance(
111           Class.forName(
112               "io.opencensus.impl.trace.TraceComponentImpl", /*initialize=*/ true, classLoader),
113           TraceComponent.class);
114     } catch (ClassNotFoundException e) {
115       logger.log(
116           Level.FINE,
117           "Couldn't load full implementation for TraceComponent, now trying to load lite "
118               + "implementation.",
119           e);
120     }
121     try {
122       // Call Class.forName with literal string name of the class to help shading tools.
123       return Provider.createInstance(
124           Class.forName(
125               "io.opencensus.impllite.trace.TraceComponentImplLite",
126               /*initialize=*/ true,
127               classLoader),
128           TraceComponent.class);
129     } catch (ClassNotFoundException e) {
130       logger.log(
131           Level.FINE,
132           "Couldn't load lite implementation for TraceComponent, now using "
133               + "default implementation for TraceComponent.",
134           e);
135     }
136     return TraceComponent.newNoopTraceComponent();
137   }
138 
139   // No instance of this class.
Tracing()140   private Tracing() {}
141 }
142