1010import io .opentelemetry .api .common .AttributeKey ;
1111import io .opentelemetry .api .common .Attributes ;
1212import io .opentelemetry .contrib .gcp .auth .GoogleAuthException .Reason ;
13+ import io .opentelemetry .exporter .otlp .http .metrics .OtlpHttpMetricExporter ;
14+ import io .opentelemetry .exporter .otlp .http .metrics .OtlpHttpMetricExporterBuilder ;
1315import io .opentelemetry .exporter .otlp .http .trace .OtlpHttpSpanExporter ;
1416import io .opentelemetry .exporter .otlp .http .trace .OtlpHttpSpanExporterBuilder ;
17+ import io .opentelemetry .exporter .otlp .metrics .OtlpGrpcMetricExporter ;
18+ import io .opentelemetry .exporter .otlp .metrics .OtlpGrpcMetricExporterBuilder ;
1519import io .opentelemetry .exporter .otlp .trace .OtlpGrpcSpanExporter ;
1620import io .opentelemetry .exporter .otlp .trace .OtlpGrpcSpanExporterBuilder ;
1721import io .opentelemetry .sdk .autoconfigure .spi .AutoConfigurationCustomizer ;
1822import io .opentelemetry .sdk .autoconfigure .spi .AutoConfigurationCustomizerProvider ;
1923import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
24+ import io .opentelemetry .sdk .metrics .export .MetricExporter ;
2025import io .opentelemetry .sdk .resources .Resource ;
2126import io .opentelemetry .sdk .trace .export .SpanExporter ;
2227import java .io .IOException ;
28+ import java .util .Arrays ;
2329import java .util .List ;
2430import java .util .Map ;
2531import java .util .Objects ;
2632import java .util .Optional ;
2733import java .util .stream .Collectors ;
34+ import javax .annotation .Nonnull ;
2835
2936/**
3037 * An AutoConfigurationCustomizerProvider for Google Cloud Platform (GCP) OpenTelemetry (OTLP)
@@ -46,13 +53,29 @@ public class GcpAuthAutoConfigurationCustomizerProvider
4653 static final String QUOTA_USER_PROJECT_HEADER = "x-goog-user-project" ;
4754 static final String GCP_USER_PROJECT_ID_KEY = "gcp.project_id" ;
4855
56+ static final String SIGNAL_TYPE_TRACES = "traces" ;
57+ static final String SIGNAL_TYPE_METRICS = "metrics" ;
58+ static final String SIGNAL_TYPE_ALL = "all" ;
59+
4960 /**
50- * Customizes the provided {@link AutoConfigurationCustomizer}.
61+ * Customizes the provided {@link AutoConfigurationCustomizer} such that authenticated exports to
62+ * GCP Telemetry API are possible from the configured OTLP exporter.
5163 *
5264 * <p>This method attempts to retrieve Google Application Default Credentials (ADC) and performs
53- * the following: - Adds authorization headers to the configured {@link SpanExporter} based on the
54- * retrieved credentials. - Adds default properties for OTLP endpoint and resource attributes for
55- * GCP integration.
65+ * the following:
66+ *
67+ * <ul>
68+ * <li>Verifies whether the configured OTLP endpoint (base or signal specific) is a known GCP
69+ * endpoint.
70+ * <li>If the configured base OTLP endpoint is a known GCP Telemetry API endpoint, customizes
71+ * both the configured OTLP {@link SpanExporter} and {@link MetricExporter}.
72+ * <li>If the configured signal specific endpoint is a known GCP Telemetry API endpoint,
73+ * customizes only the signal specific exporter.
74+ * </ul>
75+ *
76+ * The 'customization' performed includes customizing the exporters by adding required headers to
77+ * the export calls made and customizing the resource by adding required resource attributes to
78+ * enable GCP integration.
5679 *
5780 * @param autoConfiguration the AutoConfigurationCustomizer to customize.
5881 * @throws GoogleAuthException if there's an error retrieving Google Application Default
@@ -61,7 +84,7 @@ public class GcpAuthAutoConfigurationCustomizerProvider
6184 * not configured through environment variables or system properties.
6285 */
6386 @ Override
64- public void customize (AutoConfigurationCustomizer autoConfiguration ) {
87+ public void customize (@ Nonnull AutoConfigurationCustomizer autoConfiguration ) {
6588 GoogleCredentials credentials ;
6689 try {
6790 credentials = GoogleCredentials .getApplicationDefault ();
@@ -70,7 +93,10 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
7093 }
7194 autoConfiguration
7295 .addSpanExporterCustomizer (
73- (exporter , configProperties ) -> addAuthorizationHeaders (exporter , credentials ))
96+ (spanExporter , configProperties ) -> customizeSpanExporter (spanExporter , credentials ))
97+ .addMetricExporterCustomizer (
98+ (metricExporter , configProperties ) ->
99+ customizeMetricExporter (metricExporter , credentials ))
74100 .addResourceCustomizer (GcpAuthAutoConfigurationCustomizerProvider ::customizeResource );
75101 }
76102
@@ -79,6 +105,34 @@ public int order() {
79105 return Integer .MAX_VALUE - 1 ;
80106 }
81107
108+ private static SpanExporter customizeSpanExporter (
109+ SpanExporter exporter , GoogleCredentials credentials ) {
110+ if (isSignalTargeted (SIGNAL_TYPE_TRACES )) {
111+ return addAuthorizationHeaders (exporter , credentials );
112+ }
113+ return exporter ;
114+ }
115+
116+ private static MetricExporter customizeMetricExporter (
117+ MetricExporter exporter , GoogleCredentials credentials ) {
118+ if (isSignalTargeted (SIGNAL_TYPE_METRICS )) {
119+ return addAuthorizationHeaders (exporter , credentials );
120+ }
121+ return exporter ;
122+ }
123+
124+ // Checks if the auth extension is configured to target the passed signal for authentication.
125+ private static boolean isSignalTargeted (String checkSignal ) {
126+ String userSpecifiedTargetedSignals =
127+ ConfigurableOption .GOOGLE_OTEL_AUTH_TARGET_SIGNALS .getConfiguredValueWithFallback (
128+ () -> SIGNAL_TYPE_ALL );
129+ return Arrays .stream (userSpecifiedTargetedSignals .split ("," ))
130+ .map (String ::trim )
131+ .anyMatch (
132+ targetedSignal ->
133+ targetedSignal .equals (checkSignal ) || targetedSignal .equals (SIGNAL_TYPE_ALL ));
134+ }
135+
82136 // Adds authorization headers to the calls made by the OtlpGrpcSpanExporter and
83137 // OtlpHttpSpanExporter.
84138 private static SpanExporter addAuthorizationHeaders (
@@ -97,6 +151,24 @@ private static SpanExporter addAuthorizationHeaders(
97151 return exporter ;
98152 }
99153
154+ // Adds authorization headers to the calls made by the OtlpGrpcMetricExporter and
155+ // OtlpHttpMetricExporter.
156+ private static MetricExporter addAuthorizationHeaders (
157+ MetricExporter exporter , GoogleCredentials credentials ) {
158+ if (exporter instanceof OtlpHttpMetricExporter ) {
159+ OtlpHttpMetricExporterBuilder builder =
160+ ((OtlpHttpMetricExporter ) exporter )
161+ .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ));
162+ return builder .build ();
163+ } else if (exporter instanceof OtlpGrpcMetricExporter ) {
164+ OtlpGrpcMetricExporterBuilder builder =
165+ ((OtlpGrpcMetricExporter ) exporter )
166+ .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ));
167+ return builder .build ();
168+ }
169+ return exporter ;
170+ }
171+
100172 private static Map <String , String > getRequiredHeaderMap (GoogleCredentials credentials ) {
101173 Map <String , List <String >> gcpHeaders ;
102174 try {
0 commit comments