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
5 changes: 5 additions & 0 deletions java-bigtable/google-cloud-bigtable/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
import com.google.api.core.InternalApi;
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.api.gax.core.NoCredentialsProvider;
Expand Down Expand Up @@ -88,6 +89,21 @@ private enum State {
Closed
}

@InternalApi
@Nullable
public static String getMonitoringEndpointOverride() {
return trimToNull(MONITORING_ENDPOINT_OVERRIDE_SYS_PROP);
}

@Nullable
private static String trimToNull(@Nullable String value) {
if (value == null) {
return null;
}
String trimmed = value.trim();
return trimmed.isEmpty() ? null : trimmed;
}

public static BigtableCloudMonitoringExporter create(
MetricRegistry metricRegistry,
Supplier<EnvInfo> envInfo,
Expand All @@ -107,15 +123,17 @@ public static BigtableCloudMonitoringExporter create(
.<CredentialsProvider>map(FixedCredentialsProvider::create)
.orElse(NoCredentialsProvider.create()));

if (MONITORING_ENDPOINT_OVERRIDE_SYS_PROP != null) {
String monitoringEndpointOverride = getMonitoringEndpointOverride();
if (monitoringEndpointOverride != null) {
LOGGER.warning(
"Setting the monitoring endpoint through system variable will be removed in future"
+ " versions");
settingsBuilder.setEndpoint(MONITORING_ENDPOINT_OVERRIDE_SYS_PROP);
settingsBuilder.setEndpoint(monitoringEndpointOverride);
}

if (endpoint != null) {
settingsBuilder.setEndpoint(endpoint);
String normalizedEndpoint = trimToNull(endpoint);
if (normalizedEndpoint != null) {
settingsBuilder.setEndpoint(normalizedEndpoint);
}

Duration timeout = Duration.ofMinutes(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.auth.Credentials;
import com.google.auth.oauth2.GdchCredentials;
import com.google.auth.oauth2.ServiceAccountJwtAccessCredentials;
import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
import com.google.cloud.bigtable.data.v2.internal.JwtCredentialsWithAudience;
import com.google.cloud.bigtable.data.v2.internal.api.InstanceName;
import com.google.cloud.bigtable.data.v2.internal.compat.DisabledShim;
Expand All @@ -37,6 +38,7 @@
import com.google.cloud.bigtable.data.v2.internal.csm.Metrics;
import com.google.cloud.bigtable.data.v2.internal.csm.MetricsImpl;
import com.google.cloud.bigtable.data.v2.internal.csm.attributes.ClientInfo;
import com.google.cloud.bigtable.data.v2.internal.csm.exporter.BigtableCloudMonitoringExporter;
import com.google.cloud.bigtable.data.v2.internal.dp.AlwaysEnabledDirectAccessChecker;
import com.google.cloud.bigtable.data.v2.internal.dp.ClassicDirectAccessChecker;
import com.google.cloud.bigtable.data.v2.internal.dp.DirectAccessChecker;
Expand All @@ -45,6 +47,8 @@
import com.google.cloud.bigtable.gaxx.grpc.BigtableTransportChannelProvider;
import com.google.cloud.bigtable.gaxx.grpc.ChannelPrimer;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Ascii;
import com.google.common.net.InternetDomainName;
import io.grpc.ManagedChannelBuilder;
import io.opencensus.stats.Stats;
import io.opencensus.stats.StatsRecorder;
Expand All @@ -55,9 +59,12 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -155,7 +162,7 @@ public static BigtableClientContext create(

@Nullable OpenTelemetrySdk builtinOtel = null;
try {
if (settings.areInternalMetricsEnabled()) {
if (shouldEnableBuiltinMetrics(settings, credentials)) {
builtinOtel =
builtinOtelFactory.create(
metricRegistry,
Expand Down Expand Up @@ -249,6 +256,128 @@ public static BigtableClientContext create(
}
}

@VisibleForTesting
static boolean shouldEnableBuiltinMetrics(
EnhancedBigtableStubSettings settings, @Nullable Credentials credentials) {
if (!settings.areInternalMetricsEnabled()) {
return false;
}

Optional<String> effectiveMetricsEndpoint =
Stream.of(
settings.getMetricsEndpoint(),
BigtableCloudMonitoringExporter.getMonitoringEndpointOverride())
.filter(s -> !isBlank(s))
.findFirst();
if (effectiveMetricsEndpoint.isPresent()) {
return true;
}

Optional<Credentials> effectiveMetricsCredentials =
Comment thread
igorbernstein2 marked this conversation as resolved.
Resolved
Stream.of(BigtableDataSettings.getMetricsCredentials(), credentials)
.filter(Objects::nonNull)
.findFirst();
if (!effectiveMetricsCredentials.isPresent()) {
logger.log(
Level.FINE,
"Disabling built-in Cloud Monitoring metrics because no credentials are configured.");
return false;
}

// settings.getEndpoint() reflects explicit endpoints on InstantiatingGrpcChannelProvider, but
// opaque providers (e.g. FixedTransportChannelProvider / prebuilt channels) hide the real
// target, so built-in metrics remain enabled in that case.
String endpoint = settings.getEndpoint();
Comment thread
igorbernstein2 marked this conversation as resolved.
Resolved
String universeDomain = settings.getUniverseDomain();
switch (classifyEndpoint(endpoint, universeDomain)) {
case SUPPORTED:
return true;
case UNSUPPORTED_IN_UNIVERSE:
logger.log(
Level.WARNING,
String.format(
"Disabling built-in Cloud Monitoring metrics because default Cloud Monitoring"
+ " export is not supported for endpoint '%s' (regional, named PSC, or"
+ " private/restricted VIP). To enable built-in metrics, explicitly set a"
+ " Cloud Monitoring endpoint via setMetricsEndpoint().",
endpoint));
return false;
case OUTSIDE_UNIVERSE:
default:
logger.log(
Level.WARNING,
String.format(
"Disabling built-in Cloud Monitoring metrics because endpoint '%s' is not in"
+ " universe domain '%s'. To enable built-in metrics, configure the universe"
+ " domain via setUniverseDomain() or GOOGLE_CLOUD_UNIVERSE_DOMAIN, or"
+ " explicitly set a Cloud Monitoring endpoint via setMetricsEndpoint().",
endpoint, universeDomain));
return false;
}
}

@VisibleForTesting
enum EndpointStatus {
// Valid port-443 subdomain of the configured universe; default Cloud Monitoring export is safe.
SUPPORTED,
// Different universe, custom proxy/emulator, non-443 port, or malformed endpoint.
OUTSIDE_UNIVERSE,
// Same universe, but REP (*.rep.*), named PSC (*.p.*), or private/restricted VIP with no
// derived monitoring endpoint.
UNSUPPORTED_IN_UNIVERSE
}

@VisibleForTesting
static EndpointStatus classifyEndpoint(
Comment thread
igorbernstein2 marked this conversation as resolved.
Resolved
@Nullable String endpoint, @Nullable String universeDomain) {
if (isBlank(endpoint) || isBlank(universeDomain)) {
return EndpointStatus.OUTSIDE_UNIVERSE;
}

// Deliberately do not trim endpoint (unlike metricsEndpoint): surrounding whitespace on the
// data endpoint is treated as malformed rather than normalized away.
String normalizedEndpoint = Ascii.toLowerCase(endpoint);
Comment thread
igorbernstein2 marked this conversation as resolved.
Resolved
if (normalizedEndpoint.endsWith(":443")) {
normalizedEndpoint = normalizedEndpoint.substring(0, normalizedEndpoint.length() - 4);
}
if (!InternetDomainName.isValid(normalizedEndpoint)) {
return EndpointStatus.OUTSIDE_UNIVERSE;
}

String universeSuffix = "." + Ascii.toLowerCase(universeDomain);
if (!normalizedEndpoint.endsWith(universeSuffix)) {
return EndpointStatus.OUTSIDE_UNIVERSE;
}

// Strip the universe suffix and optional ".mtls" segment so mTLS variants (e.g.
// *.rep.mtls.<universe>, *.p.mtls.<universe>, private.mtls.<universe>) are classified the same
// as their non-mTLS counterparts.
String hostPrefix =
normalizedEndpoint.substring(0, normalizedEndpoint.length() - universeSuffix.length());
if (hostPrefix.equals("mtls")) {
return EndpointStatus.OUTSIDE_UNIVERSE;
}
if (hostPrefix.endsWith(".mtls")) {
hostPrefix = hostPrefix.substring(0, hostPrefix.length() - ".mtls".length());
}

// Built-in Cloud Monitoring has no endpoint derivation for REP, named PSC, or
// Private/Restricted Google Access VIPs within the universe and would otherwise silently
// dial global monitoring.<universeDomain>:443.
if (hostPrefix.equals("private")
|| hostPrefix.equals("restricted")
|| hostPrefix.endsWith(".rep")
|| hostPrefix.endsWith(".p")) {
return EndpointStatus.UNSUPPORTED_IN_UNIVERSE;
}

return EndpointStatus.SUPPORTED;
Comment thread
igorbernstein2 marked this conversation as resolved.
Resolved
}

private static boolean isBlank(@Nullable String value) {
return value == null || value.trim().isEmpty();
}
Comment thread
igorbernstein2 marked this conversation as resolved.
Resolved

@SuppressWarnings("deprecation")
private static boolean isRefreshingEnabled(EnhancedBigtableStubSettings.Builder b) {
return b.isRefreshingChannel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.api.core.ApiFutures;
import com.google.api.gax.rpc.ApiCallContext;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.auth.Credentials;
import com.google.cloud.bigtable.data.v2.internal.api.TableName;
import com.google.cloud.bigtable.data.v2.internal.csm.MetricRegistry;
import com.google.cloud.bigtable.data.v2.internal.csm.attributes.ClientInfo;
Expand Down Expand Up @@ -51,6 +52,7 @@
import io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSumData;
import io.opentelemetry.sdk.resources.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -505,6 +507,28 @@ public void testExportingToMultipleProjects() throws InterruptedException {
assertThat(counts).containsExactly(3l, 15l);
}

@Test
public void testGetMonitoringEndpointOverrideUnsetReturnsNull() {
assertThat(BigtableCloudMonitoringExporter.getMonitoringEndpointOverride()).isNull();
}

@Test
public void testCreateIgnoresBlankEndpoint() throws IOException {
BigtableCloudMonitoringExporter created =
BigtableCloudMonitoringExporter.create(
new MetricRegistry(),
Suppliers.ofInstance(envInfo),
clientInfo,
null,
" ",
Credentials.GOOGLE_DEFAULT_UNIVERSE);
try {
assertThat(created).isNotNull();
} finally {
created.close();
}
}

private static class FakeMetricServiceClient extends MetricServiceClient {
protected FakeMetricServiceClient(MetricServiceStub stub) {
super(stub);
Expand Down
Loading
Loading