|
8 | 8 | import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
9 | 9 | import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; |
10 | 10 | import java.util.Locale; |
11 | | -import java.util.Optional; |
12 | 11 | import java.util.function.BiFunction; |
13 | | -import java.util.function.Supplier; |
| 12 | +import javax.annotation.Nullable; |
14 | 13 |
|
15 | 14 | /** |
16 | 15 | * An enum representing configurable options for a GCP Authentication Extension. Each option has a |
@@ -100,60 +99,36 @@ String getUserReadableName() { |
100 | 99 | * @throws ConfigurationException if neither the environment variable nor the system property is |
101 | 100 | * set. |
102 | 101 | */ |
103 | | - <T> T getConfiguredValue( |
| 102 | + <T> T getRequiredConfiguredValue( |
104 | 103 | ConfigProperties configProperties, BiFunction<ConfigProperties, String, T> extractor) { |
105 | | - T configuredValue = extractor.apply(configProperties, this.getSystemProperty()); |
106 | | - if (configuredValue instanceof String) { |
107 | | - String value = (String) configuredValue; |
108 | | - if (value.isEmpty()) { |
109 | | - configuredValue = null; // Treat empty string as not configured |
110 | | - } |
111 | | - } |
112 | | - |
113 | | - if (configuredValue != null) { |
114 | | - return configuredValue; |
115 | | - } else { |
| 104 | + T configuredValue = getConfiguredValue(configProperties, extractor); |
| 105 | + if (configuredValue == null) { |
116 | 106 | throw new ConfigurationException( |
117 | 107 | String.format( |
118 | | - "GCP Authentication Extension not configured properly: %s not configured. Configure it by exporting environment variable %s or system property %s", |
| 108 | + "GCP Authentication Extension not configured properly: %s not configured. " |
| 109 | + + "Configure it by exporting environment variable %s or system property %s", |
119 | 110 | this.userReadableName, this.getEnvironmentVariable(), this.getSystemProperty())); |
120 | 111 | } |
| 112 | + return configuredValue; |
121 | 113 | } |
122 | 114 |
|
123 | 115 | /** |
124 | | - * Retrieves the value for this option, prioritizing environment variables and system properties. |
125 | | - * If neither an environment variable nor a system property is set for this option, the provided |
126 | | - * fallback function is used to determine the value. |
| 116 | + * Retrieves the configured value for this option. This method checks the environment variable |
| 117 | + * first and then the system property. |
127 | 118 | * |
128 | | - * @param fallback A {@link Supplier} that provides the default value for the option when it is |
129 | | - * not explicitly configured via an environment variable or system property. |
130 | | - * @return The configured value for the option, obtained from the environment variable, system |
131 | | - * property, or the fallback function, in that order of precedence. |
| 119 | + * @return The configured value as a string, or throws an exception if not configured. |
132 | 120 | */ |
133 | | - <T> T getConfiguredValueWithFallback( |
134 | | - ConfigProperties configProperties, |
135 | | - Supplier<T> fallback, |
136 | | - BiFunction<ConfigProperties, String, T> extractor) { |
137 | | - try { |
138 | | - return this.getConfiguredValue(configProperties, extractor); |
139 | | - } catch (ConfigurationException e) { |
140 | | - return fallback.get(); |
| 121 | + @Nullable |
| 122 | + <T> T getConfiguredValue( |
| 123 | + ConfigProperties configProperties, BiFunction<ConfigProperties, String, T> extractor) { |
| 124 | + T configuredValue = extractor.apply(configProperties, this.getSystemProperty()); |
| 125 | + if (configuredValue instanceof String) { |
| 126 | + String value = (String) configuredValue; |
| 127 | + if (value.isEmpty()) { |
| 128 | + configuredValue = null; // Treat empty string as not configured |
| 129 | + } |
141 | 130 | } |
142 | | - } |
143 | 131 |
|
144 | | - /** |
145 | | - * Retrieves the value for this option, prioritizing environment variables before system |
146 | | - * properties. If neither an environment variable nor a system property is set for this option, |
147 | | - * then an empty {@link Optional} is returned. |
148 | | - * |
149 | | - * @return The configured value for the option, if set, obtained from the environment variable, |
150 | | - * system property, or empty {@link Optional}, in that order of precedence. |
151 | | - */ |
152 | | - Optional<String> getConfiguredValueAsOptional(ConfigProperties configProperties) { |
153 | | - try { |
154 | | - return Optional.of(this.getConfiguredValue(configProperties, ConfigProperties::getString)); |
155 | | - } catch (ConfigurationException e) { |
156 | | - return Optional.empty(); |
157 | | - } |
| 132 | + return configuredValue; |
158 | 133 | } |
159 | 134 | } |
0 commit comments