1 /* 2 * Copyright 2018, 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.metrics; 18 19 import io.opencensus.common.ExperimentalApi; 20 import io.opencensus.internal.DefaultVisibilityForTesting; 21 import io.opencensus.internal.Provider; 22 import io.opencensus.metrics.export.ExportComponent; 23 import java.util.logging.Level; 24 import java.util.logging.Logger; 25 import javax.annotation.Nullable; 26 27 /** 28 * Class for accessing the default {@link MetricsComponent}. 29 * 30 * @since 0.17 31 */ 32 @ExperimentalApi 33 public final class Metrics { 34 private static final Logger logger = Logger.getLogger(Metrics.class.getName()); 35 private static final MetricsComponent metricsComponent = 36 loadMetricsComponent(MetricsComponent.class.getClassLoader()); 37 38 /** 39 * Returns the global {@link ExportComponent}. 40 * 41 * @return the global {@code ExportComponent}. 42 * @since 0.17 43 */ getExportComponent()44 public static ExportComponent getExportComponent() { 45 return metricsComponent.getExportComponent(); 46 } 47 48 /** 49 * Returns the global {@link MetricRegistry}. 50 * 51 * <p>This {@code MetricRegistry} is already added to the global {@link 52 * io.opencensus.metrics.export.MetricProducerManager}. 53 * 54 * @return the global {@code MetricRegistry}. 55 * @since 0.17 56 */ getMetricRegistry()57 public static MetricRegistry getMetricRegistry() { 58 return metricsComponent.getMetricRegistry(); 59 } 60 61 // Any provider that may be used for MetricsComponent can be added here. 62 @DefaultVisibilityForTesting loadMetricsComponent(@ullable ClassLoader classLoader)63 static MetricsComponent loadMetricsComponent(@Nullable ClassLoader classLoader) { 64 try { 65 // Call Class.forName with literal string name of the class to help shading tools. 66 return Provider.createInstance( 67 Class.forName( 68 "io.opencensus.impl.metrics.MetricsComponentImpl", /*initialize=*/ true, classLoader), 69 MetricsComponent.class); 70 } catch (ClassNotFoundException e) { 71 logger.log( 72 Level.FINE, 73 "Couldn't load full implementation for MetricsComponent, now trying to load lite " 74 + "implementation.", 75 e); 76 } 77 try { 78 // Call Class.forName with literal string name of the class to help shading tools. 79 return Provider.createInstance( 80 Class.forName( 81 "io.opencensus.impllite.metrics.MetricsComponentImplLite", 82 /*initialize=*/ true, 83 classLoader), 84 MetricsComponent.class); 85 } catch (ClassNotFoundException e) { 86 logger.log( 87 Level.FINE, 88 "Couldn't load lite implementation for MetricsComponent, now using default " 89 + "implementation for MetricsComponent.", 90 e); 91 } 92 return MetricsComponent.newNoopMetricsComponent(); 93 } 94 Metrics()95 private Metrics() {} 96 } 97