Skip to content

Commit 58d4bad

Browse files
committed
Provide methods to support fallback value for config options
1 parent aeda53f commit 58d4bad

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

javaagent-extensions/gcp-auth/src/main/java/com/google/cloud/opentelemetry/extension/auth/ConfigurableOption.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
1919
import java.util.Locale;
20+
import java.util.function.Supplier;
2021

2122
/**
2223
* An enum representing configurable options for a GCP Authentication Extension. Each option has a
@@ -65,7 +66,7 @@ String getSystemProperty() {
6566
* Retrieves the configured value for this option. This method checks the environment variable
6667
* first and then the system property.
6768
*
68-
* @return the configured value as a string, or throws an exception if not configured.
69+
* @return The configured value as a string, or throws an exception if not configured.
6970
* @throws ConfigurationException if neither the environment variable nor the system property is
7071
* set.
7172
*/
@@ -86,4 +87,22 @@ String getConfiguredValue() throws ConfigurationException {
8687
this.getSystemProperty()));
8788
}
8889
}
90+
91+
/**
92+
* Retrieves the value for this option, prioritizing environment variables and system properties.
93+
* If neither an environment variable nor a system property is set for this option, the provided
94+
* fallback function is used to determine the value.
95+
*
96+
* @param fallback A {@link Supplier} that provides the default value for the option when it is
97+
* not explicitly configured via an environment variable or system property.
98+
* @return The configured value for the option, obtained from the environment variable, system
99+
* property, or the fallback function, in that order of precedence.
100+
*/
101+
String getConfiguredValueWithFallback(Supplier<String> fallback) {
102+
try {
103+
return this.getConfiguredValue();
104+
} catch (ConfigurationException e) {
105+
return fallback.get();
106+
}
107+
}
89108
}

javaagent-extensions/gcp-auth/src/main/java/com/google/cloud/opentelemetry/extension/auth/GcpAuthAutoConfigurationCustomizerProvider.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,20 @@ private Map<String, String> getRequiredHeaderMap(GoogleCredentials credentials)
117117

118118
// Updates the current resource with the attributes required for ingesting OTLP data on GCP.
119119
private Resource customizeResource(Resource resource, ConfigProperties configProperties) {
120+
String gcpProjectId =
121+
ConfigurableOption.GOOGLE_CLOUD_PROJECT.getConfiguredValueWithFallback(
122+
() -> {
123+
try {
124+
GoogleCredentials googleCredentials = GoogleCredentials.getApplicationDefault();
125+
return googleCredentials.getQuotaProjectId();
126+
} catch (IOException e) {
127+
throw new GoogleAuthException(Reason.FAILED_ADC_RETRIEVAL, e);
128+
}
129+
});
130+
120131
Resource res =
121132
Resource.create(
122-
Attributes.of(
123-
AttributeKey.stringKey(GCP_USER_PROJECT_ID_KEY),
124-
ConfigurableOption.GOOGLE_CLOUD_PROJECT.getConfiguredValue()));
133+
Attributes.of(AttributeKey.stringKey(GCP_USER_PROJECT_ID_KEY), gcpProjectId));
125134
return resource.merge(res);
126135
}
127136
}

0 commit comments

Comments
 (0)