2525import io .opentelemetry .sdk .resources .Resource ;
2626import io .opentelemetry .sdk .trace .export .SpanExporter ;
2727import java .io .IOException ;
28+ import java .util .Arrays ;
2829import java .util .List ;
2930import java .util .Map ;
3031import java .util .Objects ;
3132import java .util .Optional ;
32- import java .util .regex .Matcher ;
33- import java .util .regex .Pattern ;
3433import java .util .stream .Collectors ;
3534import javax .annotation .Nonnull ;
3635
@@ -54,11 +53,9 @@ public class GcpAuthAutoConfigurationCustomizerProvider
5453 static final String QUOTA_USER_PROJECT_HEADER = "x-goog-user-project" ;
5554 static final String GCP_USER_PROJECT_ID_KEY = "gcp.project_id" ;
5655
57- private static final String OTEL_EXPORTER_OTLP_ENDPOINT = "otel.exporter.otlp.endpoint" ;
58- private static final String OTEL_EXPORTER_OTLP_TRACES_ENDPOINT =
59- "otel.exporter.otlp.traces.endpoint" ;
60- private static final String OTEL_EXPORTER_OTLP_METRICS_ENDPOINT =
61- "otel.exporter.otlp.metrics.endpoint" ;
56+ private static final String SIGNAL_TYPE_TRACES = "traces" ;
57+ private static final String SIGNAL_TYPE_METRICS = "metrics" ;
58+ private static final String SIGNAL_TYPE_ALL = "all" ;
6259
6360 /**
6461 * Customizes the provided {@link AutoConfigurationCustomizer} such that authenticated exports to
@@ -96,11 +93,10 @@ public void customize(@Nonnull AutoConfigurationCustomizer autoConfiguration) {
9693 }
9794 autoConfiguration
9895 .addSpanExporterCustomizer (
99- (spanExporter , configProperties ) ->
100- customizeSpanExporter (spanExporter , configProperties , credentials ))
96+ (spanExporter , configProperties ) -> customizeSpanExporter (spanExporter , credentials ))
10197 .addMetricExporterCustomizer (
10298 (metricExporter , configProperties ) ->
103- customizeMetricExporter (metricExporter , configProperties , credentials ))
99+ customizeMetricExporter (metricExporter , credentials ))
104100 .addResourceCustomizer (GcpAuthAutoConfigurationCustomizerProvider ::customizeResource );
105101 }
106102
@@ -109,55 +105,36 @@ public int order() {
109105 return Integer .MAX_VALUE - 1 ;
110106 }
111107
112- // This method evaluates if the span exporter should be modified to enable export to GCP.
113- private static boolean shouldCustomizeSpanExporter (ConfigProperties configProperties ) {
114- String baseEndpoint = configProperties .getString (OTEL_EXPORTER_OTLP_ENDPOINT );
115- if (baseEndpoint != null && isKnownGcpTelemetryEndpoint (baseEndpoint )) {
116- return true ;
117- }
118- String tracesEndpoint = configProperties .getString (OTEL_EXPORTER_OTLP_TRACES_ENDPOINT );
119- return tracesEndpoint != null && isKnownGcpTelemetryEndpoint (tracesEndpoint );
120- }
121-
122- // This method evaluates if the metric exporter should be modified to enable export to GCP.
123- private static boolean shouldCustomizeMetricExporter (ConfigProperties configProperties ) {
124- String baseEndpoint = configProperties .getString (OTEL_EXPORTER_OTLP_ENDPOINT );
125- if (baseEndpoint != null && isKnownGcpTelemetryEndpoint (baseEndpoint )) {
126- return true ;
127- }
128- String metricsEndpoint = configProperties .getString (OTEL_EXPORTER_OTLP_METRICS_ENDPOINT );
129- return metricsEndpoint != null && isKnownGcpTelemetryEndpoint (metricsEndpoint );
130- }
131-
132- // This method evaluates if the endpoint provided by the user is a known GCP telemetry endpoint.
133- private static boolean isKnownGcpTelemetryEndpoint (String endpoint ) {
134- String knownBaseEndpointRegex = "^https://telemetry\\ .googleapis\\ .com(?:[:/].*)?$" ;
135- String knownRegionalizedEndpointRegex =
136- "^https://([a-z0-9]+(?:-[a-z0-9]+)*)\\ .rep\\ .googleapis\\ .com(?:[:/].*)?$" ;
137- // create a combined regex that matches any of the above.
138- String knownGcpEndpointRegex =
139- String .join ("|" , knownBaseEndpointRegex , knownRegionalizedEndpointRegex );
140- Pattern knownGcpEndpointPattern = Pattern .compile (knownGcpEndpointRegex );
141- Matcher gcpEndpointMatcher = knownGcpEndpointPattern .matcher (endpoint );
142- return gcpEndpointMatcher .matches ();
143- }
144-
145108 private static SpanExporter customizeSpanExporter (
146- SpanExporter exporter , ConfigProperties configProperties , GoogleCredentials credentials ) {
147- if (shouldCustomizeSpanExporter ( configProperties )) {
109+ SpanExporter exporter , GoogleCredentials credentials ) {
110+ if (isSignalTargeted ( SIGNAL_TYPE_TRACES )) {
148111 return addAuthorizationHeaders (exporter , credentials );
149112 }
150113 return exporter ;
151114 }
152115
153116 private static MetricExporter customizeMetricExporter (
154- MetricExporter exporter , ConfigProperties configProperties , GoogleCredentials credentials ) {
155- if (shouldCustomizeMetricExporter ( configProperties )) {
117+ MetricExporter exporter , GoogleCredentials credentials ) {
118+ if (isSignalTargeted ( SIGNAL_TYPE_METRICS )) {
156119 return addAuthorizationHeaders (exporter , credentials );
157120 }
158121 return exporter ;
159122 }
160123
124+ // Checks if the auth extension is configured to target the passed signal for authentication.
125+ private static boolean isSignalTargeted (String signal ) {
126+ String targetedSignals =
127+ ConfigurableOption .GOOGLE_OTEL_AUTH_TARGET_SIGNALS .getConfiguredValueWithFallback (
128+ () -> SIGNAL_TYPE_ALL );
129+ return Arrays .stream (targetedSignals .split ("," ))
130+ .map (String ::trim )
131+ .map (
132+ targetedSignal ->
133+ targetedSignal .equals (signal ) || targetedSignal .equals (SIGNAL_TYPE_ALL ))
134+ .findFirst ()
135+ .isPresent ();
136+ }
137+
161138 // Adds authorization headers to the calls made by the OtlpGrpcSpanExporter and
162139 // OtlpHttpSpanExporter.
163140 private static SpanExporter addAuthorizationHeaders (
0 commit comments