xref: /aosp_15_r20/external/opencensus-java/contrib/agent/src/main/java/io/opencensus/contrib/agent/Settings.java (revision a24ffb47c3166327784aa05b149974e82e8f71b8)
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.contrib.agent;
18 
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.base.Preconditions.checkNotNull;
21 
22 import com.google.common.annotations.VisibleForTesting;
23 import com.google.common.base.Strings;
24 import com.typesafe.config.Config;
25 import com.typesafe.config.ConfigFactory;
26 import io.opencensus.common.Internal;
27 
28 /**
29  * The {@code Settings} class provides access to user-configurable settings.
30  *
31  * @since 0.10
32  */
33 public class Settings {
34 
35   private static final String CONFIG_ROOT = "opencensus.contrib.agent";
36 
37   private final Config config;
38 
39   /** Creates agent settings. */
40   @Internal
41   @VisibleForTesting
Settings(Config config)42   public Settings(Config config) {
43     this.config = checkNotNull(config);
44   }
45 
load()46   static Settings load() {
47     return new Settings(readConfig());
48   }
49 
readConfig()50   private static Config readConfig() {
51     Config config = ConfigFactory.load();
52     config.checkValid(ConfigFactory.defaultReference(), CONFIG_ROOT);
53 
54     return config.getConfig(CONFIG_ROOT);
55   }
56 
57   /**
58    * Checks whether a feature is enabled in the effective configuration.
59    *
60    * <p>A feature is identified by a path expression relative to {@link #CONFIG_ROOT}, such as
61    * {@code context-propagation.executor}. The feature is enabled iff the config element at the
62    * requested path has a child element {@code enabled} with a value of {@code true}, {@code on}, or
63    * {@code yes}.
64    *
65    * @param featurePath the feature's path expression
66    * @return true, if enabled, otherwise false
67    * @since 0.10
68    */
isEnabled(String featurePath)69   public boolean isEnabled(String featurePath) {
70     checkArgument(!Strings.isNullOrEmpty(featurePath));
71 
72     return config.getConfig(featurePath).getBoolean("enabled");
73   }
74 }
75