|
5 | 5 |
|
6 | 6 | package io.opentelemetry.contrib.gcp.auth; |
7 | 7 |
|
8 | | -import static io.opentelemetry.api.common.AttributeKey.stringKey; |
9 | 8 | import static java.util.Arrays.stream; |
10 | 9 | import static java.util.stream.Collectors.joining; |
11 | 10 | import static java.util.stream.Collectors.toMap; |
12 | 11 |
|
13 | 12 | import com.google.auth.oauth2.GoogleCredentials; |
14 | 13 | import com.google.auto.service.AutoService; |
| 14 | +import com.google.cloud.ServiceOptions; |
| 15 | +import io.opentelemetry.api.common.AttributeKey; |
15 | 16 | import io.opentelemetry.api.common.Attributes; |
16 | 17 | import io.opentelemetry.contrib.gcp.auth.GoogleAuthException.Reason; |
17 | 18 | import io.opentelemetry.exporter.otlp.http.metrics.OtlpHttpMetricExporter; |
|
25 | 26 | import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; |
26 | 27 | import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; |
27 | 28 | import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 29 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException; |
28 | 30 | import io.opentelemetry.sdk.metrics.export.MetricExporter; |
29 | 31 | import io.opentelemetry.sdk.resources.Resource; |
30 | 32 | import io.opentelemetry.sdk.trace.export.SpanExporter; |
@@ -111,7 +113,12 @@ public void customize(@Nonnull AutoConfigurationCustomizer autoConfiguration) { |
111 | 113 | .addMetricExporterCustomizer( |
112 | 114 | (metricExporter, configProperties) -> |
113 | 115 | customizeMetricExporter(metricExporter, credentials, configProperties)) |
114 | | - .addResourceCustomizer(GcpAuthAutoConfigurationCustomizerProvider::customizeResource); |
| 116 | + .addResourceCustomizer( |
| 117 | + (resource, configProperties) -> { |
| 118 | + String gcpProjectId = getGoogleProjectId(configProperties); |
| 119 | + |
| 120 | + return customizeResource(resource, gcpProjectId); |
| 121 | + }); |
115 | 122 | } |
116 | 123 |
|
117 | 124 | @Override |
@@ -228,10 +235,35 @@ private static Map<String, String> getRequiredHeaderMap( |
228 | 235 | } |
229 | 236 |
|
230 | 237 | // Updates the current resource with the attributes required for ingesting OTLP data on GCP. |
231 | | - private static Resource customizeResource(Resource resource, ConfigProperties 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)); |
| 238 | + private static Resource customizeResource(Resource resource, String gcpProjectId) { |
| 239 | + Resource res = |
| 240 | + Resource.create( |
| 241 | + Attributes.of(AttributeKey.stringKey(GCP_USER_PROJECT_ID_KEY), gcpProjectId)); |
235 | 242 | return resource.merge(res); |
236 | 243 | } |
| 244 | + |
| 245 | + /** |
| 246 | + * Retrieves the Google Cloud Project ID from the configuration properties, falling back to |
| 247 | + * google-cloud-core's ServiceOptions project ID resolution if not explicitly set. |
| 248 | + * |
| 249 | + * @param configProperties The configuration properties containing the GCP project ID. |
| 250 | + * @return The Google Cloud Project ID. |
| 251 | + */ |
| 252 | + @Nonnull |
| 253 | + static String getGoogleProjectId(ConfigProperties configProperties) { |
| 254 | + String googleProjectId = |
| 255 | + ConfigurableOption.GOOGLE_CLOUD_PROJECT.getConfiguredValueWithFallback( |
| 256 | + configProperties, ServiceOptions::getDefaultProjectId); |
| 257 | + |
| 258 | + if (googleProjectId == null || googleProjectId.isEmpty()) { |
| 259 | + throw new ConfigurationException( |
| 260 | + String.format( |
| 261 | + "GCP Authentication Extension not configured properly: %s not configured. Configure it by exporting environment variable %s or system property %s", |
| 262 | + ConfigurableOption.GOOGLE_CLOUD_PROJECT, |
| 263 | + ConfigurableOption.GOOGLE_CLOUD_PROJECT.getEnvironmentVariable(), |
| 264 | + ConfigurableOption.GOOGLE_CLOUD_PROJECT.getSystemProperty())); |
| 265 | + } |
| 266 | + |
| 267 | + return googleProjectId; |
| 268 | + } |
237 | 269 | } |
0 commit comments