Skip to content

Commit c239237

Browse files
committed
Add option to specify target signals for authentication
1 parent 2823710 commit c239237

File tree

2 files changed

+45
-48
lines changed

2 files changed

+45
-48
lines changed

gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/ConfigurableOption.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,27 @@ public enum ConfigurableOption {
3030
* href="https://cloud.google.com/docs/quotas/set-quota-project">official GCP client
3131
* libraries</a>.
3232
*/
33-
GOOGLE_CLOUD_QUOTA_PROJECT("Google Cloud Quota Project ID");
33+
GOOGLE_CLOUD_QUOTA_PROJECT("Google Cloud Quota Project ID"),
34+
35+
/**
36+
* Specifies a comma-separated list of OpenTelemetry signals for which this authentication
37+
* extension should be active. The authentication mechanisms provided by this extension will only
38+
* be applied to the listed signals. If not set, {@code all} is assumed to be set which means
39+
* authentication is enabled for all supported signals.
40+
*
41+
* <p>Valid signal values are:
42+
*
43+
* <ul>
44+
* <li>{@code metrics} - Enables authentication for metric exports.
45+
* <li>{@code traces} - Enables authentication for trace exports.
46+
* <li>{@code all} - Enables authentication for all exports.
47+
* </ul>
48+
*
49+
* <p>The values are case-sensitive. Whitespace around commas and values is ignored. Can be
50+
* configured using the environment variable `GOOGLE_OTEL_AUTH_TARGET_SIGNALS` or the system
51+
* property `google.otel.auth.target.signals`.
52+
*/
53+
GOOGLE_OTEL_AUTH_TARGET_SIGNALS("Target Signals for Google Auth Extension");
3454

3555
private final String userReadableName;
3656
private final String environmentVariableName;

gcp-auth-extension/src/main/java/io/opentelemetry/contrib/gcp/auth/GcpAuthAutoConfigurationCustomizerProvider.java

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
import io.opentelemetry.sdk.resources.Resource;
2626
import io.opentelemetry.sdk.trace.export.SpanExporter;
2727
import java.io.IOException;
28+
import java.util.Arrays;
2829
import java.util.List;
2930
import java.util.Map;
3031
import java.util.Objects;
3132
import java.util.Optional;
32-
import java.util.regex.Matcher;
33-
import java.util.regex.Pattern;
3433
import java.util.stream.Collectors;
3534
import 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

Comments
 (0)