|
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,11 @@ 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((resource, configProperties) -> { |
| 115 | + String gcpProjectId = getGoogleProjectId(configProperties); |
| 116 | + |
| 117 | + return customizeResource(resource, gcpProjectId); |
| 118 | + }); |
113 | 119 | }
|
114 | 120 |
|
115 | 121 | @Override
|
@@ -227,12 +233,33 @@ private static Map<String, String> getRequiredHeaderMap(
|
227 | 233 | }
|
228 | 234 |
|
229 | 235 | // 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); |
233 |
| - Resource res = |
234 |
| - Resource.create( |
235 |
| - Attributes.of(AttributeKey.stringKey(GCP_USER_PROJECT_ID_KEY), gcpProjectId)); |
| 236 | + private static Resource customizeResource(Resource resource, String gcpProjectId) { |
| 237 | + Resource res = Resource.create( |
| 238 | + Attributes.of(AttributeKey.stringKey(GCP_USER_PROJECT_ID_KEY), gcpProjectId)); |
236 | 239 | return resource.merge(res);
|
237 | 240 | }
|
| 241 | + |
| 242 | + /** |
| 243 | + * Retrieves the Google Cloud Project ID from the configuration properties, falling back to |
| 244 | + * google-cloud-core's ServiceOptions project ID resolution if not explicitly set. |
| 245 | + * |
| 246 | + * @param configProperties The configuration properties containing the GCP project ID. |
| 247 | + * @return The Google Cloud Project ID. |
| 248 | + */ |
| 249 | + @Nonnull |
| 250 | + static String getGoogleProjectId(ConfigProperties configProperties) { |
| 251 | + String googleProjectId = ConfigurableOption.GOOGLE_CLOUD_PROJECT.getConfiguredValueWithFallback( |
| 252 | + configProperties, |
| 253 | + ServiceOptions::getDefaultProjectId); |
| 254 | + |
| 255 | + if (googleProjectId == null || googleProjectId.isEmpty()) { |
| 256 | + throw new ConfigurationException(String.format( |
| 257 | + "GCP Authentication Extension not configured properly: %s not configured. Configure it by exporting environment variable %s or system property %s", |
| 258 | + ConfigurableOption.GOOGLE_CLOUD_PROJECT, |
| 259 | + ConfigurableOption.GOOGLE_CLOUD_PROJECT.getEnvironmentVariable(), |
| 260 | + ConfigurableOption.GOOGLE_CLOUD_PROJECT.getSystemProperty())); |
| 261 | + } |
| 262 | + |
| 263 | + return googleProjectId; |
| 264 | + } |
238 | 265 | }
|
0 commit comments