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