Skip to content

Commit 17bbdf0

Browse files
committed
remove
1 parent 26cc42d commit 17bbdf0

File tree

5 files changed

+72
-363
lines changed

5 files changed

+72
-363
lines changed

gcp-auth-extension/build.gradle.kts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ dependencies {
2121
compileOnly("com.google.auto.service:auto-service-annotations")
2222
compileOnly("io.opentelemetry:opentelemetry-api")
2323
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
24-
compileOnly("io.opentelemetry:opentelemetry-sdk-extension-incubator")
25-
compileOnly("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-incubator")
2624
compileOnly("io.opentelemetry:opentelemetry-exporter-otlp")
2725

2826
// Only dependencies added to `implementation` configuration will be picked up by Shadow plugin
@@ -38,9 +36,7 @@ dependencies {
3836
testImplementation("io.opentelemetry:opentelemetry-exporter-otlp")
3937
testImplementation("io.opentelemetry:opentelemetry-sdk-testing")
4038
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-autoconfigure")
41-
testImplementation("io.opentelemetry:opentelemetry-sdk-extension-incubator")
4239
testImplementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-annotations")
43-
testImplementation("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-incubator")
4440

4541
testImplementation("org.awaitility:awaitility")
4642
testImplementation("org.mockito:mockito-inline")
@@ -137,11 +133,3 @@ tasks.register<Test>("IntegrationTestUserCreds") {
137133
"-Dmockserver.logLevel=trace"
138134
)
139135
}
140-
141-
// todo remove when https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/14497 is merged
142-
configurations.all {
143-
resolutionStrategy {
144-
force("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api-incubator:2.20.0-alpha-SNAPSHOT")
145-
force("io.opentelemetry.instrumentation:opentelemetry-instrumentation-api:2.19.0")
146-
}
147-
}

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

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
1111
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
12-
import java.util.function.BiFunction;
13-
import javax.annotation.Nullable;
12+
import java.util.Optional;
13+
import java.util.function.Supplier;
1414

1515
/**
1616
* An enum representing configurable options for a GCP Authentication Extension. Each option has a
@@ -92,41 +92,57 @@ String getUserReadableName() {
9292
}
9393

9494
/**
95-
* Retrieves the configured value for this option.
95+
* Retrieves the configured value for this option. This method checks the environment variable
96+
* first and then the system property.
9697
*
9798
* @return The configured value as a string, or throws an exception if not configured.
9899
* @throws ConfigurationException if neither the environment variable nor the system property is
99100
* set.
100101
*/
101-
<T> T getRequiredConfiguredValue(
102-
ConfigProperties configProperties, BiFunction<ConfigProperties, String, T> extractor) {
103-
T configuredValue = getConfiguredValue(configProperties, extractor);
104-
if (configuredValue == null) {
102+
String getConfiguredValue(ConfigProperties configProperties) {
103+
String configuredValue = configProperties.getString(this.getSystemProperty());
104+
if (configuredValue != null && !configuredValue.isEmpty()) {
105+
return configuredValue;
106+
} else {
105107
throw new ConfigurationException(
106108
String.format(
107-
"GCP Authentication Extension not configured properly: %s not configured. "
108-
+ "Configure it by exporting environment variable %s or system property %s",
109+
"GCP Authentication Extension not configured properly: %s not configured. Configure it by exporting environment variable %s or system property %s",
109110
this.userReadableName, this.getEnvironmentVariable(), this.getSystemProperty()));
110111
}
111-
return configuredValue;
112112
}
113113

114114
/**
115-
* Retrieves the configured value for this option.
115+
* Retrieves the value for this option, prioritizing environment variables and system properties.
116+
* If neither an environment variable nor a system property is set for this option, the provided
117+
* fallback function is used to determine the value.
116118
*
117-
* @return The configured value as a string, or {@code null} if not configured.
119+
* @param fallback A {@link Supplier} that provides the default value for the option when it is
120+
* not explicitly configured via an environment variable or system property.
121+
* @return The configured value for the option, obtained from the environment variable, system
122+
* property, or the fallback function, in that order of precedence.
118123
*/
119-
@Nullable
120-
<T> T getConfiguredValue(
121-
ConfigProperties configProperties, BiFunction<ConfigProperties, String, T> extractor) {
122-
T configuredValue = extractor.apply(configProperties, this.getSystemProperty());
123-
if (configuredValue instanceof String) {
124-
String value = (String) configuredValue;
125-
if (value.isEmpty()) {
126-
configuredValue = null; // Treat empty string as not configured
127-
}
124+
String getConfiguredValueWithFallback(
125+
ConfigProperties configProperties, Supplier<String> fallback) {
126+
try {
127+
return this.getConfiguredValue(configProperties);
128+
} catch (ConfigurationException e) {
129+
return fallback.get();
128130
}
131+
}
129132

130-
return configuredValue;
133+
/**
134+
* Retrieves the value for this option, prioritizing environment variables before system
135+
* properties. If neither an environment variable nor a system property is set for this option,
136+
* then an empty {@link Optional} is returned.
137+
*
138+
* @return The configured value for the option, if set, obtained from the environment variable,
139+
* system property, or empty {@link Optional}, in that order of precedence.
140+
*/
141+
Optional<String> getConfiguredValueAsOptional(ConfigProperties configProperties) {
142+
try {
143+
return Optional.of(this.getConfiguredValue(configProperties));
144+
} catch (ConfigurationException e) {
145+
return Optional.empty();
146+
}
131147
}
132148
}

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

Lines changed: 34 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
import com.google.auth.oauth2.GoogleCredentials;
1414
import com.google.auto.service.AutoService;
15-
import com.google.common.annotations.VisibleForTesting;
16-
import io.opentelemetry.api.common.AttributeKey;
1715
import io.opentelemetry.api.common.Attributes;
1816
import io.opentelemetry.contrib.gcp.auth.GoogleAuthException.Reason;
1917
import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter;
@@ -31,7 +29,6 @@
3129
import io.opentelemetry.sdk.resources.Resource;
3230
import io.opentelemetry.sdk.trace.export.SpanExporter;
3331
import java.io.IOException;
34-
import java.util.Collections;
3532
import java.util.List;
3633
import java.util.Map;
3734
import java.util.Objects;
@@ -89,8 +86,8 @@ public class GcpAuthAutoConfigurationCustomizerProvider
8986
* customizes only the signal specific exporter.
9087
* </ul>
9188
*
92-
* <p>The 'customization' performed includes customizing the exporters by adding required headers
93-
* to the export calls made and customizing the resource by adding required resource attributes to
89+
* The 'customization' performed includes customizing the exporters by adding required headers to
90+
* the export calls made and customizing the resource by adding required resource attributes to
9491
* enable GCP integration.
9592
*
9693
* @param autoConfiguration the AutoConfigurationCustomizer to customize.
@@ -101,7 +98,12 @@ public class GcpAuthAutoConfigurationCustomizerProvider
10198
*/
10299
@Override
103100
public void customize(@Nonnull AutoConfigurationCustomizer autoConfiguration) {
104-
GoogleCredentials credentials = getCredentials();
101+
GoogleCredentials credentials;
102+
try {
103+
credentials = GoogleCredentials.getApplicationDefault();
104+
} catch (IOException e) {
105+
throw new GoogleAuthException(Reason.FAILED_ADC_RETRIEVAL, e);
106+
}
105107
autoConfiguration
106108
.addSpanExporterCustomizer(
107109
(spanExporter, configProperties) ->
@@ -112,80 +114,51 @@ public void customize(@Nonnull AutoConfigurationCustomizer autoConfiguration) {
112114
.addResourceCustomizer(GcpAuthAutoConfigurationCustomizerProvider::customizeResource);
113115
}
114116

115-
static GoogleCredentials getCredentials() {
116-
GoogleCredentials credentials;
117-
try {
118-
credentials = GoogleCredentials.getApplicationDefault();
119-
} catch (IOException e) {
120-
throw new GoogleAuthException(Reason.FAILED_ADC_RETRIEVAL, e);
121-
}
122-
return credentials;
123-
}
124-
125117
@Override
126118
public int order() {
127119
return Integer.MAX_VALUE - 1;
128120
}
129121

130122
private static SpanExporter customizeSpanExporter(
131123
SpanExporter exporter, GoogleCredentials credentials, ConfigProperties configProperties) {
132-
if (shouldCustomizeExporter(
133-
SIGNAL_TYPE_TRACES, SIGNAL_TARGET_WARNING_FIX_SUGGESTION, configProperties)) {
124+
if (isSignalTargeted(SIGNAL_TYPE_TRACES, configProperties)) {
134125
return addAuthorizationHeaders(exporter, credentials, configProperties);
126+
} else {
127+
String[] params = {SIGNAL_TYPE_TRACES, SIGNAL_TARGET_WARNING_FIX_SUGGESTION};
128+
logger.log(
129+
Level.WARNING,
130+
"GCP Authentication Extension is not configured for signal type: {0}. {1}",
131+
params);
135132
}
136133
return exporter;
137134
}
138135

139136
private static MetricExporter customizeMetricExporter(
140137
MetricExporter exporter, GoogleCredentials credentials, ConfigProperties configProperties) {
141-
if (shouldCustomizeExporter(
142-
SIGNAL_TYPE_METRICS, SIGNAL_TARGET_WARNING_FIX_SUGGESTION, configProperties)) {
138+
if (isSignalTargeted(SIGNAL_TYPE_METRICS, configProperties)) {
143139
return addAuthorizationHeaders(exporter, credentials, configProperties);
144-
}
145-
return exporter;
146-
}
147-
148-
/**
149-
* Utility method to check whether OTLP exporters should be customized for the given target
150-
* signal.
151-
*
152-
* @param signal The target signal to check against. Could be one of {@value SIGNAL_TYPE_TRACES},
153-
* {@value SIGNAL_TYPE_METRICS} or {@value SIGNAL_TYPE_ALL}.
154-
* @param fixSuggestion A warning to alert the user that auth extension is not configured for the
155-
* provided target signal.
156-
* @param configProperties The {@link ConfigProperties} object used to configure the extension.
157-
* @return A boolean indicating whether the OTLP exporters should be customized or not.
158-
*/
159-
static boolean shouldCustomizeExporter(
160-
String signal, String fixSuggestion, ConfigProperties configProperties) {
161-
if (isSignalTargeted(signal, configProperties)) {
162-
return true;
163140
} else {
141+
String[] params = {SIGNAL_TYPE_METRICS, SIGNAL_TARGET_WARNING_FIX_SUGGESTION};
164142
logger.log(
165143
Level.WARNING,
166144
"GCP Authentication Extension is not configured for signal type: {0}. {1}",
167-
new String[] {signal, fixSuggestion});
168-
return false;
145+
params);
169146
}
147+
return exporter;
170148
}
171149

172150
// Checks if the auth extension is configured to target the passed signal for authentication.
173151
private static boolean isSignalTargeted(String checkSignal, ConfigProperties configProperties) {
174-
return targetSignals(configProperties).stream()
152+
String userSpecifiedTargetedSignals =
153+
ConfigurableOption.GOOGLE_OTEL_AUTH_TARGET_SIGNALS.getConfiguredValueWithFallback(
154+
configProperties, () -> SIGNAL_TYPE_ALL);
155+
return stream(userSpecifiedTargetedSignals.split(","))
156+
.map(String::trim)
175157
.anyMatch(
176158
targetedSignal ->
177159
targetedSignal.equals(checkSignal) || targetedSignal.equals(SIGNAL_TYPE_ALL));
178160
}
179161

180-
@VisibleForTesting
181-
static List<String> targetSignals(ConfigProperties configProperties) {
182-
return Objects.requireNonNull(
183-
ConfigurableOption.GOOGLE_OTEL_AUTH_TARGET_SIGNALS.getConfiguredValue(
184-
configProperties,
185-
(properties, name) ->
186-
properties.getList(name, Collections.singletonList(SIGNAL_TYPE_ALL))));
187-
}
188-
189162
// Adds authorization headers to the calls made by the OtlpGrpcSpanExporter and
190163
// OtlpHttpSpanExporter.
191164
private static SpanExporter addAuthorizationHeaders(
@@ -222,7 +195,7 @@ private static MetricExporter addAuthorizationHeaders(
222195
return exporter;
223196
}
224197

225-
static Map<String, String> getRequiredHeaderMap(
198+
private static Map<String, String> getRequiredHeaderMap(
226199
GoogleCredentials credentials, ConfigProperties configProperties) {
227200
Map<String, List<String>> gcpHeaders;
228201
try {
@@ -244,31 +217,21 @@ static Map<String, String> getRequiredHeaderMap(
244217
// Add quota user project header if not detected by the auth library and user provided it via
245218
// system properties.
246219
if (!flattenedHeaders.containsKey(QUOTA_USER_PROJECT_HEADER)) {
247-
getQuotaProjectId(configProperties)
248-
.ifPresent(
249-
configuredQuotaProjectId ->
250-
flattenedHeaders.put(QUOTA_USER_PROJECT_HEADER, configuredQuotaProjectId));
220+
Optional<String> maybeConfiguredQuotaProjectId =
221+
ConfigurableOption.GOOGLE_CLOUD_QUOTA_PROJECT.getConfiguredValueAsOptional(
222+
configProperties);
223+
maybeConfiguredQuotaProjectId.ifPresent(
224+
configuredQuotaProjectId ->
225+
flattenedHeaders.put(QUOTA_USER_PROJECT_HEADER, configuredQuotaProjectId));
251226
}
252227
return flattenedHeaders;
253228
}
254229

255-
static Optional<String> getQuotaProjectId(ConfigProperties configProperties) {
256-
return Optional.ofNullable(
257-
ConfigurableOption.GOOGLE_CLOUD_QUOTA_PROJECT.getConfiguredValue(
258-
configProperties, ConfigProperties::getString));
259-
}
260-
261230
// Updates the current resource with the attributes required for ingesting OTLP data on GCP.
262231
private static Resource customizeResource(Resource resource, ConfigProperties configProperties) {
263-
Resource res =
264-
Resource.create(
265-
Attributes.of(
266-
stringKey(GCP_USER_PROJECT_ID_KEY), getProjectId(configProperties)));
232+
String gcpProjectId =
233+
ConfigurableOption.GOOGLE_CLOUD_PROJECT.getConfiguredValue(configProperties);
234+
Resource res = Resource.create(Attributes.of(stringKey(GCP_USER_PROJECT_ID_KEY), gcpProjectId));
267235
return resource.merge(res);
268236
}
269-
270-
static String getProjectId(ConfigProperties configProperties) {
271-
return ConfigurableOption.GOOGLE_CLOUD_PROJECT.getRequiredConfiguredValue(
272-
configProperties, ConfigProperties::getString);
273-
}
274237
}

0 commit comments

Comments
 (0)