Skip to content

Commit 82a8c8e

Browse files
committed
feat(storage): support DirectPath over Interconnect in Cloud Monitoring and register channel_creation_latency view
- Register storage.googleapis.com/internal/client/channel_creation_latency histogram view in OpenTelemetryBootstrappingUtils.createMeterProvider. - Expose MeterProvider from enableGrpcMetrics via GrpcMetricsChannelConfigurator implementing Supplier<MeterProvider> while preserving the existing method signature. - Update getCloudMonitoringEndpoint to support DirectPath over Cloud Interconnect endpoints (storage-direct.googleapis.com, storage-preprod-direct.sandbox.googleapis.com, storage-direct.<universeDomain>, and google-c2p:/// / google-c2p-experimental:/// targets with query/fragment parameters) with defensive null/empty handling. - Add unit tests in OpenTelemetryBootstrappingUtilsTest. [Generated-by: AI]
1 parent 589c66c commit 82a8c8e

2 files changed

Lines changed: 218 additions & 19 deletions

File tree

‎java-storage/google-cloud-storage/src/main/java/com/google/cloud/storage/OpenTelemetryBootstrappingUtils.java‎

Lines changed: 111 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.opentelemetry.api.common.Attributes;
3434
import io.opentelemetry.api.common.AttributesBuilder;
3535
import io.opentelemetry.api.internal.StringUtils;
36+
import io.opentelemetry.api.metrics.MeterProvider;
3637
import io.opentelemetry.contrib.gcp.resource.GCPResourceProvider;
3738
import io.opentelemetry.sdk.OpenTelemetrySdk;
3839
import io.opentelemetry.sdk.common.CompletableResultCode;
@@ -61,12 +62,27 @@
6162
import java.util.Objects;
6263
import java.util.UUID;
6364
import java.util.concurrent.atomic.AtomicBoolean;
65+
import java.util.function.Supplier;
6466
import java.util.logging.Logger;
6567
import java.util.stream.Collectors;
6668
import org.checkerframework.checker.nullness.qual.NonNull;
6769
import org.checkerframework.checker.nullness.qual.Nullable;
6870

6971
final class OpenTelemetryBootstrappingUtils {
72+
static final String CHANNEL_CREATION_LATENCY_INSTRUMENT =
73+
"internal/client/channel_creation_latency";
74+
static final String CHANNEL_CREATION_LATENCY_VIEW_NAME =
75+
"storage.googleapis.com/internal/client/channel_creation_latency";
76+
static final String CHANNEL_CREATION_LATENCY_DESCRIPTION =
77+
"Time from client construction until the first gRPC channel became ready.";
78+
static final String CHANNEL_CREATION_LATENCY_UNIT = "s";
79+
80+
private static final String C2P_PREFIX = "google-c2p:///";
81+
private static final String C2P_EXPERIMENTAL_PREFIX = "google-c2p-experimental:///";
82+
private static final String DIRECT_PATH_INTERCONNECT_HOST = "storage-direct.googleapis.com";
83+
private static final String DIRECT_PATH_INTERCONNECT_PREPROD_HOST =
84+
"storage-preprod-direct.sandbox.googleapis.com";
85+
7086
private static final Collection<String> METRICS_TO_ENABLE =
7187
ImmutableList.of(
7288
"grpc.lb.wrr.rr_fallback",
@@ -128,7 +144,30 @@ static ChannelConfigurator enableGrpcMetrics(
128144
grpcOpenTelemetry.configureChannelBuilder(b);
129145
return b;
130146
};
131-
return otelConfigurator.andThen(channelConfigurator);
147+
return new GrpcMetricsChannelConfigurator(
148+
otelConfigurator.andThen(channelConfigurator), provider);
149+
}
150+
151+
@SuppressWarnings("rawtypes") // ManagedChannelBuilder
152+
static final class GrpcMetricsChannelConfigurator
153+
implements ChannelConfigurator, Supplier<MeterProvider> {
154+
private final ChannelConfigurator delegate;
155+
private final MeterProvider meterProvider;
156+
157+
GrpcMetricsChannelConfigurator(ChannelConfigurator delegate, MeterProvider meterProvider) {
158+
this.delegate = delegate;
159+
this.meterProvider = meterProvider;
160+
}
161+
162+
@Override
163+
public ManagedChannelBuilder apply(ManagedChannelBuilder input) {
164+
return delegate.apply(input);
165+
}
166+
167+
@Override
168+
public MeterProvider get() {
169+
return meterProvider;
170+
}
132171
}
133172

134173
@SuppressWarnings("rawtypes") // ManagedChannelBuilder
@@ -168,13 +207,21 @@ public ManagedChannelBuilder apply(ManagedChannelBuilder input) {
168207
}
169208

170209
@VisibleForTesting
171-
static String getCloudMonitoringEndpoint(String endpoint, String universeDomain) {
210+
static String getCloudMonitoringEndpoint(
211+
@Nullable String endpoint, @Nullable String universeDomain) {
212+
if (endpoint == null || endpoint.isEmpty()) {
213+
return "monitoring.googleapis.com:443";
214+
}
172215
String metricServiceEndpoint = "monitoring.googleapis.com";
216+
HostAndPort parsed = parseHostAndPort(endpoint);
217+
String host = parsed != null ? parsed.getHost() : extractHostFallback(endpoint);
218+
int port = parsed != null ? parsed.getPortOrDefault(443) : 443;
173219

174-
// use contains instead of equals because endpoint has a port in it
175-
if (universeDomain != null && endpoint.contains("storage." + universeDomain)) {
220+
if (universeDomain != null
221+
&& (host.equals("storage." + universeDomain)
222+
|| host.equals("storage-direct." + universeDomain))) {
176223
metricServiceEndpoint = "monitoring." + universeDomain;
177-
} else if (!endpoint.contains("storage.googleapis.com")) {
224+
} else if (!host.equals("storage.googleapis.com") && !isDirectPathInterconnectHost(host)) {
178225
String canonicalEndpoint = "storage.googleapis.com";
179226
String privateEndpoint = "private.googleapis.com";
180227
String restrictedEndpoint = "restricted.googleapis.com";
@@ -183,35 +230,66 @@ static String getCloudMonitoringEndpoint(String endpoint, String universeDomain)
183230
privateEndpoint = "private." + universeDomain;
184231
restrictedEndpoint = "restricted." + universeDomain;
185232
}
233+
final String resolvedHost = host;
186234
String match =
187235
ImmutableList.of(canonicalEndpoint, privateEndpoint, restrictedEndpoint).stream()
188-
.filter(s -> endpoint.contains(s) || endpoint.contains("google-c2p:///" + s))
236+
.filter(s -> resolvedHost.contains(s) || endpoint.contains(C2P_PREFIX + s))
189237
.collect(Collectors.joining());
190238
if (!StringUtils.isNullOrEmpty(match)) {
191239
metricServiceEndpoint = match;
192240
}
193241
}
194-
HostAndPort parsed = parseHostAndPort(endpoint);
195-
int port = parsed != null ? parsed.getPortOrDefault(443) : 443;
196242
return metricServiceEndpoint + ":" + port;
197243
}
198244

245+
private static boolean isDirectPathInterconnectHost(String host) {
246+
return DIRECT_PATH_INTERCONNECT_HOST.equals(host)
247+
|| DIRECT_PATH_INTERCONNECT_PREPROD_HOST.equals(host);
248+
}
249+
250+
private static String extractHostFallback(String endpoint) {
251+
String s = stripEndpointScheme(endpoint.trim());
252+
int end = indexOfPathQueryOrFragment(s);
253+
String hostPort = end >= 0 ? s.substring(0, end) : s;
254+
int colon = hostPort.lastIndexOf(':');
255+
return colon >= 0 ? hostPort.substring(0, colon) : hostPort;
256+
}
257+
258+
private static String stripEndpointScheme(String s) {
259+
if (s.startsWith(C2P_PREFIX)) {
260+
return s.substring(C2P_PREFIX.length());
261+
}
262+
if (s.startsWith(C2P_EXPERIMENTAL_PREFIX)) {
263+
return s.substring(C2P_EXPERIMENTAL_PREFIX.length());
264+
}
265+
if (s.startsWith("https://")) {
266+
return s.substring("https://".length());
267+
}
268+
if (s.startsWith("http://")) {
269+
return s.substring("http://".length());
270+
}
271+
return s;
272+
}
273+
274+
private static int indexOfPathQueryOrFragment(String s) {
275+
for (int i = 0; i < s.length(); i++) {
276+
char c = s.charAt(i);
277+
if (c == '/' || c == '?' || c == '#') {
278+
return i;
279+
}
280+
}
281+
return -1;
282+
}
283+
199284
@Nullable
200285
private static HostAndPort parseHostAndPort(@Nullable String host) {
201286
if (host == null || host.trim().isEmpty()) {
202287
return null;
203288
}
204-
String s = host.trim();
205-
if (s.startsWith("google-c2p:///")) {
206-
s = s.substring("google-c2p:///".length());
207-
} else if (s.startsWith("https://")) {
208-
s = s.substring("https://".length());
209-
} else if (s.startsWith("http://")) {
210-
s = s.substring("http://".length());
211-
}
212-
int slashIdx = s.indexOf('/');
213-
if (slashIdx >= 0) {
214-
s = s.substring(0, slashIdx);
289+
String s = stripEndpointScheme(host.trim());
290+
int endIdx = indexOfPathQueryOrFragment(s);
291+
if (endIdx >= 0) {
292+
s = s.substring(0, endIdx);
215293
}
216294
if (s.isEmpty()) {
217295
return null;
@@ -415,6 +493,7 @@ static SdkMeterProvider createMeterProvider(
415493
"By",
416494
"A view of grpc/client/attempt/sent_total_compressed_message_size with histogram boundaries"
417495
+ " more appropriate for Google Cloud Storage RPCs");
496+
registerGrpcChannelViews(providerBuilder);
418497

419498
return buildMeterProvider(
420499
cloudMonitoringExporter,
@@ -569,6 +648,19 @@ private static SdkMeterProvider buildMeterProvider(
569648
return providerBuilder.build();
570649
}
571650

651+
@VisibleForTesting
652+
static SdkMeterProviderBuilder registerGrpcChannelViews(SdkMeterProviderBuilder providerBuilder) {
653+
addHistogramView(
654+
providerBuilder,
655+
latencyHistogramBoundaries(),
656+
CHANNEL_CREATION_LATENCY_INSTRUMENT,
657+
CHANNEL_CREATION_LATENCY_VIEW_NAME,
658+
StorageClientMetrics.METER_NAME,
659+
CHANNEL_CREATION_LATENCY_UNIT,
660+
CHANNEL_CREATION_LATENCY_DESCRIPTION);
661+
return providerBuilder;
662+
}
663+
572664
@VisibleForTesting
573665
static SdkMeterProviderBuilder registerClientViews(SdkMeterProviderBuilder providerBuilder) {
574666
for (String metric : CLIENT_COUNTERS) {

‎java-storage/google-cloud-storage/src/test/java/com/google/cloud/storage/OpenTelemetryBootstrappingUtilsTest.java‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,23 @@
2222
import static org.mockito.Mockito.mock;
2323

2424
import com.google.cloud.storage.OpenTelemetryBootstrappingUtils.ChannelConfigurator;
25+
import com.google.cloud.storage.OpenTelemetryBootstrappingUtils.GrpcMetricsChannelConfigurator;
2526
import io.grpc.ManagedChannelBuilder;
27+
import io.opentelemetry.api.common.AttributeKey;
2628
import io.opentelemetry.api.common.Attributes;
29+
import io.opentelemetry.api.metrics.DoubleHistogram;
30+
import io.opentelemetry.api.metrics.Meter;
31+
import io.opentelemetry.api.metrics.MeterProvider;
2732
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
2833
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
34+
import io.opentelemetry.sdk.metrics.data.HistogramPointData;
35+
import io.opentelemetry.sdk.metrics.data.MetricData;
36+
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
2937
import java.time.Duration;
38+
import java.util.Collection;
3039
import java.util.List;
3140
import java.util.concurrent.atomic.AtomicBoolean;
41+
import java.util.function.Supplier;
3242
import org.junit.Test;
3343

3444
public final class OpenTelemetryBootstrappingUtilsTest {
@@ -276,6 +286,103 @@ public void getCloudMonitoringEndpoint_extractsPortFromIpv4AndIpv6Endpoints() {
276286
OpenTelemetryBootstrappingUtils.getCloudMonitoringEndpoint(
277287
"storage.my-universe.com:8443", "my-universe.com"))
278288
.isEqualTo("monitoring.my-universe.com:8443");
289+
assertThat(
290+
OpenTelemetryBootstrappingUtils.getCloudMonitoringEndpoint(
291+
"storage.googleapis.com:443", null))
292+
.isEqualTo("monitoring.googleapis.com:443");
293+
assertThat(
294+
OpenTelemetryBootstrappingUtils.getCloudMonitoringEndpoint(
295+
"storage-direct.googleapis.com:443", null))
296+
.isEqualTo("monitoring.googleapis.com:443");
297+
assertThat(
298+
OpenTelemetryBootstrappingUtils.getCloudMonitoringEndpoint(
299+
"storage-preprod-direct.sandbox.googleapis.com:443", null))
300+
.isEqualTo("monitoring.googleapis.com:443");
301+
assertThat(
302+
OpenTelemetryBootstrappingUtils.getCloudMonitoringEndpoint(
303+
"storage-direct.apis-tpclp.goog:443", "apis-tpclp.goog"))
304+
.isEqualTo("monitoring.apis-tpclp.goog:443");
305+
assertThat(
306+
OpenTelemetryBootstrappingUtils.getCloudMonitoringEndpoint(
307+
"google-c2p:///storage-direct.googleapis.com?force-xds", null))
308+
.isEqualTo("monitoring.googleapis.com:443");
309+
assertThat(
310+
OpenTelemetryBootstrappingUtils.getCloudMonitoringEndpoint(
311+
"google-c2p-experimental:///storage-preprod-direct.sandbox.googleapis.com:443?force-xds",
312+
null))
313+
.isEqualTo("monitoring.googleapis.com:443");
314+
assertThat(OpenTelemetryBootstrappingUtils.getCloudMonitoringEndpoint(null, null))
315+
.isEqualTo("monitoring.googleapis.com:443");
316+
assertThat(OpenTelemetryBootstrappingUtils.getCloudMonitoringEndpoint("", null))
317+
.isEqualTo("monitoring.googleapis.com:443");
318+
}
319+
320+
@Test
321+
public void registerGrpcChannelViews_exportsChannelCreationLatencyHistogram() {
322+
InMemoryMetricReader metricReader = InMemoryMetricReader.create();
323+
SdkMeterProviderBuilder providerBuilder =
324+
SdkMeterProvider.builder().registerMetricReader(metricReader);
325+
SdkMeterProviderBuilder returned =
326+
OpenTelemetryBootstrappingUtils.registerGrpcChannelViews(providerBuilder);
327+
assertThat(returned).isSameInstanceAs(providerBuilder);
328+
329+
AttributeKey<String> transportTypeKey = AttributeKey.stringKey("transport_type");
330+
try (SdkMeterProvider meterProvider = providerBuilder.build()) {
331+
Meter meter = meterProvider.meterBuilder(StorageClientMetrics.METER_NAME).build();
332+
DoubleHistogram histogram =
333+
meter
334+
.histogramBuilder(OpenTelemetryBootstrappingUtils.CHANNEL_CREATION_LATENCY_INSTRUMENT)
335+
.setDescription(OpenTelemetryBootstrappingUtils.CHANNEL_CREATION_LATENCY_DESCRIPTION)
336+
.setUnit(OpenTelemetryBootstrappingUtils.CHANNEL_CREATION_LATENCY_UNIT)
337+
.build();
338+
histogram.record(0.125, Attributes.of(transportTypeKey, "DirectPathInterconnect"));
339+
histogram.record(0.050, Attributes.of(transportTypeKey, "DirectPath"));
340+
histogram.record(0.250, Attributes.of(transportTypeKey, "CloudPath"));
341+
342+
Collection<MetricData> metrics = metricReader.collectAllMetrics();
343+
assertThat(metrics).hasSize(1);
344+
MetricData metricData = metrics.iterator().next();
345+
assertThat(metricData.getName())
346+
.isEqualTo(OpenTelemetryBootstrappingUtils.CHANNEL_CREATION_LATENCY_VIEW_NAME);
347+
assertThat(metricData.getUnit())
348+
.isEqualTo(OpenTelemetryBootstrappingUtils.CHANNEL_CREATION_LATENCY_UNIT);
349+
assertThat(metricData.getDescription())
350+
.isEqualTo(OpenTelemetryBootstrappingUtils.CHANNEL_CREATION_LATENCY_DESCRIPTION);
351+
352+
Collection<HistogramPointData> points = metricData.getHistogramData().getPoints();
353+
assertThat(points).hasSize(3);
354+
for (HistogramPointData point : points) {
355+
String transportType = point.getAttributes().get(transportTypeKey);
356+
assertThat(point.getCount()).isEqualTo(1);
357+
if ("DirectPathInterconnect".equals(transportType)) {
358+
assertThat(point.getSum()).isWithin(1e-6).of(0.125);
359+
} else if ("DirectPath".equals(transportType)) {
360+
assertThat(point.getSum()).isWithin(1e-6).of(0.050);
361+
} else if ("CloudPath".equals(transportType)) {
362+
assertThat(point.getSum()).isWithin(1e-6).of(0.250);
363+
} else {
364+
throw new AssertionError("Unexpected transport_type: " + transportType);
365+
}
366+
}
367+
}
368+
}
369+
370+
@SuppressWarnings("rawtypes") // ManagedChannelBuilder
371+
@Test
372+
public void grpcMetricsChannelConfigurator_delegatesAndSuppliesMeterProvider() {
373+
ManagedChannelBuilder b1 = mock(ManagedChannelBuilder.class, "b1");
374+
ManagedChannelBuilder b2 = mock(ManagedChannelBuilder.class, "b2");
375+
ChannelConfigurator delegate =
376+
b -> {
377+
assertThat(b).isSameInstanceAs(b1);
378+
return b2;
379+
};
380+
try (SdkMeterProvider meterProvider = SdkMeterProvider.builder().build()) {
381+
GrpcMetricsChannelConfigurator configurator =
382+
new GrpcMetricsChannelConfigurator(delegate, meterProvider);
383+
assertThat(configurator.apply(b1)).isSameInstanceAs(b2);
384+
assertThat(((Supplier<MeterProvider>) configurator).get()).isSameInstanceAs(meterProvider);
385+
}
279386
}
280387

281388
@Test

0 commit comments

Comments
 (0)