1212
1313import com .google .auth .oauth2 .GoogleCredentials ;
1414import com .google .auto .service .AutoService ;
15- import com .google .common .annotations .VisibleForTesting ;
16- import io .opentelemetry .api .common .AttributeKey ;
1715import io .opentelemetry .api .common .Attributes ;
1816import io .opentelemetry .contrib .gcp .auth .GoogleAuthException .Reason ;
1917import io .opentelemetry .exporter .otlp .http .metrics .OtlpHttpMetricExporter ;
3129import io .opentelemetry .sdk .resources .Resource ;
3230import io .opentelemetry .sdk .trace .export .SpanExporter ;
3331import java .io .IOException ;
34- import java .util .Collections ;
3532import java .util .List ;
3633import java .util .Map ;
3734import java .util .Objects ;
@@ -89,8 +86,8 @@ public class GcpAuthAutoConfigurationCustomizerProvider
8986 * customizes only the signal specific exporter.
9087 * </ul>
9188 *
92- * <p> The 'customization' performed includes customizing the exporters by adding required headers
93- * to the export calls made and customizing the resource by adding required resource attributes to
89+ * The 'customization' performed includes customizing the exporters by adding required headers to
90+ * the export calls made and customizing the resource by adding required resource attributes to
9491 * enable GCP integration.
9592 *
9693 * @param autoConfiguration the AutoConfigurationCustomizer to customize.
@@ -101,7 +98,12 @@ public class GcpAuthAutoConfigurationCustomizerProvider
10198 */
10299 @ Override
103100 public void customize (@ Nonnull AutoConfigurationCustomizer autoConfiguration ) {
104- GoogleCredentials credentials = getCredentials ();
101+ GoogleCredentials credentials ;
102+ try {
103+ credentials = GoogleCredentials .getApplicationDefault ();
104+ } catch (IOException e ) {
105+ throw new GoogleAuthException (Reason .FAILED_ADC_RETRIEVAL , e );
106+ }
105107 autoConfiguration
106108 .addSpanExporterCustomizer (
107109 (spanExporter , configProperties ) ->
@@ -112,80 +114,51 @@ public void customize(@Nonnull AutoConfigurationCustomizer autoConfiguration) {
112114 .addResourceCustomizer (GcpAuthAutoConfigurationCustomizerProvider ::customizeResource );
113115 }
114116
115- static GoogleCredentials getCredentials () {
116- GoogleCredentials credentials ;
117- try {
118- credentials = GoogleCredentials .getApplicationDefault ();
119- } catch (IOException e ) {
120- throw new GoogleAuthException (Reason .FAILED_ADC_RETRIEVAL , e );
121- }
122- return credentials ;
123- }
124-
125117 @ Override
126118 public int order () {
127119 return Integer .MAX_VALUE - 1 ;
128120 }
129121
130122 private static SpanExporter customizeSpanExporter (
131123 SpanExporter exporter , GoogleCredentials credentials , ConfigProperties configProperties ) {
132- if (shouldCustomizeExporter (
133- SIGNAL_TYPE_TRACES , SIGNAL_TARGET_WARNING_FIX_SUGGESTION , configProperties )) {
124+ if (isSignalTargeted (SIGNAL_TYPE_TRACES , configProperties )) {
134125 return addAuthorizationHeaders (exporter , credentials , configProperties );
126+ } else {
127+ String [] params = {SIGNAL_TYPE_TRACES , SIGNAL_TARGET_WARNING_FIX_SUGGESTION };
128+ logger .log (
129+ Level .WARNING ,
130+ "GCP Authentication Extension is not configured for signal type: {0}. {1}" ,
131+ params );
135132 }
136133 return exporter ;
137134 }
138135
139136 private static MetricExporter customizeMetricExporter (
140137 MetricExporter exporter , GoogleCredentials credentials , ConfigProperties configProperties ) {
141- if (shouldCustomizeExporter (
142- SIGNAL_TYPE_METRICS , SIGNAL_TARGET_WARNING_FIX_SUGGESTION , configProperties )) {
138+ if (isSignalTargeted (SIGNAL_TYPE_METRICS , configProperties )) {
143139 return addAuthorizationHeaders (exporter , credentials , configProperties );
144- }
145- return exporter ;
146- }
147-
148- /**
149- * Utility method to check whether OTLP exporters should be customized for the given target
150- * signal.
151- *
152- * @param signal The target signal to check against. Could be one of {@value SIGNAL_TYPE_TRACES},
153- * {@value SIGNAL_TYPE_METRICS} or {@value SIGNAL_TYPE_ALL}.
154- * @param fixSuggestion A warning to alert the user that auth extension is not configured for the
155- * provided target signal.
156- * @param configProperties The {@link ConfigProperties} object used to configure the extension.
157- * @return A boolean indicating whether the OTLP exporters should be customized or not.
158- */
159- static boolean shouldCustomizeExporter (
160- String signal , String fixSuggestion , ConfigProperties configProperties ) {
161- if (isSignalTargeted (signal , configProperties )) {
162- return true ;
163140 } else {
141+ String [] params = {SIGNAL_TYPE_METRICS , SIGNAL_TARGET_WARNING_FIX_SUGGESTION };
164142 logger .log (
165143 Level .WARNING ,
166144 "GCP Authentication Extension is not configured for signal type: {0}. {1}" ,
167- new String [] {signal , fixSuggestion });
168- return false ;
145+ params );
169146 }
147+ return exporter ;
170148 }
171149
172150 // Checks if the auth extension is configured to target the passed signal for authentication.
173151 private static boolean isSignalTargeted (String checkSignal , ConfigProperties configProperties ) {
174- return targetSignals (configProperties ).stream ()
152+ String userSpecifiedTargetedSignals =
153+ ConfigurableOption .GOOGLE_OTEL_AUTH_TARGET_SIGNALS .getConfiguredValueWithFallback (
154+ configProperties , () -> SIGNAL_TYPE_ALL );
155+ return stream (userSpecifiedTargetedSignals .split ("," ))
156+ .map (String ::trim )
175157 .anyMatch (
176158 targetedSignal ->
177159 targetedSignal .equals (checkSignal ) || targetedSignal .equals (SIGNAL_TYPE_ALL ));
178160 }
179161
180- @ VisibleForTesting
181- static List <String > targetSignals (ConfigProperties configProperties ) {
182- return Objects .requireNonNull (
183- ConfigurableOption .GOOGLE_OTEL_AUTH_TARGET_SIGNALS .getConfiguredValue (
184- configProperties ,
185- (properties , name ) ->
186- properties .getList (name , Collections .singletonList (SIGNAL_TYPE_ALL ))));
187- }
188-
189162 // Adds authorization headers to the calls made by the OtlpGrpcSpanExporter and
190163 // OtlpHttpSpanExporter.
191164 private static SpanExporter addAuthorizationHeaders (
@@ -222,7 +195,7 @@ private static MetricExporter addAuthorizationHeaders(
222195 return exporter ;
223196 }
224197
225- static Map <String , String > getRequiredHeaderMap (
198+ private static Map <String , String > getRequiredHeaderMap (
226199 GoogleCredentials credentials , ConfigProperties configProperties ) {
227200 Map <String , List <String >> gcpHeaders ;
228201 try {
@@ -244,31 +217,21 @@ static Map<String, String> getRequiredHeaderMap(
244217 // Add quota user project header if not detected by the auth library and user provided it via
245218 // system properties.
246219 if (!flattenedHeaders .containsKey (QUOTA_USER_PROJECT_HEADER )) {
247- getQuotaProjectId (configProperties )
248- .ifPresent (
249- configuredQuotaProjectId ->
250- flattenedHeaders .put (QUOTA_USER_PROJECT_HEADER , configuredQuotaProjectId ));
220+ Optional <String > maybeConfiguredQuotaProjectId =
221+ ConfigurableOption .GOOGLE_CLOUD_QUOTA_PROJECT .getConfiguredValueAsOptional (
222+ configProperties );
223+ maybeConfiguredQuotaProjectId .ifPresent (
224+ configuredQuotaProjectId ->
225+ flattenedHeaders .put (QUOTA_USER_PROJECT_HEADER , configuredQuotaProjectId ));
251226 }
252227 return flattenedHeaders ;
253228 }
254229
255- static Optional <String > getQuotaProjectId (ConfigProperties configProperties ) {
256- return Optional .ofNullable (
257- ConfigurableOption .GOOGLE_CLOUD_QUOTA_PROJECT .getConfiguredValue (
258- configProperties , ConfigProperties ::getString ));
259- }
260-
261230 // Updates the current resource with the attributes required for ingesting OTLP data on GCP.
262231 private static Resource customizeResource (Resource resource , ConfigProperties configProperties ) {
263- Resource res =
264- Resource .create (
265- Attributes .of (
266- stringKey (GCP_USER_PROJECT_ID_KEY ), getProjectId (configProperties )));
232+ String gcpProjectId =
233+ ConfigurableOption .GOOGLE_CLOUD_PROJECT .getConfiguredValue (configProperties );
234+ Resource res = Resource .create (Attributes .of (stringKey (GCP_USER_PROJECT_ID_KEY ), gcpProjectId ));
267235 return resource .merge (res );
268236 }
269-
270- static String getProjectId (ConfigProperties configProperties ) {
271- return ConfigurableOption .GOOGLE_CLOUD_PROJECT .getRequiredConfiguredValue (
272- configProperties , ConfigProperties ::getString );
273- }
274237}
0 commit comments