Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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> {

Copy link
Copy Markdown
Contributor

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.andThen returns 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?

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
Expand Down Expand Up @@ -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)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isDirectPathInterconnectHost(host) does not change the result: neither host contains storage., private. or restricted. plus the domain, so the branch below leaves monitoring.googleapis.com in place.

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";
Expand All @@ -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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resolvedHost::contains can now match canonicalEndpoint, which was unreachable before the outer checks moved from contains to equals. For example, storage.googleapis.com.:443 (trailing dot) now returns storage.googleapis.com:443 instead of monitoring.googleapis.com:443.

Consider dropping canonicalEndpoint from this list, or matching the private and restricted hosts exactly.

.collect(Collectors.joining());
Comment thread
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) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extractHostFallback only runs when HostAndPort.fromString rejects the input, and the one test that reaches it ([::1]:invalid) expects the default endpoint whatever host comes back.

Please add a case with a host such as storage.my-universe.com:99999 with universe my-universe.com expecting monitoring.my-universe.com:443.

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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
Loading
Loading