10
10
import io .opentelemetry .api .common .AttributeKey ;
11
11
import io .opentelemetry .api .common .Attributes ;
12
12
import 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 ;
13
15
import io .opentelemetry .exporter .otlp .http .trace .OtlpHttpSpanExporter ;
14
16
import io .opentelemetry .exporter .otlp .http .trace .OtlpHttpSpanExporterBuilder ;
17
+ import io .opentelemetry .exporter .otlp .metrics .OtlpGrpcMetricExporter ;
18
+ import io .opentelemetry .exporter .otlp .metrics .OtlpGrpcMetricExporterBuilder ;
15
19
import io .opentelemetry .exporter .otlp .trace .OtlpGrpcSpanExporter ;
16
20
import io .opentelemetry .exporter .otlp .trace .OtlpGrpcSpanExporterBuilder ;
17
21
import io .opentelemetry .sdk .autoconfigure .spi .AutoConfigurationCustomizer ;
18
22
import io .opentelemetry .sdk .autoconfigure .spi .AutoConfigurationCustomizerProvider ;
19
23
import io .opentelemetry .sdk .autoconfigure .spi .ConfigProperties ;
24
+ import io .opentelemetry .sdk .metrics .export .MetricExporter ;
20
25
import io .opentelemetry .sdk .resources .Resource ;
21
26
import io .opentelemetry .sdk .trace .export .SpanExporter ;
22
27
import java .io .IOException ;
28
+ import java .util .Arrays ;
23
29
import java .util .List ;
24
30
import java .util .Map ;
25
31
import java .util .Objects ;
26
32
import java .util .Optional ;
27
33
import java .util .stream .Collectors ;
34
+ import javax .annotation .Nonnull ;
28
35
29
36
/**
30
37
* An AutoConfigurationCustomizerProvider for Google Cloud Platform (GCP) OpenTelemetry (OTLP)
@@ -46,13 +53,29 @@ public class GcpAuthAutoConfigurationCustomizerProvider
46
53
static final String QUOTA_USER_PROJECT_HEADER = "x-goog-user-project" ;
47
54
static final String GCP_USER_PROJECT_ID_KEY = "gcp.project_id" ;
48
55
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
+
49
60
/**
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.
51
63
*
52
64
* <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.
56
79
*
57
80
* @param autoConfiguration the AutoConfigurationCustomizer to customize.
58
81
* @throws GoogleAuthException if there's an error retrieving Google Application Default
@@ -61,7 +84,7 @@ public class GcpAuthAutoConfigurationCustomizerProvider
61
84
* not configured through environment variables or system properties.
62
85
*/
63
86
@ Override
64
- public void customize (AutoConfigurationCustomizer autoConfiguration ) {
87
+ public void customize (@ Nonnull AutoConfigurationCustomizer autoConfiguration ) {
65
88
GoogleCredentials credentials ;
66
89
try {
67
90
credentials = GoogleCredentials .getApplicationDefault ();
@@ -70,7 +93,10 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
70
93
}
71
94
autoConfiguration
72
95
.addSpanExporterCustomizer (
73
- (exporter , configProperties ) -> addAuthorizationHeaders (exporter , credentials ))
96
+ (spanExporter , configProperties ) -> customizeSpanExporter (spanExporter , credentials ))
97
+ .addMetricExporterCustomizer (
98
+ (metricExporter , configProperties ) ->
99
+ customizeMetricExporter (metricExporter , credentials ))
74
100
.addResourceCustomizer (GcpAuthAutoConfigurationCustomizerProvider ::customizeResource );
75
101
}
76
102
@@ -79,6 +105,34 @@ public int order() {
79
105
return Integer .MAX_VALUE - 1 ;
80
106
}
81
107
108
+ private static SpanExporter customizeSpanExporter (
109
+ SpanExporter exporter , GoogleCredentials credentials ) {
110
+ if (isSignalTargeted (SIGNAL_TYPE_TRACES )) {
111
+ return addAuthorizationHeaders (exporter , credentials );
112
+ }
113
+ return exporter ;
114
+ }
115
+
116
+ private static MetricExporter customizeMetricExporter (
117
+ MetricExporter exporter , GoogleCredentials credentials ) {
118
+ if (isSignalTargeted (SIGNAL_TYPE_METRICS )) {
119
+ return addAuthorizationHeaders (exporter , credentials );
120
+ }
121
+ return exporter ;
122
+ }
123
+
124
+ // Checks if the auth extension is configured to target the passed signal for authentication.
125
+ private static boolean isSignalTargeted (String checkSignal ) {
126
+ String userSpecifiedTargetedSignals =
127
+ ConfigurableOption .GOOGLE_OTEL_AUTH_TARGET_SIGNALS .getConfiguredValueWithFallback (
128
+ () -> SIGNAL_TYPE_ALL );
129
+ return Arrays .stream (userSpecifiedTargetedSignals .split ("," ))
130
+ .map (String ::trim )
131
+ .anyMatch (
132
+ targetedSignal ->
133
+ targetedSignal .equals (checkSignal ) || targetedSignal .equals (SIGNAL_TYPE_ALL ));
134
+ }
135
+
82
136
// Adds authorization headers to the calls made by the OtlpGrpcSpanExporter and
83
137
// OtlpHttpSpanExporter.
84
138
private static SpanExporter addAuthorizationHeaders (
@@ -97,6 +151,24 @@ private static SpanExporter addAuthorizationHeaders(
97
151
return exporter ;
98
152
}
99
153
154
+ // Adds authorization headers to the calls made by the OtlpGrpcMetricExporter and
155
+ // OtlpHttpMetricExporter.
156
+ private static MetricExporter addAuthorizationHeaders (
157
+ MetricExporter exporter , GoogleCredentials credentials ) {
158
+ if (exporter instanceof OtlpHttpMetricExporter ) {
159
+ OtlpHttpMetricExporterBuilder builder =
160
+ ((OtlpHttpMetricExporter ) exporter )
161
+ .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ));
162
+ return builder .build ();
163
+ } else if (exporter instanceof OtlpGrpcMetricExporter ) {
164
+ OtlpGrpcMetricExporterBuilder builder =
165
+ ((OtlpGrpcMetricExporter ) exporter )
166
+ .toBuilder ().setHeaders (() -> getRequiredHeaderMap (credentials ));
167
+ return builder .build ();
168
+ }
169
+ return exporter ;
170
+ }
171
+
100
172
private static Map <String , String > getRequiredHeaderMap (GoogleCredentials credentials ) {
101
173
Map <String , List <String >> gcpHeaders ;
102
174
try {
0 commit comments