Repository navigation
feat(storage): log effective gRPC transport configuration for DirectPath over Interconnect #15026
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
af5482f
fa14368
7cf27e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,287 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.google.cloud.storage; | ||
|
|
||
| import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; | ||
| import com.google.auth.Credentials; | ||
| import com.google.cloud.NoCredentials; | ||
| import com.google.common.annotations.VisibleForTesting; | ||
| import io.grpc.internal.GrpcUtil; | ||
| import java.util.logging.Level; | ||
| import java.util.logging.Logger; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
||
| /** | ||
| * Package-private transport classification and startup configuration diagnostics for gRPC channels, | ||
| * supporting CloudPath, DirectPath, and DirectPath over Cloud Interconnect. | ||
| */ | ||
| final class GrpcChannelTelemetry { | ||
|
|
||
| static final String C2P_PREFIX = "google-c2p:///"; | ||
| static final String C2P_EXPERIMENTAL_PREFIX = "google-c2p-experimental:///"; | ||
| static final String FORCE_XDS_PARAM = "force-xds"; | ||
| static final String DIRECT_PATH_INTERCONNECT_HOST = "storage-direct.googleapis.com"; | ||
| static final String DIRECT_PATH_INTERCONNECT_PREPROD_HOST = | ||
| "storage-preprod-direct.sandbox.googleapis.com"; | ||
|
|
||
| @VisibleForTesting | ||
| static final Logger LOG = Logger.getLogger(GrpcChannelTelemetry.class.getName()); | ||
|
|
||
| private static final String DIRECT_PATH_INTERCONNECT_INFIX = "-direct."; | ||
| private static final String ENV_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT = | ||
| "GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT"; | ||
| private static final boolean GRPC_FORCE_XDS_SUPPORTED = detectGrpcForceXdsSupport(); | ||
|
|
||
| /** The transport used to reach the GCS service. */ | ||
| enum TransportType { | ||
| CLOUD_PATH("CloudPath"), | ||
| DIRECT_PATH("DirectPath"), | ||
| DIRECT_PATH_INTERCONNECT("DirectPathInterconnect"); | ||
|
|
||
| private final String label; | ||
|
|
||
| TransportType(String label) { | ||
| this.label = label; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return label; | ||
| } | ||
| } | ||
|
|
||
| static boolean hasQueryParameter(@Nullable String uri, String key) { | ||
| if (uri == null || key == null) { | ||
| return false; | ||
| } | ||
| int fragmentPos = uri.indexOf('#'); | ||
| String addressable = fragmentPos >= 0 ? uri.substring(0, fragmentPos) : uri; | ||
| int queryPos = addressable.indexOf('?'); | ||
| if (queryPos < 0) { | ||
| return false; | ||
| } | ||
| String query = addressable.substring(queryPos + 1); | ||
| int start = 0; | ||
| while (start <= query.length()) { | ||
| int ampPos = query.indexOf('&', start); | ||
| String param = ampPos >= 0 ? query.substring(start, ampPos) : query.substring(start); | ||
| int eqPos = param.indexOf('='); | ||
| String paramName = eqPos >= 0 ? param.substring(0, eqPos) : param; | ||
| if (paramName.equals(key)) { | ||
| return true; | ||
| } | ||
| if (ampPos < 0) { | ||
| break; | ||
| } | ||
| start = ampPos + 1; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| static TransportType detectTransportType(@Nullable String endpoint) { | ||
| if (endpoint == null | ||
| || (!endpoint.startsWith(C2P_PREFIX) && !endpoint.startsWith(C2P_EXPERIMENTAL_PREFIX))) { | ||
| return TransportType.CLOUD_PATH; | ||
| } | ||
| if (hasQueryParameter(endpoint, FORCE_XDS_PARAM)) { | ||
| return TransportType.DIRECT_PATH_INTERCONNECT; | ||
| } | ||
| return TransportType.DIRECT_PATH; | ||
| } | ||
|
|
||
| static String extractHost(@Nullable String endpointOrAuthority) { | ||
| if (endpointOrAuthority == null || endpointOrAuthority.isEmpty()) { | ||
| return ""; | ||
| } | ||
| String normalized = endpointOrAuthority; | ||
| if (normalized.startsWith(C2P_EXPERIMENTAL_PREFIX)) { | ||
| normalized = C2P_PREFIX + normalized.substring(C2P_EXPERIMENTAL_PREFIX.length()); | ||
| } | ||
| int queryIdx = normalized.indexOf('?'); | ||
| if (queryIdx >= 0) { | ||
| normalized = normalized.substring(0, queryIdx); | ||
| } | ||
| int fragmentIdx = normalized.indexOf('#'); | ||
| if (fragmentIdx >= 0) { | ||
| normalized = normalized.substring(0, fragmentIdx); | ||
| } | ||
| String extracted = OpenTelemetryBootstrappingUtils.extractServerAddress(normalized); | ||
| return extracted != null ? extracted : ""; | ||
| } | ||
|
|
||
| static boolean isDirectPathInterconnectHost(@Nullable String host) { | ||
| String h = extractHost(host); | ||
| return DIRECT_PATH_INTERCONNECT_HOST.equals(h) | ||
| || DIRECT_PATH_INTERCONNECT_PREPROD_HOST.equals(h); | ||
| } | ||
|
|
||
| static boolean grpcForceXdsIsSupported() { | ||
| return GRPC_FORCE_XDS_SUPPORTED; | ||
| } | ||
|
|
||
| static boolean isDirectPathInterconnectEnabled(boolean attemptDirectPathXdsOverInterconnect) { | ||
| return isDirectPathInterconnectEnabled( | ||
| attemptDirectPathXdsOverInterconnect, | ||
| System.getenv(ENV_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT)); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static boolean isDirectPathInterconnectEnabled( | ||
| boolean attemptDirectPathXdsOverInterconnect, @Nullable String envValue) { | ||
| if (envValue != null) { | ||
| String trimmed = envValue.trim(); | ||
| if ("true".equalsIgnoreCase(trimmed)) { | ||
| return true; | ||
| } | ||
| if ("false".equalsIgnoreCase(trimmed)) { | ||
| return false; | ||
| } | ||
| } | ||
| return attemptDirectPathXdsOverInterconnect; | ||
| } | ||
|
|
||
| static String resolveEffectiveTarget( | ||
| InstantiatingGrpcChannelProvider provider, boolean interconnectEnabled) { | ||
| String endpoint = provider.getEndpoint(); | ||
| if (endpoint == null) { | ||
| return ""; | ||
| } | ||
| if (endpoint.startsWith(C2P_PREFIX) || endpoint.startsWith(C2P_EXPERIMENTAL_PREFIX)) { | ||
| return endpoint; | ||
| } | ||
| if (!provider.needsCredentials() | ||
| && provider.canUseDirectPath() | ||
| && provider.isDirectPathXdsEnabled()) { | ||
| String serviceAddress = extractHost(endpoint); | ||
| if (interconnectEnabled) { | ||
| return C2P_PREFIX | ||
| + rewriteDirectPathInterconnectHost(serviceAddress) | ||
| + "?" | ||
| + FORCE_XDS_PARAM; | ||
| } | ||
| return C2P_PREFIX + serviceAddress; | ||
| } | ||
| return endpoint; | ||
| } | ||
|
|
||
| static String logChannelConfiguration( | ||
| InstantiatingGrpcChannelProvider baseProvider, | ||
| Credentials credentials, | ||
| boolean attemptDirectPathXdsOverInterconnect) { | ||
| InstantiatingGrpcChannelProvider telemetryProvider = | ||
| credentials instanceof NoCredentials | ||
| ? baseProvider | ||
| : (InstantiatingGrpcChannelProvider) baseProvider.withCredentials(credentials); | ||
| boolean interconnectEnabled = | ||
| isDirectPathInterconnectEnabled(attemptDirectPathXdsOverInterconnect); | ||
| String effectiveTarget = resolveEffectiveTarget(telemetryProvider, interconnectEnabled); | ||
| logChannelConfiguration(effectiveTarget, interconnectEnabled, grpcForceXdsIsSupported()); | ||
| return effectiveTarget; | ||
| } | ||
|
|
||
| static void logChannelConfiguration( | ||
| String effectiveEndpoint, boolean interconnectEnabled, boolean grpcSupportsForceXds) { | ||
| TransportType requestedTransport = detectTransportType(effectiveEndpoint); | ||
| String effectiveAuthority = extractHost(effectiveEndpoint); | ||
| String grpcVersion = GrpcUtil.getGrpcBuildVersion().toString(); | ||
|
|
||
| if (interconnectEnabled && requestedTransport != TransportType.DIRECT_PATH_INTERCONNECT) { | ||
| LOG.log( | ||
| Level.WARNING, | ||
| "DirectPath over Interconnect is enabled, but the effective endpoint does not request" | ||
| + " it. Check whether a custom endpoint, non-default universe domain, disabled" | ||
| + " DirectPath, or unauthenticated credentials overrode the DirectPath over" | ||
| + " Interconnect target. requested_transport_type=" | ||
| + requestedTransport | ||
| + ", endpoint=" | ||
| + effectiveEndpoint); | ||
| return; | ||
| } | ||
| if (!grpcSupportsForceXds && requestedTransport == TransportType.DIRECT_PATH_INTERCONNECT) { | ||
| LOG.log( | ||
| Level.WARNING, | ||
| "DirectPath over Interconnect requires a gRPC version supporting force-xds (found " | ||
| + grpcVersion | ||
| + "). The force-xds query parameter will be ignored by the google-c2p resolver;" | ||
| + " outside GCE this channel will fall back to CloudPath, and on GCE it will use" | ||
| + " standard DirectPath. requested_transport_type=" | ||
| + requestedTransport | ||
| + ", endpoint=" | ||
| + effectiveEndpoint); | ||
| return; | ||
| } | ||
| if (requestedTransport == TransportType.DIRECT_PATH_INTERCONNECT | ||
| && !isDirectPathInterconnectHost(effectiveAuthority)) { | ||
| LOG.log( | ||
| Level.WARNING, | ||
| "DirectPath over Interconnect is active, but the channel authority is not a DirectPath" | ||
| + " over Interconnect host; the service will route RPCs as standard DirectPath. Use '" | ||
| + DIRECT_PATH_INTERCONNECT_HOST | ||
| + "' (or '" | ||
| + DIRECT_PATH_INTERCONNECT_PREPROD_HOST | ||
| + "' in preprod). requested_transport_type=" | ||
| + requestedTransport | ||
| + ", endpoint=" | ||
| + effectiveEndpoint | ||
| + ", authority=" | ||
| + effectiveAuthority | ||
| + ", grpc_version=" | ||
| + grpcVersion); | ||
| return; | ||
| } | ||
| Level level = | ||
| requestedTransport == TransportType.DIRECT_PATH_INTERCONNECT ? Level.INFO : Level.CONFIG; | ||
| LOG.log( | ||
| level, | ||
| "Configured gRPC transport. requested_transport_type=" | ||
| + requestedTransport | ||
| + ", endpoint=" | ||
| + effectiveEndpoint | ||
| + ", authority=" | ||
| + effectiveAuthority | ||
| + ", grpc_version=" | ||
| + grpcVersion); | ||
| } | ||
|
|
||
| private GrpcChannelTelemetry() {} | ||
|
|
||
| private static boolean detectGrpcForceXdsSupport() { | ||
| try { | ||
| Class<?> clazz = | ||
| Class.forName( | ||
| "io.grpc.googleapis.GoogleCloudToProdNameResolver", | ||
| /* initialize= */ false, | ||
| GrpcChannelTelemetry.class.getClassLoader()); | ||
| clazz.getDeclaredField("forceXds"); | ||
| return true; | ||
| } catch (ReflectiveOperationException | LinkageError | SecurityException e) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static String rewriteDirectPathInterconnectHost(String serviceAddress) { | ||
| if (!serviceAddress.contains(DIRECT_PATH_INTERCONNECT_INFIX)) { | ||
| int dotIndex = serviceAddress.indexOf('.'); | ||
| if (dotIndex > 0) { | ||
| return serviceAddress.substring(0, dotIndex) | ||
| + DIRECT_PATH_INTERCONNECT_INFIX | ||
| + serviceAddress.substring(dotIndex + 1); | ||
| } | ||
| } | ||
| return serviceAddress; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -326,7 +326,7 @@ private Tuple<StorageSettings, Opts<UserProject>> resolveSettingsAndOpts() throw | |
| InstantiatingGrpcChannelProvider.newBuilder() | ||
| .setEndpoint(endpoint) | ||
| .setAllowNonDefaultServiceAccount(true) | ||
| .setAttemptDirectPath(attemptDirectPath || attemptDirectPathXdsOverInterconnect) | ||
| .setAttemptDirectPath(attemptDirectPath) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing this to Should we note this precedence in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done — documented the setAttemptDirectPath(false) and GOOGLE_CLOUD_DISABLE_DIRECT_PATH precedence in Builder#setAttemptDirectPathXdsOverInterconnect Javadoc. Co-authored by AI Agent |
||
| .setAttemptDirectPathXdsOverInterconnect(attemptDirectPathXdsOverInterconnect); | ||
|
|
||
| if (!DIRECT_PATH_BOUND_TOKEN_DISABLED) { | ||
|
|
@@ -357,7 +357,10 @@ private Tuple<StorageSettings, Opts<UserProject>> resolveSettingsAndOpts() throw | |
| channelProviderBuilder.setChannelConfigurator(channelConfigurator); | ||
| } | ||
|
|
||
| builder.setTransportChannelProvider(channelProviderBuilder.build()); | ||
| InstantiatingGrpcChannelProvider baseProvider = channelProviderBuilder.build(); | ||
| GrpcChannelTelemetry.logChannelConfiguration( | ||
| baseProvider, credentials, attemptDirectPathXdsOverInterconnect); | ||
| builder.setTransportChannelProvider(baseProvider); | ||
| RetrySettings baseRetrySettings = getRetrySettings(); | ||
| RetrySettings readRetrySettings = | ||
| baseRetrySettings.toBuilder() | ||
|
|
@@ -584,7 +587,10 @@ public GrpcStorageOptions.Builder setAttemptDirectPath(boolean attemptDirectPath | |
| * | ||
| * <p>This can also be enabled or disabled via the {@code | ||
| * GOOGLE_CLOUD_ENABLE_DIRECT_PATH_XDS_OVER_INTERCONNECT} environment variable ({@code "true"} | ||
| * or {@code "false"}), which takes precedence over this builder option. | ||
| * or {@code "false"}), which takes precedence over this builder option. Disabling DirectPath | ||
| * via {@link #setAttemptDirectPath(boolean) setAttemptDirectPath(false)} or the {@code | ||
| * GOOGLE_CLOUD_DISABLE_DIRECT_PATH} environment variable also disables DirectPath over | ||
| * Interconnect. | ||
| * | ||
| * @since 2.76.0 | ||
| */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No test covers
resolveEffectiveTargetwhen the input host already contains-direct.(such ashttps://storage-direct.googleapis.com), so removing this guard would double-rewrite it without failing any test. Consider adding a test case with an already--direct.host.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done — added grpcStorageOptions_directPathOverInterconnect_doesNotDoubleRewriteDirectHost testing https://storage-direct.googleapis.com to ensure no double -direct. rewrite.
Co-authored by AI Agent