Skip to content

Commit ee3ca1d

Browse files
committed
fix
1 parent caebe76 commit ee3ca1d

File tree

2 files changed

+29
-52
lines changed

2 files changed

+29
-52
lines changed

gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/ConfigurableOption.java

Lines changed: 20 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@
88
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
99
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
1010
import java.util.Locale;
11-
import java.util.Optional;
1211
import java.util.function.BiFunction;
13-
import java.util.function.Supplier;
12+
import javax.annotation.Nullable;
1413

1514
/**
1615
* An enum representing configurable options for a GCP Authentication Extension. Each option has a
@@ -100,60 +99,36 @@ String getUserReadableName() {
10099
* @throws ConfigurationException if neither the environment variable nor the system property is
101100
* set.
102101
*/
103-
<T> T getConfiguredValue(
102+
<T> T getRequiredConfiguredValue(
104103
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) {
116106
throw new ConfigurationException(
117107
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",
119110
this.userReadableName, this.getEnvironmentVariable(), this.getSystemProperty()));
120111
}
112+
return configuredValue;
121113
}
122114

123115
/**
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.
127118
*
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.
132120
*/
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+
}
141130
}
142-
}
143131

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;
158133
}
159134
}

gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/GcpAuthAutoConfigurationCustomizerProvider.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,11 @@ private static boolean isSignalTargeted(String checkSignal, ConfigProperties con
162162
}
163163

164164
static List<String> targetSignals(ConfigProperties configProperties) {
165-
return ConfigurableOption.GOOGLE_OTEL_AUTH_TARGET_SIGNALS.getConfiguredValueWithFallback(
166-
configProperties,
167-
() -> Collections.singletonList(SIGNAL_TYPE_ALL),
168-
ConfigProperties::getList);
165+
return Objects.requireNonNull(
166+
ConfigurableOption.GOOGLE_OTEL_AUTH_TARGET_SIGNALS.getConfiguredValue(
167+
configProperties,
168+
(properties, name) ->
169+
properties.getList(name, Collections.singletonList(SIGNAL_TYPE_ALL))));
169170
}
170171

171172
// Adds authorization headers to the calls made by the OtlpGrpcSpanExporter and
@@ -236,8 +237,9 @@ static Map<String, String> getRequiredHeaderMap(
236237
}
237238

238239
static Optional<String> getQuotaProjectId(ConfigProperties configProperties) {
239-
return ConfigurableOption.GOOGLE_CLOUD_QUOTA_PROJECT.getConfiguredValueAsOptional(
240-
configProperties);
240+
return Optional.ofNullable(
241+
ConfigurableOption.GOOGLE_CLOUD_QUOTA_PROJECT.getConfiguredValue(
242+
configProperties, ConfigProperties::getString));
241243
}
242244

243245
// Updates the current resource with the attributes required for ingesting OTLP data on GCP.
@@ -250,7 +252,7 @@ private static Resource customizeResource(Resource resource, ConfigProperties co
250252
}
251253

252254
static String getProjectId(ConfigProperties configProperties) {
253-
return ConfigurableOption.GOOGLE_CLOUD_PROJECT.getConfiguredValue(
255+
return ConfigurableOption.GOOGLE_CLOUD_PROJECT.getRequiredConfiguredValue(
254256
configProperties, ConfigProperties::getString);
255257
}
256258
}

0 commit comments

Comments
 (0)