3030import  java .util .Map ;
3131import  java .util .Objects ;
3232import  java .util .Optional ;
33+ import  java .util .logging .Level ;
34+ import  java .util .logging .Logger ;
3335import  java .util .stream .Collectors ;
3436import  javax .annotation .Nonnull ;
3537
5052public  class  GcpAuthAutoConfigurationCustomizerProvider 
5153    implements  AutoConfigurationCustomizerProvider  {
5254
55+   private  static  final  Logger  logger  =
56+       Logger .getLogger (GcpAuthAutoConfigurationCustomizerProvider .class .getName ());
57+   private  static  final  String  SIGNAL_TARGET_WARNING_FIX_SUGGESTION  =
58+       String .format (
59+           "You may safely ignore this warning if it is intentional, otherwise please configure the '%s' by exporting valid values to environment variable: %s or by setting valid values in system property: %s." ,
60+           ConfigurableOption .GOOGLE_OTEL_AUTH_TARGET_SIGNALS .getUserReadableName (),
61+           ConfigurableOption .GOOGLE_OTEL_AUTH_TARGET_SIGNALS .getEnvironmentVariable (),
62+           ConfigurableOption .GOOGLE_OTEL_AUTH_TARGET_SIGNALS .getSystemProperty ());
63+ 
5364  static  final  String  QUOTA_USER_PROJECT_HEADER  = "x-goog-user-project" ;
5465  static  final  String  GCP_USER_PROJECT_ID_KEY  = "gcp.project_id" ;
5566
@@ -93,10 +104,11 @@ public void customize(@Nonnull AutoConfigurationCustomizer autoConfiguration) {
93104    }
94105    autoConfiguration 
95106        .addSpanExporterCustomizer (
96-             (spanExporter , configProperties ) -> customizeSpanExporter (spanExporter , credentials ))
107+             (spanExporter , configProperties ) ->
108+                 customizeSpanExporter (spanExporter , credentials , configProperties ))
97109        .addMetricExporterCustomizer (
98110            (metricExporter , configProperties ) ->
99-                 customizeMetricExporter (metricExporter , credentials ))
111+                 customizeMetricExporter (metricExporter , credentials ,  configProperties ))
100112        .addResourceCustomizer (GcpAuthAutoConfigurationCustomizerProvider ::customizeResource );
101113  }
102114
@@ -106,26 +118,38 @@ public int order() {
106118  }
107119
108120  private  static  SpanExporter  customizeSpanExporter (
109-       SpanExporter  exporter , GoogleCredentials  credentials ) {
110-     if  (isSignalTargeted (SIGNAL_TYPE_TRACES )) {
111-       return  addAuthorizationHeaders (exporter , credentials );
121+       SpanExporter  exporter , GoogleCredentials  credentials , ConfigProperties  configProperties ) {
122+     if  (isSignalTargeted (SIGNAL_TYPE_TRACES , configProperties )) {
123+       return  addAuthorizationHeaders (exporter , credentials , configProperties );
124+     } else  {
125+       String [] params  = {SIGNAL_TYPE_TRACES , SIGNAL_TARGET_WARNING_FIX_SUGGESTION };
126+       logger .log (
127+           Level .WARNING ,
128+           "GCP Authentication Extension is not configured for signal type: {0}. {1}" ,
129+           params );
112130    }
113131    return  exporter ;
114132  }
115133
116134  private  static  MetricExporter  customizeMetricExporter (
117-       MetricExporter  exporter , GoogleCredentials  credentials ) {
118-     if  (isSignalTargeted (SIGNAL_TYPE_METRICS )) {
119-       return  addAuthorizationHeaders (exporter , credentials );
135+       MetricExporter  exporter , GoogleCredentials  credentials , ConfigProperties  configProperties ) {
136+     if  (isSignalTargeted (SIGNAL_TYPE_METRICS , configProperties )) {
137+       return  addAuthorizationHeaders (exporter , credentials , configProperties );
138+     } else  {
139+       String [] params  = {SIGNAL_TYPE_METRICS , SIGNAL_TARGET_WARNING_FIX_SUGGESTION };
140+       logger .log (
141+           Level .WARNING ,
142+           "GCP Authentication Extension is not configured for signal type: {0}. {1}" ,
143+           params );
120144    }
121145    return  exporter ;
122146  }
123147
124148  // Checks if the auth extension is configured to target the passed signal for authentication. 
125-   private  static  boolean  isSignalTargeted (String  checkSignal ) {
149+   private  static  boolean  isSignalTargeted (String  checkSignal ,  ConfigProperties   configProperties ) {
126150    String  userSpecifiedTargetedSignals  =
127151        ConfigurableOption .GOOGLE_OTEL_AUTH_TARGET_SIGNALS .getConfiguredValueWithFallback (
128-             () -> SIGNAL_TYPE_ALL );
152+             configProperties ,  () -> SIGNAL_TYPE_ALL );
129153    return  Arrays .stream (userSpecifiedTargetedSignals .split ("," ))
130154        .map (String ::trim )
131155        .anyMatch (
@@ -136,16 +160,16 @@ private static boolean isSignalTargeted(String checkSignal) {
136160  // Adds authorization headers to the calls made by the OtlpGrpcSpanExporter and 
137161  // OtlpHttpSpanExporter. 
138162  private  static  SpanExporter  addAuthorizationHeaders (
139-       SpanExporter  exporter , GoogleCredentials  credentials ) {
163+       SpanExporter  exporter , GoogleCredentials  credentials ,  ConfigProperties   configProperties ) {
140164    if  (exporter  instanceof  OtlpHttpSpanExporter ) {
141165      OtlpHttpSpanExporterBuilder  builder  =
142166          ((OtlpHttpSpanExporter ) exporter )
143-               .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ));
167+               .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ,  configProperties ));
144168      return  builder .build ();
145169    } else  if  (exporter  instanceof  OtlpGrpcSpanExporter ) {
146170      OtlpGrpcSpanExporterBuilder  builder  =
147171          ((OtlpGrpcSpanExporter ) exporter )
148-               .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ));
172+               .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ,  configProperties ));
149173      return  builder .build ();
150174    }
151175    return  exporter ;
@@ -154,22 +178,23 @@ private static SpanExporter addAuthorizationHeaders(
154178  // Adds authorization headers to the calls made by the OtlpGrpcMetricExporter and 
155179  // OtlpHttpMetricExporter. 
156180  private  static  MetricExporter  addAuthorizationHeaders (
157-       MetricExporter  exporter , GoogleCredentials  credentials ) {
181+       MetricExporter  exporter , GoogleCredentials  credentials ,  ConfigProperties   configProperties ) {
158182    if  (exporter  instanceof  OtlpHttpMetricExporter ) {
159183      OtlpHttpMetricExporterBuilder  builder  =
160184          ((OtlpHttpMetricExporter ) exporter )
161-               .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ));
185+               .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ,  configProperties ));
162186      return  builder .build ();
163187    } else  if  (exporter  instanceof  OtlpGrpcMetricExporter ) {
164188      OtlpGrpcMetricExporterBuilder  builder  =
165189          ((OtlpGrpcMetricExporter ) exporter )
166-               .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ));
190+               .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ,  configProperties ));
167191      return  builder .build ();
168192    }
169193    return  exporter ;
170194  }
171195
172-   private  static  Map <String , String > getRequiredHeaderMap (GoogleCredentials  credentials ) {
196+   private  static  Map <String , String > getRequiredHeaderMap (
197+       GoogleCredentials  credentials , ConfigProperties  configProperties ) {
173198    Map <String , List <String >> gcpHeaders ;
174199    try  {
175200      // this also refreshes the credentials, if required 
@@ -192,7 +217,8 @@ private static Map<String, String> getRequiredHeaderMap(GoogleCredentials creden
192217    // system properties. 
193218    if  (!flattenedHeaders .containsKey (QUOTA_USER_PROJECT_HEADER )) {
194219      Optional <String > maybeConfiguredQuotaProjectId  =
195-           ConfigurableOption .GOOGLE_CLOUD_QUOTA_PROJECT .getConfiguredValueAsOptional ();
220+           ConfigurableOption .GOOGLE_CLOUD_QUOTA_PROJECT .getConfiguredValueAsOptional (
221+               configProperties );
196222      maybeConfiguredQuotaProjectId .ifPresent (
197223          configuredQuotaProjectId  ->
198224              flattenedHeaders .put (QUOTA_USER_PROJECT_HEADER , configuredQuotaProjectId ));
@@ -202,7 +228,8 @@ private static Map<String, String> getRequiredHeaderMap(GoogleCredentials creden
202228
203229  // Updates the current resource with the attributes required for ingesting OTLP data on GCP. 
204230  private  static  Resource  customizeResource (Resource  resource , ConfigProperties  configProperties ) {
205-     String  gcpProjectId  = ConfigurableOption .GOOGLE_CLOUD_PROJECT .getConfiguredValue ();
231+     String  gcpProjectId  =
232+         ConfigurableOption .GOOGLE_CLOUD_PROJECT .getConfiguredValue (configProperties );
206233    Resource  res  =
207234        Resource .create (
208235            Attributes .of (AttributeKey .stringKey (GCP_USER_PROJECT_ID_KEY ), gcpProjectId ));
0 commit comments