Skip to content

Commit 72dc156

Browse files
authored
GCP Auth extension - allow users to specify quota project ID (#1647)
1 parent c00f094 commit 72dc156

File tree

5 files changed

+273
-40
lines changed

5 files changed

+273
-40
lines changed

gcp-auth-extension/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ Here is a list of configurable options for the extension:
3636

3737
- `GOOGLE_CLOUD_PROJECT`: Environment variable that represents the Google Cloud Project ID to which the telemetry needs to be exported.
3838
- Can also be configured using `google.cloud.project` system property.
39-
- If this option is not configured, the extension would infer GCP Project ID from the application default credentials. For more information on application default credentials, see [here](https://cloud.google.com/docs/authentication/application-default-credentials).
39+
- This is a required option, the agent configuration will fail if this option is not set.
40+
- `GOOGLE_CLOUD_QUOTA_PROJECT`: Environment variable that represents the Google Cloud Quota Project ID which will be charged for the GCP API usage. To learn more about a *quota project*, see [here](https://cloud.google.com/docs/quotas/quota-project).
41+
- Can also be configured using `google.cloud.quota.project` system property.
42+
- If this option is not configured, the extension will use the Quota Project ID found in the Application Default Credentials (ADC), if available. For more information on application default credentials, see [here](https://cloud.google.com/docs/authentication/application-default-credentials).
4043

4144
## Usage
4245

gcp-auth-extension/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ dependencies {
2929
testCompileOnly("com.google.auto.service:auto-service-annotations")
3030
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
3131
testImplementation("org.junit.jupiter:junit-jupiter-api")
32+
testCompileOnly("org.junit.jupiter:junit-jupiter-params")
3233

3334
testImplementation("io.opentelemetry:opentelemetry-api")
3435
testImplementation("io.opentelemetry:opentelemetry-exporter-otlp")
@@ -45,6 +46,9 @@ dependencies {
4546
testImplementation("org.springframework.boot:spring-boot-starter:2.7.18")
4647
testImplementation("org.springframework.boot:spring-boot-starter-test:2.7.18")
4748

49+
testAnnotationProcessor("com.google.auto.value:auto-value")
50+
testCompileOnly("com.google.auto.value:auto-value-annotations")
51+
4852
agent("io.opentelemetry.javaagent:opentelemetry-javaagent")
4953
}
5054

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@ public enum ConfigurableOption {
1818
* Represents the Google Cloud Project ID option. Can be configured using the environment variable
1919
* `GOOGLE_CLOUD_PROJECT` or the system property `google.cloud.project`.
2020
*/
21-
GOOGLE_CLOUD_PROJECT("Google Cloud Project ID");
21+
GOOGLE_CLOUD_PROJECT("Google Cloud Project ID"),
22+
23+
/**
24+
* Represents the Google Cloud Quota Project ID option. Can be configured using the environment
25+
* variable `GOOGLE_CLOUD_QUOTA_PROJECT` or the system property `google.cloud.quota.project`. The
26+
* quota project is the project that is used for quota management and billing for the API usage.
27+
*
28+
* <p>The environment variable name is selected to be consistent with the <a
29+
* href="https://cloud.google.com/docs/quotas/set-quota-project">official GCP client
30+
* libraries</a>.
31+
*/
32+
GOOGLE_CLOUD_QUOTA_PROJECT("Google Cloud Quota Project ID");
2233

2334
private final String userReadableName;
2435
private final String environmentVariableName;

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

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,16 @@ public class GcpAuthAutoConfigurationCustomizerProvider
5959
*/
6060
@Override
6161
public void customize(AutoConfigurationCustomizer autoConfiguration) {
62+
GoogleCredentials credentials;
6263
try {
63-
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
64-
autoConfiguration
65-
.addSpanExporterCustomizer(
66-
(exporter, configProperties) -> addAuthorizationHeaders(exporter, credentials))
67-
.addResourceCustomizer(GcpAuthAutoConfigurationCustomizerProvider::customizeResource);
64+
credentials = GoogleCredentials.getApplicationDefault();
6865
} catch (IOException e) {
6966
throw new GoogleAuthException(Reason.FAILED_ADC_RETRIEVAL, e);
7067
}
68+
autoConfiguration
69+
.addSpanExporterCustomizer(
70+
(exporter, configProperties) -> addAuthorizationHeaders(exporter, credentials))
71+
.addResourceCustomizer(GcpAuthAutoConfigurationCustomizerProvider::customizeResource);
7172
}
7273

7374
@Override
@@ -100,24 +101,19 @@ private static Map<String, String> getRequiredHeaderMap(GoogleCredentials creden
100101
} catch (IOException e) {
101102
throw new GoogleAuthException(Reason.FAILED_ADC_REFRESH, e);
102103
}
103-
gcpHeaders.put(QUOTA_USER_PROJECT_HEADER, credentials.getQuotaProjectId());
104104
gcpHeaders.put("Authorization", "Bearer " + credentials.getAccessToken().getTokenValue());
105+
String configuredQuotaProjectId =
106+
ConfigurableOption.GOOGLE_CLOUD_QUOTA_PROJECT.getConfiguredValueWithFallback(
107+
credentials::getQuotaProjectId);
108+
if (configuredQuotaProjectId != null && !configuredQuotaProjectId.isEmpty()) {
109+
gcpHeaders.put(QUOTA_USER_PROJECT_HEADER, configuredQuotaProjectId);
110+
}
105111
return gcpHeaders;
106112
}
107113

108114
// Updates the current resource with the attributes required for ingesting OTLP data on GCP.
109115
private static Resource customizeResource(Resource resource, ConfigProperties configProperties) {
110-
String gcpProjectId =
111-
ConfigurableOption.GOOGLE_CLOUD_PROJECT.getConfiguredValueWithFallback(
112-
() -> {
113-
try {
114-
GoogleCredentials googleCredentials = GoogleCredentials.getApplicationDefault();
115-
return googleCredentials.getQuotaProjectId();
116-
} catch (IOException e) {
117-
throw new GoogleAuthException(Reason.FAILED_ADC_RETRIEVAL, e);
118-
}
119-
});
120-
116+
String gcpProjectId = ConfigurableOption.GOOGLE_CLOUD_PROJECT.getConfiguredValue();
121117
Resource res =
122118
Resource.create(
123119
Attributes.of(AttributeKey.stringKey(GCP_USER_PROJECT_ID_KEY), gcpProjectId));

0 commit comments

Comments
 (0)