Skip to content

Commit 4fe3342

Browse files
authored
[OpenTelemetry] Add gcp otel auth extension (#38773)
* migrate otel auth extension.
1 parent dd2a86c commit 4fe3342

9 files changed

Lines changed: 1837 additions & 0 deletions

File tree

buildSrc/src/main/groovy/org/apache/beam/gradle/BeamModulePlugin.groovy

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,8 @@ class BeamModulePlugin implements Plugin<Project> {
869869
opentelemetry_sdk : "io.opentelemetry:opentelemetry-sdk", // opentelemetry-bom sets version
870870
opentelemetry_exporter_otlp : "io.opentelemetry:opentelemetry-exporter-otlp", // opentelemetry-bom sets version
871871
opentelemetry_extension_autoconfigure : "io.opentelemetry:opentelemetry-sdk-extension-autoconfigure", // opentelemetry-bom sets version
872+
opentelemetry_proto : "io.opentelemetry.proto:opentelemetry-proto:$opentelemetry_version-alpha",
873+
opentelemetry_sdk_testing : "io.opentelemetry:opentelemetry-sdk-testing:$opentelemetry_version",
872874
postgres : "org.postgresql:postgresql:$postgres_version",
873875
protobuf_java : "com.google.protobuf:protobuf-java:$protobuf_version",
874876
protobuf_java_util : "com.google.protobuf:protobuf-java-util:$protobuf_version",
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* License); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an AS IS BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
plugins { id 'org.apache.beam.module' }
20+
applyJavaNature(
21+
automaticModuleName: 'org.apache.beam.sdk.extensions.opentelemetry.gcp.auth',
22+
)
23+
24+
description = "Apache Beam :: SDKs :: Java :: Extensions :: OpenTelemetry GCP Auth"
25+
ext.summary = "OpenTelemetry extension that provides GCP authentication support for OTLP exporters."
26+
27+
dependencies {
28+
implementation enforcedPlatform(library.java.google_cloud_platform_libraries_bom)
29+
implementation platform(library.java.opentelemetry_bom)
30+
implementation library.java.google_auth_library_oauth2_http
31+
implementation library.java.vendored_guava_32_1_2_jre
32+
compileOnly library.java.opentelemetry_api
33+
compileOnly library.java.opentelemetry_extension_autoconfigure
34+
compileOnly library.java.opentelemetry_exporter_otlp
35+
implementation project(path: ":sdks:java:core", configuration: "shadow")
36+
37+
testImplementation library.java.junit
38+
testImplementation library.java.mockito_core
39+
testImplementation library.java.mockito_inline
40+
testImplementation library.java.jupiter_api
41+
testImplementation library.java.jupiter_params
42+
testRuntimeOnly library.java.jupiter_engine
43+
testImplementation library.java.truth
44+
testImplementation library.java.opentelemetry_api
45+
testImplementation library.java.opentelemetry_sdk
46+
testImplementation library.java.opentelemetry_exporter_otlp
47+
testImplementation library.java.opentelemetry_sdk_testing
48+
testImplementation library.java.opentelemetry_extension_autoconfigure
49+
}
50+
test { useJUnitPlatform() }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.beam.sdk.extensions.opentelemetry.gcp.auth;
19+
20+
import static java.util.Locale.ROOT;
21+
22+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
23+
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException;
24+
import java.util.Optional;
25+
import java.util.function.Supplier;
26+
27+
/**
28+
* An enum representing configurable options for a GCP Authentication Extension. Each option has a
29+
* user-readable name and can be configured using environment variables or system properties.
30+
*
31+
* <p>Copied from
32+
* https://github.com/open-telemetry/opentelemetry-java-contrib/blob/main/gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/ConfigurableOption.java
33+
*/
34+
enum ConfigurableOption {
35+
/**
36+
* Represents the Google Cloud Project ID option. Can be configured using the environment variable
37+
* `GOOGLE_CLOUD_PROJECT` or the system property `google.cloud.project`.
38+
*/
39+
GOOGLE_CLOUD_PROJECT("Google Cloud Project ID"),
40+
41+
/**
42+
* Represents the Google Cloud Quota Project ID option. Can be configured using the environment
43+
* variable `GOOGLE_CLOUD_QUOTA_PROJECT` or the system property `google.cloud.quota.project`. The
44+
* quota project is the project that is used for quota management and billing for the API usage.
45+
*
46+
* <p>The environment variable name is selected to be consistent with the <a
47+
* href="https://cloud.google.com/docs/quotas/set-quota-project">official GCP client
48+
* libraries</a>.
49+
*/
50+
GOOGLE_CLOUD_QUOTA_PROJECT("Google Cloud Quota Project ID"),
51+
52+
/**
53+
* Specifies a comma-separated list of OpenTelemetry signals for which this authentication
54+
* extension should be active. The authentication mechanisms provided by this extension will only
55+
* be applied to the listed signals. If not set, {@code all} is assumed to be set which means
56+
* authentication is enabled for all supported signals.
57+
*
58+
* <p>Valid signal values are:
59+
*
60+
* <ul>
61+
* <li>{@code metrics} - Enables authentication for metric exports.
62+
* <li>{@code traces} - Enables authentication for trace exports.
63+
* <li>{@code all} - Enables authentication for all exports.
64+
* </ul>
65+
*
66+
* <p>The values are case-sensitive. Whitespace around commas and values is ignored. Can be
67+
* configured using the environment variable `GOOGLE_OTEL_AUTH_TARGET_SIGNALS` or the system
68+
* property `google.otel.auth.target.signals`.
69+
*/
70+
GOOGLE_OTEL_AUTH_TARGET_SIGNALS("Target Signals for Google Authentication Extension");
71+
72+
private final String userReadableName;
73+
private final String environmentVariableName;
74+
private final String systemPropertyName;
75+
76+
ConfigurableOption(String userReadableName) {
77+
this.userReadableName = userReadableName;
78+
this.environmentVariableName = this.name();
79+
this.systemPropertyName = this.environmentVariableName.toLowerCase(ROOT).replace('_', '.');
80+
}
81+
82+
/**
83+
* Returns the environment variable name associated with this option.
84+
*
85+
* @return the environment variable name (e.g., GOOGLE_CLOUD_PROJECT)
86+
*/
87+
String getEnvironmentVariable() {
88+
return this.environmentVariableName;
89+
}
90+
91+
/**
92+
* Returns the system property name associated with this option.
93+
*
94+
* @return the system property name (e.g., google.cloud.project)
95+
*/
96+
String getSystemProperty() {
97+
return this.systemPropertyName;
98+
}
99+
100+
/**
101+
* Returns the user readable name associated with this option.
102+
*
103+
* @return the user readable name (e.g., "Google Cloud Quota Project ID")
104+
*/
105+
String getUserReadableName() {
106+
return this.userReadableName;
107+
}
108+
109+
/**
110+
* Retrieves the configured value for this option. This method checks the environment variable
111+
* first and then the system property.
112+
*
113+
* @return The configured value as a string, or throws an exception if not configured.
114+
* @throws ConfigurationException if neither the environment variable nor the system property is
115+
* set.
116+
*/
117+
String getConfiguredValue(ConfigProperties configProperties) {
118+
String configuredValue = configProperties.getString(this.getSystemProperty());
119+
if (configuredValue != null && !configuredValue.isEmpty()) {
120+
return configuredValue;
121+
} else {
122+
throw new ConfigurationException(
123+
String.format(
124+
"GCP Authentication Extension not configured properly: %s not configured. Configure it by exporting environment variable %s or system property %s",
125+
this.userReadableName, this.getEnvironmentVariable(), this.getSystemProperty()));
126+
}
127+
}
128+
129+
/**
130+
* Retrieves the value for this option, prioritizing environment variables and system properties.
131+
* If neither an environment variable nor a system property is set for this option, the provided
132+
* fallback function is used to determine the value.
133+
*
134+
* @param fallback A {@link Supplier} that provides the default value for the option when it is
135+
* not explicitly configured via an environment variable or system property.
136+
* @return The configured value for the option, obtained from the environment variable, system
137+
* property, or the fallback function, in that order of precedence.
138+
*/
139+
String getConfiguredValueWithFallback(
140+
ConfigProperties configProperties, Supplier<String> fallback) {
141+
try {
142+
return this.getConfiguredValue(configProperties);
143+
} catch (ConfigurationException e) {
144+
return fallback.get();
145+
}
146+
}
147+
148+
/**
149+
* Retrieves the value for this option, prioritizing environment variables before system
150+
* properties. If neither an environment variable nor a system property is set for this option,
151+
* then an empty {@link Optional} is returned.
152+
*
153+
* @return The configured value for the option, if set, obtained from the environment variable,
154+
* system property, or empty {@link Optional}, in that order of precedence.
155+
*/
156+
Optional<String> getConfiguredValueAsOptional(ConfigProperties configProperties) {
157+
try {
158+
return Optional.of(this.getConfiguredValue(configProperties));
159+
} catch (ConfigurationException e) {
160+
return Optional.empty();
161+
}
162+
}
163+
}

0 commit comments

Comments
 (0)