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 private static final Logger logger = Logger.getLogger(Tracing.class.getName()); 36 private static final TraceComponent traceComponent = 37 loadTraceComponent(TraceComponent.class.getClassLoader()); 38 39 /** 40 * Returns the global {@link Tracer}. 41 * 42 * @return the global {@code Tracer}. 43 * @since 0.5 44 */ getTracer()45 public static Tracer getTracer() { 46 return traceComponent.getTracer(); 47 } 48 49 /** 50 * Returns the global {@link PropagationComponent}. 51 * 52 * @return the global {@code PropagationComponent}. 53 * @since 0.5 54 */ getPropagationComponent()55 public static PropagationComponent getPropagationComponent() { 56 return traceComponent.getPropagationComponent(); 57 } 58 59 /** 60 * Returns the global {@link Clock}. 61 * 62 * @return the global {@code Clock}. 63 * @since 0.5 64 */ getClock()65 public static Clock getClock() { 66 return traceComponent.getClock(); 67 } 68 69 /** 70 * Returns the global {@link ExportComponent}. 71 * 72 * @return the global {@code ExportComponent}. 73 * @since 0.5 74 */ getExportComponent()75 public static ExportComponent getExportComponent() { 76 return traceComponent.getExportComponent(); 77 } 78 79 /** 80 * Returns the global {@link TraceConfig}. 81 * 82 * @return the global {@code TraceConfig}. 83 * @since 0.5 84 */ getTraceConfig()85 public static TraceConfig getTraceConfig() { 86 return traceComponent.getTraceConfig(); 87 } 88 89 // Any provider that may be used for TraceComponent can be added here. 90 @DefaultVisibilityForTesting loadTraceComponent(@ullable ClassLoader classLoader)91 static TraceComponent loadTraceComponent(@Nullable ClassLoader classLoader) { 92 try { 93 // Call Class.forName with literal string name of the class to help shading tools. 94 return Provider.createInstance( 95 Class.forName( 96 "io.opencensus.impl.trace.TraceComponentImpl", /*initialize=*/ true, classLoader), 97 TraceComponent.class); 98 } catch (ClassNotFoundException e) { 99 logger.log( 100 Level.FINE, 101 "Couldn't load full implementation for TraceComponent, now trying to load lite " 102 + "implementation.", 103 e); 104 } 105 try { 106 // Call Class.forName with literal string name of the class to help shading tools. 107 return Provider.createInstance( 108 Class.forName( 109 "io.opencensus.impllite.trace.TraceComponentImplLite", 110 /*initialize=*/ true, 111 classLoader), 112 TraceComponent.class); 113 } catch (ClassNotFoundException e) { 114 logger.log( 115 Level.FINE, 116 "Couldn't load lite implementation for TraceComponent, now using " 117 + "default implementation for TraceComponent.", 118 e); 119 } 120 return TraceComponent.newNoopTraceComponent(); 121 } 122 123 // No instance of this class. Tracing()124 private Tracing() {} 125 } 126