Skip to content

Commit 7b8bbf7

Browse files
authored
Update ConfigurableOptions to read from ConfigProperties (#1904)
1 parent 91276a9 commit 7b8bbf7

File tree

3 files changed

+68
-35
lines changed

3 files changed

+68
-35
lines changed

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
package io.opentelemetry.contrib.gcp.auth;
77

8+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
89
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
910
import java.util.Locale;
1011
import java.util.Optional;
@@ -14,7 +15,7 @@
1415
* An enum representing configurable options for a GCP Authentication Extension. Each option has a
1516
* user-readable name and can be configured using environment variables or system properties.
1617
*/
17-
public enum ConfigurableOption {
18+
enum ConfigurableOption {
1819
/**
1920
* Represents the Google Cloud Project ID option. Can be configured using the environment variable
2021
* `GOOGLE_CLOUD_PROJECT` or the system property `google.cloud.project`.
@@ -50,7 +51,7 @@ public enum ConfigurableOption {
5051
* configured using the environment variable `GOOGLE_OTEL_AUTH_TARGET_SIGNALS` or the system
5152
* property `google.otel.auth.target.signals`.
5253
*/
53-
GOOGLE_OTEL_AUTH_TARGET_SIGNALS("Target Signals for Google Auth Extension");
54+
GOOGLE_OTEL_AUTH_TARGET_SIGNALS("Target Signals for Google Authentication Extension");
5455

5556
private final String userReadableName;
5657
private final String environmentVariableName;
@@ -60,7 +61,7 @@ public enum ConfigurableOption {
6061
this.userReadableName = userReadableName;
6162
this.environmentVariableName = this.name();
6263
this.systemPropertyName =
63-
this.environmentVariableName.toLowerCase(Locale.ENGLISH).replace('_', '.');
64+
this.environmentVariableName.toLowerCase(Locale.ROOT).replace('_', '.');
6465
}
6566

6667
/**
@@ -81,6 +82,15 @@ String getSystemProperty() {
8182
return this.systemPropertyName;
8283
}
8384

85+
/**
86+
* Returns the user readable name associated with this option.
87+
*
88+
* @return the user readable name (e.g., "Google Cloud Quota Project ID")
89+
*/
90+
String getUserReadableName() {
91+
return this.userReadableName;
92+
}
93+
8494
/**
8595
* Retrieves the configured value for this option. This method checks the environment variable
8696
* first and then the system property.
@@ -89,14 +99,10 @@ String getSystemProperty() {
8999
* @throws ConfigurationException if neither the environment variable nor the system property is
90100
* set.
91101
*/
92-
String getConfiguredValue() {
93-
String envVar = System.getenv(this.getEnvironmentVariable());
94-
String sysProp = System.getProperty(this.getSystemProperty());
95-
96-
if (envVar != null && !envVar.isEmpty()) {
97-
return envVar;
98-
} else if (sysProp != null && !sysProp.isEmpty()) {
99-
return sysProp;
102+
String getConfiguredValue(ConfigProperties configProperties) {
103+
String configuredValue = configProperties.getString(this.getSystemProperty());
104+
if (configuredValue != null && !configuredValue.isEmpty()) {
105+
return configuredValue;
100106
} else {
101107
throw new ConfigurationException(
102108
String.format(
@@ -115,9 +121,10 @@ String getConfiguredValue() {
115121
* @return The configured value for the option, obtained from the environment variable, system
116122
* property, or the fallback function, in that order of precedence.
117123
*/
118-
String getConfiguredValueWithFallback(Supplier<String> fallback) {
124+
String getConfiguredValueWithFallback(
125+
ConfigProperties configProperties, Supplier<String> fallback) {
119126
try {
120-
return this.getConfiguredValue();
127+
return this.getConfiguredValue(configProperties);
121128
} catch (ConfigurationException e) {
122129
return fallback.get();
123130
}
@@ -131,9 +138,9 @@ String getConfiguredValueWithFallback(Supplier<String> fallback) {
131138
* @return The configured value for the option, if set, obtained from the environment variable,
132139
* system property, or empty {@link Optional}, in that order of precedence.
133140
*/
134-
Optional<String> getConfiguredValueAsOptional() {
141+
Optional<String> getConfiguredValueAsOptional(ConfigProperties configProperties) {
135142
try {
136-
return Optional.of(this.getConfiguredValue());
143+
return Optional.of(this.getConfiguredValue(configProperties));
137144
} catch (ConfigurationException e) {
138145
return Optional.empty();
139146
}

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

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import java.util.Map;
3131
import java.util.Objects;
3232
import java.util.Optional;
33+
import java.util.logging.Level;
34+
import java.util.logging.Logger;
3335
import java.util.stream.Collectors;
3436
import javax.annotation.Nonnull;
3537

@@ -50,6 +52,15 @@
5052
public class GcpAuthAutoConfigurationCustomizerProvider
5153
implements AutoConfigurationCustomizerProvider {
5254

55+
private static final Logger logger =
56+
Logger.getLogger(GcpAuthAutoConfigurationCustomizerProvider.class.getName());
57+
private static final String SIGNAL_TARGET_WARNING_FIX_SUGGESTION =
58+
String.format(
59+
"You may safely ignore this warning if it is intentional, otherwise please configure the '%s' by exporting valid values to environment variable: %s or by setting valid values in system property: %s.",
60+
ConfigurableOption.GOOGLE_OTEL_AUTH_TARGET_SIGNALS.getUserReadableName(),
61+
ConfigurableOption.GOOGLE_OTEL_AUTH_TARGET_SIGNALS.getEnvironmentVariable(),
62+
ConfigurableOption.GOOGLE_OTEL_AUTH_TARGET_SIGNALS.getSystemProperty());
63+
5364
static final String QUOTA_USER_PROJECT_HEADER = "x-goog-user-project";
5465
static final String GCP_USER_PROJECT_ID_KEY = "gcp.project_id";
5566

@@ -93,10 +104,11 @@ public void customize(@Nonnull AutoConfigurationCustomizer autoConfiguration) {
93104
}
94105
autoConfiguration
95106
.addSpanExporterCustomizer(
96-
(spanExporter, configProperties) -> customizeSpanExporter(spanExporter, credentials))
107+
(spanExporter, configProperties) ->
108+
customizeSpanExporter(spanExporter, credentials, configProperties))
97109
.addMetricExporterCustomizer(
98110
(metricExporter, configProperties) ->
99-
customizeMetricExporter(metricExporter, credentials))
111+
customizeMetricExporter(metricExporter, credentials, configProperties))
100112
.addResourceCustomizer(GcpAuthAutoConfigurationCustomizerProvider::customizeResource);
101113
}
102114

@@ -106,26 +118,38 @@ public int order() {
106118
}
107119

108120
private static SpanExporter customizeSpanExporter(
109-
SpanExporter exporter, GoogleCredentials credentials) {
110-
if (isSignalTargeted(SIGNAL_TYPE_TRACES)) {
111-
return addAuthorizationHeaders(exporter, credentials);
121+
SpanExporter exporter, GoogleCredentials credentials, ConfigProperties configProperties) {
122+
if (isSignalTargeted(SIGNAL_TYPE_TRACES, configProperties)) {
123+
return addAuthorizationHeaders(exporter, credentials, configProperties);
124+
} else {
125+
String[] params = {SIGNAL_TYPE_TRACES, SIGNAL_TARGET_WARNING_FIX_SUGGESTION};
126+
logger.log(
127+
Level.WARNING,
128+
"GCP Authentication Extension is not configured for signal type: {0}. {1}",
129+
params);
112130
}
113131
return exporter;
114132
}
115133

116134
private static MetricExporter customizeMetricExporter(
117-
MetricExporter exporter, GoogleCredentials credentials) {
118-
if (isSignalTargeted(SIGNAL_TYPE_METRICS)) {
119-
return addAuthorizationHeaders(exporter, credentials);
135+
MetricExporter exporter, GoogleCredentials credentials, ConfigProperties configProperties) {
136+
if (isSignalTargeted(SIGNAL_TYPE_METRICS, configProperties)) {
137+
return addAuthorizationHeaders(exporter, credentials, configProperties);
138+
} else {
139+
String[] params = {SIGNAL_TYPE_METRICS, SIGNAL_TARGET_WARNING_FIX_SUGGESTION};
140+
logger.log(
141+
Level.WARNING,
142+
"GCP Authentication Extension is not configured for signal type: {0}. {1}",
143+
params);
120144
}
121145
return exporter;
122146
}
123147

124148
// Checks if the auth extension is configured to target the passed signal for authentication.
125-
private static boolean isSignalTargeted(String checkSignal) {
149+
private static boolean isSignalTargeted(String checkSignal, ConfigProperties configProperties) {
126150
String userSpecifiedTargetedSignals =
127151
ConfigurableOption.GOOGLE_OTEL_AUTH_TARGET_SIGNALS.getConfiguredValueWithFallback(
128-
() -> SIGNAL_TYPE_ALL);
152+
configProperties, () -> SIGNAL_TYPE_ALL);
129153
return Arrays.stream(userSpecifiedTargetedSignals.split(","))
130154
.map(String::trim)
131155
.anyMatch(
@@ -136,16 +160,16 @@ private static boolean isSignalTargeted(String checkSignal) {
136160
// Adds authorization headers to the calls made by the OtlpGrpcSpanExporter and
137161
// OtlpHttpSpanExporter.
138162
private static SpanExporter addAuthorizationHeaders(
139-
SpanExporter exporter, GoogleCredentials credentials) {
163+
SpanExporter exporter, GoogleCredentials credentials, ConfigProperties configProperties) {
140164
if (exporter instanceof OtlpHttpSpanExporter) {
141165
OtlpHttpSpanExporterBuilder builder =
142166
((OtlpHttpSpanExporter) exporter)
143-
.toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials));
167+
.toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials, configProperties));
144168
return builder.build();
145169
} else if (exporter instanceof OtlpGrpcSpanExporter) {
146170
OtlpGrpcSpanExporterBuilder builder =
147171
((OtlpGrpcSpanExporter) exporter)
148-
.toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials));
172+
.toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials, configProperties));
149173
return builder.build();
150174
}
151175
return exporter;
@@ -154,22 +178,23 @@ private static SpanExporter addAuthorizationHeaders(
154178
// Adds authorization headers to the calls made by the OtlpGrpcMetricExporter and
155179
// OtlpHttpMetricExporter.
156180
private static MetricExporter addAuthorizationHeaders(
157-
MetricExporter exporter, GoogleCredentials credentials) {
181+
MetricExporter exporter, GoogleCredentials credentials, ConfigProperties configProperties) {
158182
if (exporter instanceof OtlpHttpMetricExporter) {
159183
OtlpHttpMetricExporterBuilder builder =
160184
((OtlpHttpMetricExporter) exporter)
161-
.toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials));
185+
.toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials, configProperties));
162186
return builder.build();
163187
} else if (exporter instanceof OtlpGrpcMetricExporter) {
164188
OtlpGrpcMetricExporterBuilder builder =
165189
((OtlpGrpcMetricExporter) exporter)
166-
.toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials));
190+
.toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials, configProperties));
167191
return builder.build();
168192
}
169193
return exporter;
170194
}
171195

172-
private static Map<String, String> getRequiredHeaderMap(GoogleCredentials credentials) {
196+
private static Map<String, String> getRequiredHeaderMap(
197+
GoogleCredentials credentials, ConfigProperties configProperties) {
173198
Map<String, List<String>> gcpHeaders;
174199
try {
175200
// this also refreshes the credentials, if required
@@ -192,7 +217,8 @@ private static Map<String, String> getRequiredHeaderMap(GoogleCredentials creden
192217
// system properties.
193218
if (!flattenedHeaders.containsKey(QUOTA_USER_PROJECT_HEADER)) {
194219
Optional<String> maybeConfiguredQuotaProjectId =
195-
ConfigurableOption.GOOGLE_CLOUD_QUOTA_PROJECT.getConfiguredValueAsOptional();
220+
ConfigurableOption.GOOGLE_CLOUD_QUOTA_PROJECT.getConfiguredValueAsOptional(
221+
configProperties);
196222
maybeConfiguredQuotaProjectId.ifPresent(
197223
configuredQuotaProjectId ->
198224
flattenedHeaders.put(QUOTA_USER_PROJECT_HEADER, configuredQuotaProjectId));
@@ -202,7 +228,8 @@ private static Map<String, String> getRequiredHeaderMap(GoogleCredentials creden
202228

203229
// Updates the current resource with the attributes required for ingesting OTLP data on GCP.
204230
private static Resource customizeResource(Resource resource, ConfigProperties configProperties) {
205-
String gcpProjectId = ConfigurableOption.GOOGLE_CLOUD_PROJECT.getConfiguredValue();
231+
String gcpProjectId =
232+
ConfigurableOption.GOOGLE_CLOUD_PROJECT.getConfiguredValue(configProperties);
206233
Resource res =
207234
Resource.create(
208235
Attributes.of(AttributeKey.stringKey(GCP_USER_PROJECT_ID_KEY), gcpProjectId));

gcp-auth-extension/src/test/java/io/opentelemetry/contrib/gcp/auth/GcpAuthAutoConfigurationCustomizerProviderTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,6 @@ private OpenTelemetrySdk buildOpenTelemetrySdkWithExporter(
941941
spanExporter, OtlpHttpMetricExporter.getDefault(), customOTelProperties);
942942
}
943943

944-
@SuppressWarnings("UnusedMethod")
945944
private OpenTelemetrySdk buildOpenTelemetrySdkWithExporter(MetricExporter metricExporter) {
946945
return buildOpenTelemetrySdkWithExporter(
947946
OtlpHttpSpanExporter.getDefault(), metricExporter, defaultOtelPropertiesMetricExporter);

0 commit comments

Comments
 (0)