Repository navigation
feat(storage): support DirectPath over Interconnect in Cloud Monitoring and register channel_creation_latency view #15025
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
a6f9328
5e6de50
640013f
ddcfb06
b21f67c
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 |
|---|---|---|
|
|
@@ -33,6 +33,9 @@ | |
| import io.opentelemetry.api.common.Attributes; | ||
| import io.opentelemetry.api.common.AttributesBuilder; | ||
| import io.opentelemetry.api.internal.StringUtils; | ||
| import io.opentelemetry.api.metrics.DoubleHistogram; | ||
| import io.opentelemetry.api.metrics.Meter; | ||
| import io.opentelemetry.api.metrics.MeterProvider; | ||
| import io.opentelemetry.contrib.gcp.resource.GCPResourceProvider; | ||
| import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
| import io.opentelemetry.sdk.common.CompletableResultCode; | ||
|
|
@@ -61,12 +64,29 @@ | |
| import java.util.Objects; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.function.BiConsumer; | ||
| import java.util.function.Supplier; | ||
| import java.util.logging.Logger; | ||
| import java.util.stream.Collectors; | ||
| import org.checkerframework.checker.nullness.qual.NonNull; | ||
| import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
|
||
| final class OpenTelemetryBootstrappingUtils { | ||
| static final String CHANNEL_CREATION_LATENCY_INSTRUMENT = | ||
| "internal/client/channel_creation_latency"; | ||
| static final String CHANNEL_CREATION_LATENCY_VIEW_NAME = | ||
| "storage.googleapis.com/internal/client/channel_creation_latency"; | ||
| static final String CHANNEL_CREATION_LATENCY_DESCRIPTION = | ||
| "Time from client construction until the first gRPC channel became ready."; | ||
| static final String CHANNEL_CREATION_LATENCY_UNIT = "s"; | ||
| static final AttributeKey<String> TRANSPORT_TYPE_KEY = AttributeKey.stringKey("transport_type"); | ||
|
|
||
| private static final String C2P_PREFIX = "google-c2p:///"; | ||
| private static final String C2P_EXPERIMENTAL_PREFIX = "google-c2p-experimental:///"; | ||
| private static final String DIRECT_PATH_INTERCONNECT_HOST = "storage-direct.googleapis.com"; | ||
| private static final String DIRECT_PATH_INTERCONNECT_PREPROD_HOST = | ||
| "storage-preprod-direct.sandbox.googleapis.com"; | ||
|
|
||
| private static final Collection<String> METRICS_TO_ENABLE = | ||
| ImmutableList.of( | ||
| "grpc.lb.wrr.rr_fallback", | ||
|
|
@@ -128,7 +148,30 @@ static ChannelConfigurator enableGrpcMetrics( | |
| grpcOpenTelemetry.configureChannelBuilder(b); | ||
| return b; | ||
| }; | ||
| return otelConfigurator.andThen(channelConfigurator); | ||
| return new GrpcMetricsChannelConfigurator( | ||
| otelConfigurator.andThen(channelConfigurator), provider); | ||
| } | ||
|
|
||
| @SuppressWarnings("rawtypes") // ManagedChannelBuilder | ||
| static final class GrpcMetricsChannelConfigurator | ||
| implements ChannelConfigurator, Supplier<MeterProvider> { | ||
| private final ChannelConfigurator delegate; | ||
| private final MeterProvider meterProvider; | ||
|
|
||
| GrpcMetricsChannelConfigurator(ChannelConfigurator delegate, MeterProvider meterProvider) { | ||
| this.delegate = delegate; | ||
| this.meterProvider = meterProvider; | ||
| } | ||
|
|
||
| @Override | ||
| public ManagedChannelBuilder apply(ManagedChannelBuilder input) { | ||
| return delegate.apply(input); | ||
| } | ||
|
|
||
| @Override | ||
| public MeterProvider get() { | ||
| return meterProvider; | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("rawtypes") // ManagedChannelBuilder | ||
|
|
@@ -168,13 +211,21 @@ public ManagedChannelBuilder apply(ManagedChannelBuilder input) { | |
| } | ||
|
|
||
| @VisibleForTesting | ||
| static String getCloudMonitoringEndpoint(String endpoint, String universeDomain) { | ||
| static String getCloudMonitoringEndpoint( | ||
| @Nullable String endpoint, @Nullable String universeDomain) { | ||
| if (endpoint == null || endpoint.isEmpty()) { | ||
| return "monitoring.googleapis.com:443"; | ||
| } | ||
| String metricServiceEndpoint = "monitoring.googleapis.com"; | ||
| HostAndPort parsed = parseHostAndPort(endpoint); | ||
| String host = parsed != null ? parsed.getHost() : extractHostFallback(endpoint); | ||
| int port = parsed != null ? parsed.getPortOrDefault(443) : 443; | ||
|
|
||
| // use contains instead of equals because endpoint has a port in it | ||
| if (universeDomain != null && endpoint.contains("storage." + universeDomain)) { | ||
| if (universeDomain != null | ||
| && (host.equals("storage." + universeDomain) | ||
| || host.equals("storage-direct." + universeDomain))) { | ||
| metricServiceEndpoint = "monitoring." + universeDomain; | ||
| } else if (!endpoint.contains("storage.googleapis.com")) { | ||
| } else if (!host.equals("storage.googleapis.com") && !isDirectPathInterconnectHost(host)) { | ||
|
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.
Is the check, and its two constants, needed? If it is kept for readability, a short comment saying so would help. |
||
| String canonicalEndpoint = "storage.googleapis.com"; | ||
| String privateEndpoint = "private.googleapis.com"; | ||
| String restrictedEndpoint = "restricted.googleapis.com"; | ||
|
|
@@ -183,35 +234,76 @@ static String getCloudMonitoringEndpoint(String endpoint, String universeDomain) | |
| privateEndpoint = "private." + universeDomain; | ||
| restrictedEndpoint = "restricted." + universeDomain; | ||
| } | ||
| final String resolvedHost = host; | ||
| String match = | ||
| ImmutableList.of(canonicalEndpoint, privateEndpoint, restrictedEndpoint).stream() | ||
| .filter(s -> endpoint.contains(s) || endpoint.contains("google-c2p:///" + s)) | ||
| .filter(resolvedHost::contains) | ||
|
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.
Consider dropping |
||
| .collect(Collectors.joining()); | ||
|
nidhiii-27 marked this conversation as resolved.
Resolved
|
||
| if (!StringUtils.isNullOrEmpty(match)) { | ||
| metricServiceEndpoint = match; | ||
| } | ||
| } | ||
| HostAndPort parsed = parseHostAndPort(endpoint); | ||
| int port = parsed != null ? parsed.getPortOrDefault(443) : 443; | ||
| return metricServiceEndpoint + ":" + port; | ||
| } | ||
|
|
||
| private static boolean isDirectPathInterconnectHost(String host) { | ||
| return DIRECT_PATH_INTERCONNECT_HOST.equals(host) | ||
| || DIRECT_PATH_INTERCONNECT_PREPROD_HOST.equals(host); | ||
| } | ||
|
|
||
| private static String extractHostFallback(String endpoint) { | ||
|
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.
Please add a case with a host such as |
||
| String s = stripEndpointScheme(endpoint.trim()); | ||
| int end = indexOfPathQueryOrFragment(s); | ||
| String hostPort = end >= 0 ? s.substring(0, end) : s; | ||
| int colon = hostPort.lastIndexOf(':'); | ||
| if (colon >= 0) { | ||
| int closeBracket = hostPort.indexOf(']'); | ||
| if (closeBracket >= 0 && colon < closeBracket) { | ||
| return hostPort; | ||
| } | ||
| if (closeBracket < 0 && hostPort.indexOf(':') != colon) { | ||
| return hostPort; | ||
| } | ||
| return hostPort.substring(0, colon); | ||
| } | ||
| return hostPort; | ||
| } | ||
|
|
||
| private static String stripEndpointScheme(String s) { | ||
| if (s.startsWith(C2P_PREFIX)) { | ||
| return s.substring(C2P_PREFIX.length()); | ||
| } | ||
| if (s.startsWith(C2P_EXPERIMENTAL_PREFIX)) { | ||
| return s.substring(C2P_EXPERIMENTAL_PREFIX.length()); | ||
| } | ||
| if (s.startsWith("https://")) { | ||
| return s.substring("https://".length()); | ||
| } | ||
| if (s.startsWith("http://")) { | ||
| return s.substring("http://".length()); | ||
| } | ||
| return s; | ||
| } | ||
|
|
||
| private static int indexOfPathQueryOrFragment(String s) { | ||
| for (int i = 0; i < s.length(); i++) { | ||
| char c = s.charAt(i); | ||
| if (c == '/' || c == '?' || c == '#') { | ||
| return i; | ||
| } | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| @Nullable | ||
| private static HostAndPort parseHostAndPort(@Nullable String host) { | ||
| if (host == null || host.trim().isEmpty()) { | ||
| return null; | ||
| } | ||
| String s = host.trim(); | ||
| if (s.startsWith("google-c2p:///")) { | ||
| s = s.substring("google-c2p:///".length()); | ||
| } else if (s.startsWith("https://")) { | ||
| s = s.substring("https://".length()); | ||
| } else if (s.startsWith("http://")) { | ||
| s = s.substring("http://".length()); | ||
| } | ||
| int slashIdx = s.indexOf('/'); | ||
| if (slashIdx >= 0) { | ||
| s = s.substring(0, slashIdx); | ||
| String s = stripEndpointScheme(host.trim()); | ||
| int endIdx = indexOfPathQueryOrFragment(s); | ||
| if (endIdx >= 0) { | ||
| s = s.substring(0, endIdx); | ||
| } | ||
| if (s.isEmpty()) { | ||
| return null; | ||
|
|
@@ -415,6 +507,7 @@ static SdkMeterProvider createMeterProvider( | |
| "By", | ||
| "A view of grpc/client/attempt/sent_total_compressed_message_size with histogram boundaries" | ||
| + " more appropriate for Google Cloud Storage RPCs"); | ||
| registerGrpcChannelViews(providerBuilder); | ||
|
|
||
| return buildMeterProvider( | ||
| cloudMonitoringExporter, | ||
|
|
@@ -569,6 +662,41 @@ private static SdkMeterProvider buildMeterProvider( | |
| return providerBuilder.build(); | ||
| } | ||
|
|
||
| static SdkMeterProviderBuilder registerGrpcChannelViews(SdkMeterProviderBuilder providerBuilder) { | ||
| addHistogramView( | ||
| providerBuilder, | ||
| latencyHistogramBoundaries(), | ||
| CHANNEL_CREATION_LATENCY_INSTRUMENT, | ||
| CHANNEL_CREATION_LATENCY_VIEW_NAME, | ||
| StorageClientMetrics.METER_NAME, | ||
| CHANNEL_CREATION_LATENCY_UNIT, | ||
| CHANNEL_CREATION_LATENCY_DESCRIPTION); | ||
| return providerBuilder; | ||
| } | ||
|
|
||
| static BiConsumer<String, Duration> createChannelCreationLatencyRecorder( | ||
| @Nullable MeterProvider meterProvider) { | ||
| if (meterProvider == null) { | ||
| return (transportType, latency) -> {}; | ||
| } | ||
| Meter meter = | ||
| meterProvider | ||
| .meterBuilder(StorageClientMetrics.METER_NAME) | ||
| .setInstrumentationVersion(StorageOptions.version()) | ||
| .build(); | ||
| DoubleHistogram histogram = | ||
| meter | ||
| .histogramBuilder(CHANNEL_CREATION_LATENCY_INSTRUMENT) | ||
| .setDescription(CHANNEL_CREATION_LATENCY_DESCRIPTION) | ||
| .setUnit(CHANNEL_CREATION_LATENCY_UNIT) | ||
| .setExplicitBucketBoundariesAdvice(latencyHistogramBoundaries()) | ||
| .build(); | ||
| return (transportType, latency) -> { | ||
| double seconds = latency.toNanos() / 1_000_000_000.0; | ||
| histogram.record(seconds, Attributes.of(TRANSPORT_TYPE_KEY, transportType)); | ||
| }; | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static SdkMeterProviderBuilder registerClientViews(SdkMeterProviderBuilder providerBuilder) { | ||
| for (String metric : CLIENT_COUNTERS) { | ||
|
|
||
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.
IIUC,
implements Supplier<MeterProvider>makes the provider reachable only while the caller holds this exact type.ChannelConfigurator.andThenreturns a plain lambda, so any later composition silently drops the provider and the recorder becomes a no-op.Should we return a small holder with both the configurator and the provider from a package-private overload?