Skip to content

Commit 0c5ff96

Browse files
committed
Merge branch 'main' into update-auth-extension-config
2 parents 7ae860a + fc6507e commit 0c5ff96

File tree

17 files changed

+716
-61
lines changed

17 files changed

+716
-61
lines changed

.github/workflows/auto-spotless-check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
java-version: 17
2626

2727
- name: Set up gradle
28-
uses: gradle/actions/setup-gradle@06832c7b30a0129d7fb559bcc6e43d26f6374244 # v4.3.1
28+
uses: gradle/actions/setup-gradle@8379f6a1328ee0e06e2bb424dadb7b159856a326 # v4.4.0
2929
with:
3030
cache-read-only: true
3131

buildSrc/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
plugins {
22
`kotlin-dsl`
33
// When updating, update below in dependencies too
4-
id("com.diffplug.spotless") version "7.0.3"
4+
id("com.diffplug.spotless") version "7.0.4"
55
}
66

77
repositories {
@@ -12,7 +12,7 @@ repositories {
1212

1313
dependencies {
1414
// When updating, update above in plugins too
15-
implementation("com.diffplug.spotless:spotless-plugin-gradle:7.0.3")
15+
implementation("com.diffplug.spotless:spotless-plugin-gradle:7.0.4")
1616
implementation("net.ltgt.gradle:gradle-errorprone-plugin:4.2.0")
1717
implementation("net.ltgt.gradle:gradle-nullaway-plugin:2.2.0")
1818
implementation("org.owasp:dependency-check-gradle:12.1.1")

dependencyManagement/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dependencies {
2828
api("com.google.errorprone:error_prone_annotations:2.38.0")
2929
api("com.google.errorprone:error_prone_core:2.38.0")
3030
api("io.github.netmikey.logunit:logunit-jul:2.0.0")
31-
api("io.opentelemetry.proto:opentelemetry-proto:1.5.0-alpha")
31+
api("io.opentelemetry.proto:opentelemetry-proto:1.7.0-alpha")
3232
api("io.prometheus:simpleclient:0.16.0")
3333
api("io.prometheus:simpleclient_common:0.16.0")
3434
api("io.prometheus:simpleclient_httpserver:0.16.0")

disk-buffering/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ dependencies {
2828
testImplementation("org.mockito:mockito-inline")
2929
testImplementation("io.opentelemetry:opentelemetry-sdk-testing")
3030

31-
protos("io.opentelemetry.proto:opentelemetry-proto:1.5.0-alpha@jar")
31+
protos("io.opentelemetry.proto:opentelemetry-proto:1.7.0-alpha@jar")
3232
}
3333

3434
animalsniffer {

gcp-auth-extension/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ Here is a list of required and optional configuration available for the extensio
4747

4848
- Can also be configured using `google.cloud.quota.project` system property.
4949

50+
- `GOOGLE_OTEL_AUTH_TARGET_SIGNALS`: Environment variable that specifies a comma-separated list of OpenTelemetry signals for which this authentication extension should be active. Valid values contain - `metrics`, `traces` or `all`. If left unspecified, `all` is assumed meaning the extension will attempt to apply authentication to exports for all signals.
51+
52+
- Can also be configured using `google.otel.auth.target.signals` system property.
53+
5054
## Usage
5155

5256
### With OpenTelemetry Java agent

gcp-auth-extension/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ dependencies {
4141
testImplementation("org.mockito:mockito-inline")
4242
testImplementation("org.mockito:mockito-junit-jupiter")
4343
testImplementation("org.mock-server:mockserver-netty:5.15.0")
44-
testImplementation("io.opentelemetry.proto:opentelemetry-proto:1.5.0-alpha")
44+
testImplementation("io.opentelemetry.proto:opentelemetry-proto:1.7.0-alpha")
4545
testImplementation("org.springframework.boot:spring-boot-starter-web:2.7.18")
4646
testImplementation("org.springframework.boot:spring-boot-starter:2.7.18")
4747
testImplementation("org.springframework.boot:spring-boot-starter-test:2.7.18")

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

3656
private final String userReadableName;
3757
private final String environmentVariableName;

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

Lines changed: 79 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,28 @@
1010
import io.opentelemetry.api.common.AttributeKey;
1111
import io.opentelemetry.api.common.Attributes;
1212
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;
1315
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
1416
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporterBuilder;
17+
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporter;
18+
import io.opentelemetry.exporter.otlp.metrics.OtlpGrpcMetricExporterBuilder;
1519
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
1620
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporterBuilder;
1721
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizer;
1822
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
1923
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
24+
import io.opentelemetry.sdk.metrics.export.MetricExporter;
2025
import io.opentelemetry.sdk.resources.Resource;
2126
import io.opentelemetry.sdk.trace.export.SpanExporter;
2227
import java.io.IOException;
28+
import java.util.Arrays;
2329
import java.util.List;
2430
import java.util.Map;
2531
import java.util.Objects;
2632
import java.util.Optional;
2733
import java.util.stream.Collectors;
34+
import javax.annotation.Nonnull;
2835

2936
/**
3037
* An AutoConfigurationCustomizerProvider for Google Cloud Platform (GCP) OpenTelemetry (OTLP)
@@ -46,13 +53,29 @@ public class GcpAuthAutoConfigurationCustomizerProvider
4653
static final String QUOTA_USER_PROJECT_HEADER = "x-goog-user-project";
4754
static final String GCP_USER_PROJECT_ID_KEY = "gcp.project_id";
4855

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+
4960
/**
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.
5163
*
5264
* <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.
5679
*
5780
* @param autoConfiguration the AutoConfigurationCustomizer to customize.
5881
* @throws GoogleAuthException if there's an error retrieving Google Application Default
@@ -61,7 +84,7 @@ public class GcpAuthAutoConfigurationCustomizerProvider
6184
* not configured through environment variables or system properties.
6285
*/
6386
@Override
64-
public void customize(AutoConfigurationCustomizer autoConfiguration) {
87+
public void customize(@Nonnull AutoConfigurationCustomizer autoConfiguration) {
6588
GoogleCredentials credentials;
6689
try {
6790
credentials = GoogleCredentials.getApplicationDefault();
@@ -70,8 +93,11 @@ public void customize(AutoConfigurationCustomizer autoConfiguration) {
7093
}
7194
autoConfiguration
7295
.addSpanExporterCustomizer(
73-
(exporter, configProperties) ->
74-
addAuthorizationHeaders(exporter, credentials, configProperties))
96+
(spanExporter, configProperties) ->
97+
customizeSpanExporter(spanExporter, credentials, configProperties))
98+
.addMetricExporterCustomizer(
99+
(metricExporter, configProperties) ->
100+
customizeMetricExporter(metricExporter, credentials, configProperties))
75101
.addResourceCustomizer(GcpAuthAutoConfigurationCustomizerProvider::customizeResource);
76102
}
77103

@@ -80,6 +106,34 @@ public int order() {
80106
return Integer.MAX_VALUE - 1;
81107
}
82108

109+
private static SpanExporter customizeSpanExporter(
110+
SpanExporter exporter, GoogleCredentials credentials, ConfigProperties configProperties) {
111+
if (isSignalTargeted(SIGNAL_TYPE_TRACES, configProperties)) {
112+
return addAuthorizationHeaders(exporter, credentials, configProperties);
113+
}
114+
return exporter;
115+
}
116+
117+
private static MetricExporter customizeMetricExporter(
118+
MetricExporter exporter, GoogleCredentials credentials, ConfigProperties configProperties) {
119+
if (isSignalTargeted(SIGNAL_TYPE_METRICS, configProperties)) {
120+
return addAuthorizationHeaders(exporter, credentials, configProperties);
121+
}
122+
return exporter;
123+
}
124+
125+
// Checks if the auth extension is configured to target the passed signal for authentication.
126+
private static boolean isSignalTargeted(String checkSignal, ConfigProperties configProperties) {
127+
String userSpecifiedTargetedSignals =
128+
ConfigurableOption.GOOGLE_OTEL_AUTH_TARGET_SIGNALS.getConfiguredValueWithFallback(
129+
configProperties, () -> SIGNAL_TYPE_ALL);
130+
return Arrays.stream(userSpecifiedTargetedSignals.split(","))
131+
.map(String::trim)
132+
.anyMatch(
133+
targetedSignal ->
134+
targetedSignal.equals(checkSignal) || targetedSignal.equals(SIGNAL_TYPE_ALL));
135+
}
136+
83137
// Adds authorization headers to the calls made by the OtlpGrpcSpanExporter and
84138
// OtlpHttpSpanExporter.
85139
private static SpanExporter addAuthorizationHeaders(
@@ -98,6 +152,24 @@ private static SpanExporter addAuthorizationHeaders(
98152
return exporter;
99153
}
100154

155+
// Adds authorization headers to the calls made by the OtlpGrpcMetricExporter and
156+
// OtlpHttpMetricExporter.
157+
private static MetricExporter addAuthorizationHeaders(
158+
MetricExporter exporter, GoogleCredentials credentials, ConfigProperties configProperties) {
159+
if (exporter instanceof OtlpHttpMetricExporter) {
160+
OtlpHttpMetricExporterBuilder builder =
161+
((OtlpHttpMetricExporter) exporter)
162+
.toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials, configProperties));
163+
return builder.build();
164+
} else if (exporter instanceof OtlpGrpcMetricExporter) {
165+
OtlpGrpcMetricExporterBuilder builder =
166+
((OtlpGrpcMetricExporter) exporter)
167+
.toBuilder().setHeaders(() -> getRequiredHeaderMap(credentials, configProperties));
168+
return builder.build();
169+
}
170+
return exporter;
171+
}
172+
101173
private static Map<String, String> getRequiredHeaderMap(
102174
GoogleCredentials credentials, ConfigProperties configProperties) {
103175
Map<String, List<String>> gcpHeaders;

0 commit comments

Comments
 (0)