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,8 +93,11 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
7093 }
7194 autoConfiguration
7295 .addSpanExporterCustomizer (
73- (exporter , configProperties ) ->
74- addAuthorizationHeaders (exporter , credentials , configProperties ))
96+ (spanExporter , configProperties ) ->
97+ customizeSpanExporter (spanExporter , credentials , configProperties ))
98+ .addMetricExporterCustomizer (
99+ (metricExporter , configProperties ) ->
100+ customizeMetricExporter (metricExporter , credentials , configProperties ))
75101 .addResourceCustomizer (GcpAuthAutoConfigurationCustomizerProvider ::customizeResource );
76102 }
77103
@@ -80,6 +106,34 @@ public int order() {
80106 return Integer .MAX_VALUE - 1 ;
81107 }
82108
109+ private static SpanExporter customizeSpanExporter (
110+ SpanExporter exporter , GoogleCredentials credentials , ConfigProperties configProperties ) {
111+ if (isSignalTargeted (SIGNAL_TYPE_TRACES , configProperties )) {
112+ return addAuthorizationHeaders (exporter , credentials , configProperties );
113+ }
114+ return exporter ;
115+ }
116+
117+ private static MetricExporter customizeMetricExporter (
118+ MetricExporter exporter , GoogleCredentials credentials , ConfigProperties configProperties ) {
119+ if (isSignalTargeted (SIGNAL_TYPE_METRICS , configProperties )) {
120+ return addAuthorizationHeaders (exporter , credentials , configProperties );
121+ }
122+ return exporter ;
123+ }
124+
125+ // Checks if the auth extension is configured to target the passed signal for authentication.
126+ private static boolean isSignalTargeted (String checkSignal , ConfigProperties configProperties ) {
127+ String userSpecifiedTargetedSignals =
128+ ConfigurableOption .GOOGLE_OTEL_AUTH_TARGET_SIGNALS .getConfiguredValueWithFallback (
129+ configProperties , () -> SIGNAL_TYPE_ALL );
130+ return Arrays .stream (userSpecifiedTargetedSignals .split ("," ))
131+ .map (String ::trim )
132+ .anyMatch (
133+ targetedSignal ->
134+ targetedSignal .equals (checkSignal ) || targetedSignal .equals (SIGNAL_TYPE_ALL ));
135+ }
136+
83137 // Adds authorization headers to the calls made by the OtlpGrpcSpanExporter and
84138 // OtlpHttpSpanExporter.
85139 private static SpanExporter addAuthorizationHeaders (
@@ -98,6 +152,24 @@ private static SpanExporter addAuthorizationHeaders(
98152 return exporter ;
99153 }
100154
155+ // Adds authorization headers to the calls made by the OtlpGrpcMetricExporter and
156+ // OtlpHttpMetricExporter.
157+ private static MetricExporter addAuthorizationHeaders (
158+ MetricExporter exporter , GoogleCredentials credentials , ConfigProperties configProperties ) {
159+ if (exporter instanceof OtlpHttpMetricExporter ) {
160+ OtlpHttpMetricExporterBuilder builder =
161+ ((OtlpHttpMetricExporter ) exporter )
162+ .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials , configProperties ));
163+ return builder .build ();
164+ } else if (exporter instanceof OtlpGrpcMetricExporter ) {
165+ OtlpGrpcMetricExporterBuilder builder =
166+ ((OtlpGrpcMetricExporter ) exporter )
167+ .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials , configProperties ));
168+ return builder .build ();
169+ }
170+ return exporter ;
171+ }
172+
101173 private static Map <String , String > getRequiredHeaderMap (
102174 GoogleCredentials credentials , ConfigProperties configProperties ) {
103175 Map <String , List <String >> gcpHeaders ;
0 commit comments