|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.contrib.gcp.auth; |
| 7 | + |
| 8 | +import com.google.auth.oauth2.GoogleCredentials; |
| 9 | +import com.google.auto.service.AutoService; |
| 10 | +import io.opentelemetry.api.common.AttributeKey; |
| 11 | +import io.opentelemetry.api.common.Attributes; |
| 12 | +import io.opentelemetry.contrib.gcp.auth.GoogleAuthException.Reason; |
| 13 | +import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter; |
| 14 | +import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder; |
| 15 | +import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter; |
| 16 | +import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder; |
| 17 | +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer; |
| 18 | +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; |
| 19 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 20 | +import io.opentelemetry.sdk.resources.Resource; |
| 21 | +import io.opentelemetry.sdk.trace.export.SpanExporter; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +/** |
| 27 | + * An AutoConfigurationCustomizerProvider for Google Cloud Platform (GCP) OpenTelemetry (OTLP) |
| 28 | + * integration. |
| 29 | + * |
| 30 | + * <p>This class is registered as a service provider using {@link AutoService} and is responsible |
| 31 | + * for customizing the OpenTelemetry configuration for GCP specific behavior. It retrieves Google |
| 32 | + * Application Default Credentials (ADC) and adds them as authorization headers to the configured |
| 33 | + * {@link SpanExporter}. It also sets default properties and resource attributes for GCP |
| 34 | + * integration. |
| 35 | + * |
| 36 | + * @see AutoConfigurationCustomizerProvider |
| 37 | + * @see GoogleCredentials |
| 38 | + */ |
| 39 | +@AutoService(AutoConfigurationCustomizerProvider.class) |
| 40 | +public class GcpAuthAutoConfigurationCustomizerProvider |
| 41 | + implements AutoConfigurationCustomizerProvider { |
| 42 | + |
| 43 | + static final String QUOTA_USER_PROJECT_HEADER = "X-Goog-User-Project"; |
| 44 | + static final String GCP_USER_PROJECT_ID_KEY = "gcp.project_id"; |
| 45 | + |
| 46 | + /** |
| 47 | + * Customizes the provided {@link AutoConfigurationCustomizer}. |
| 48 | + * |
| 49 | + * <p>This method attempts to retrieve Google Application Default Credentials (ADC) and performs |
| 50 | + * the following: - Adds authorization headers to the configured {@link SpanExporter} based on the |
| 51 | + * retrieved credentials. - Adds default properties for OTLP endpoint and resource attributes for |
| 52 | + * GCP integration. |
| 53 | + * |
| 54 | + * @param autoConfiguration the AutoConfigurationCustomizer to customize. |
| 55 | + * @throws GoogleAuthException if there's an error retrieving Google Application Default |
| 56 | + * Credentials. |
| 57 | + * @throws io.opentelemetry.sdk.autoconfigure.spi.ConfigurationException if required options are |
| 58 | + * not configured through environment variables or system properties. |
| 59 | + */ |
| 60 | + @Override |
| 61 | + public void customize(AutoConfigurationCustomizer autoConfiguration) { |
| 62 | + try { |
| 63 | + GoogleCredentials credentials = GoogleCredentials.getApplicationDefault(); |
| 64 | + autoConfiguration |
| 65 | + .addSpanExporterCustomizer( |
| 66 | + (exporter, configProperties) -> addAuthorizationHeaders(exporter, credentials)) |
| 67 | + .addResourceCustomizer(GcpAuthAutoConfigurationCustomizerProvider::customizeResource); |
| 68 | + } catch (IOException e) { |
| 69 | + throw new GoogleAuthException(Reason.FAILED_ADC_RETRIEVAL, e); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public int order() { |
| 75 | + return Integer.MAX_VALUE - 1; |
| 76 | + } |
| 77 | + |
| 78 | + // Adds authorization headers to the calls made by the OtlpGrpcSpanExporter and |
| 79 | + // OtlpHttpSpanExporter. |
| 80 | + private static SpanExporter addAuthorizationHeaders( |
| 81 | + SpanExporter exporter, GoogleCredentials credentials) { |
| 82 | + if (exporter instanceof OtlpHttpSpanExporter) { |
| 83 | + OtlpHttpSpanExporterBuilder builder = |
| 84 | + ((OtlpHttpSpanExporter) exporter) |
| 85 | + .toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials)); |
| 86 | + return builder.build(); |
| 87 | + } else if (exporter instanceof OtlpGrpcSpanExporter) { |
| 88 | + OtlpGrpcSpanExporterBuilder builder = |
| 89 | + ((OtlpGrpcSpanExporter) exporter) |
| 90 | + .toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials)); |
| 91 | + return builder.build(); |
| 92 | + } |
| 93 | + return exporter; |
| 94 | + } |
| 95 | + |
| 96 | + private static Map<String, String> getRequiredHeaderMap(GoogleCredentials credentials) { |
| 97 | + Map<String, String> gcpHeaders = new HashMap<>(); |
| 98 | + try { |
| 99 | + credentials.refreshIfExpired(); |
| 100 | + } catch (IOException e) { |
| 101 | + throw new GoogleAuthException(Reason.FAILED_ADC_REFRESH, e); |
| 102 | + } |
| 103 | + gcpHeaders.put(QUOTA_USER_PROJECT_HEADER, credentials.getQuotaProjectId()); |
| 104 | + gcpHeaders.put("Authorization", "Bearer " + credentials.getAccessToken().getTokenValue()); |
| 105 | + return gcpHeaders; |
| 106 | + } |
| 107 | + |
| 108 | + // Updates the current resource with the attributes required for ingesting OTLP data on GCP. |
| 109 | + 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 | + |
| 121 | + Resource res = |
| 122 | + Resource.create( |
| 123 | + Attributes.of(AttributeKey.stringKey(GCP_USER_PROJECT_ID_KEY), gcpProjectId)); |
| 124 | + return resource.merge(res); |
| 125 | + } |
| 126 | +} |
0 commit comments