30
30
import java .util .Map ;
31
31
import java .util .Objects ;
32
32
import java .util .Optional ;
33
+ import java .util .logging .Level ;
34
+ import java .util .logging .Logger ;
33
35
import java .util .stream .Collectors ;
34
36
import javax .annotation .Nonnull ;
35
37
50
52
public class GcpAuthAutoConfigurationCustomizerProvider
51
53
implements AutoConfigurationCustomizerProvider {
52
54
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
+
53
64
static final String QUOTA_USER_PROJECT_HEADER = "x-goog-user-project" ;
54
65
static final String GCP_USER_PROJECT_ID_KEY = "gcp.project_id" ;
55
66
@@ -93,10 +104,11 @@ public void customize(@Nonnull AutoConfigurationCustomizer autoConfiguration) {
93
104
}
94
105
autoConfiguration
95
106
.addSpanExporterCustomizer (
96
- (spanExporter , configProperties ) -> customizeSpanExporter (spanExporter , credentials ))
107
+ (spanExporter , configProperties ) ->
108
+ customizeSpanExporter (spanExporter , credentials , configProperties ))
97
109
.addMetricExporterCustomizer (
98
110
(metricExporter , configProperties ) ->
99
- customizeMetricExporter (metricExporter , credentials ))
111
+ customizeMetricExporter (metricExporter , credentials , configProperties ))
100
112
.addResourceCustomizer (GcpAuthAutoConfigurationCustomizerProvider ::customizeResource );
101
113
}
102
114
@@ -106,26 +118,38 @@ public int order() {
106
118
}
107
119
108
120
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 );
112
130
}
113
131
return exporter ;
114
132
}
115
133
116
134
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 );
120
144
}
121
145
return exporter ;
122
146
}
123
147
124
148
// 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 ) {
126
150
String userSpecifiedTargetedSignals =
127
151
ConfigurableOption .GOOGLE_OTEL_AUTH_TARGET_SIGNALS .getConfiguredValueWithFallback (
128
- () -> SIGNAL_TYPE_ALL );
152
+ configProperties , () -> SIGNAL_TYPE_ALL );
129
153
return Arrays .stream (userSpecifiedTargetedSignals .split ("," ))
130
154
.map (String ::trim )
131
155
.anyMatch (
@@ -136,16 +160,16 @@ private static boolean isSignalTargeted(String checkSignal) {
136
160
// Adds authorization headers to the calls made by the OtlpGrpcSpanExporter and
137
161
// OtlpHttpSpanExporter.
138
162
private static SpanExporter addAuthorizationHeaders (
139
- SpanExporter exporter , GoogleCredentials credentials ) {
163
+ SpanExporter exporter , GoogleCredentials credentials , ConfigProperties configProperties ) {
140
164
if (exporter instanceof OtlpHttpSpanExporter ) {
141
165
OtlpHttpSpanExporterBuilder builder =
142
166
((OtlpHttpSpanExporter ) exporter )
143
- .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ));
167
+ .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials , configProperties ));
144
168
return builder .build ();
145
169
} else if (exporter instanceof OtlpGrpcSpanExporter ) {
146
170
OtlpGrpcSpanExporterBuilder builder =
147
171
((OtlpGrpcSpanExporter ) exporter )
148
- .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ));
172
+ .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials , configProperties ));
149
173
return builder .build ();
150
174
}
151
175
return exporter ;
@@ -154,22 +178,23 @@ private static SpanExporter addAuthorizationHeaders(
154
178
// Adds authorization headers to the calls made by the OtlpGrpcMetricExporter and
155
179
// OtlpHttpMetricExporter.
156
180
private static MetricExporter addAuthorizationHeaders (
157
- MetricExporter exporter , GoogleCredentials credentials ) {
181
+ MetricExporter exporter , GoogleCredentials credentials , ConfigProperties configProperties ) {
158
182
if (exporter instanceof OtlpHttpMetricExporter ) {
159
183
OtlpHttpMetricExporterBuilder builder =
160
184
((OtlpHttpMetricExporter ) exporter )
161
- .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ));
185
+ .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials , configProperties ));
162
186
return builder .build ();
163
187
} else if (exporter instanceof OtlpGrpcMetricExporter ) {
164
188
OtlpGrpcMetricExporterBuilder builder =
165
189
((OtlpGrpcMetricExporter ) exporter )
166
- .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ));
190
+ .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials , configProperties ));
167
191
return builder .build ();
168
192
}
169
193
return exporter ;
170
194
}
171
195
172
- private static Map <String , String > getRequiredHeaderMap (GoogleCredentials credentials ) {
196
+ private static Map <String , String > getRequiredHeaderMap (
197
+ GoogleCredentials credentials , ConfigProperties configProperties ) {
173
198
Map <String , List <String >> gcpHeaders ;
174
199
try {
175
200
// this also refreshes the credentials, if required
@@ -192,7 +217,8 @@ private static Map<String, String> getRequiredHeaderMap(GoogleCredentials creden
192
217
// system properties.
193
218
if (!flattenedHeaders .containsKey (QUOTA_USER_PROJECT_HEADER )) {
194
219
Optional <String > maybeConfiguredQuotaProjectId =
195
- ConfigurableOption .GOOGLE_CLOUD_QUOTA_PROJECT .getConfiguredValueAsOptional ();
220
+ ConfigurableOption .GOOGLE_CLOUD_QUOTA_PROJECT .getConfiguredValueAsOptional (
221
+ configProperties );
196
222
maybeConfiguredQuotaProjectId .ifPresent (
197
223
configuredQuotaProjectId ->
198
224
flattenedHeaders .put (QUOTA_USER_PROJECT_HEADER , configuredQuotaProjectId ));
@@ -202,7 +228,8 @@ private static Map<String, String> getRequiredHeaderMap(GoogleCredentials creden
202
228
203
229
// Updates the current resource with the attributes required for ingesting OTLP data on GCP.
204
230
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 );
206
233
Resource res =
207
234
Resource .create (
208
235
Attributes .of (AttributeKey .stringKey (GCP_USER_PROJECT_ID_KEY ), gcpProjectId ));
0 commit comments