Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class BuiltInMetricsConstant {
static final String ATTEMPT_COUNT_NAME = "attempt_count";
static final String EEF_FALLBACK_COUNT_NAME = "eef.fallback_count";
static final String EEF_CALL_STATUS_NAME = "eef.call_status";
static final String CHANNEL_COUNT_NAME = "channel_pool.channel_count";
static final String CHANNEL_LOAD_NAME = "channel_pool.channel_load";

public static final Set<String> SPANNER_METRICS =
ImmutableSet.of(
Expand Down Expand Up @@ -145,14 +147,34 @@ public class BuiltInMetricsConstant {
static final Set<String> GRPC_GCP_EEF_CALL_STATUS_ATTRIBUTES =
ImmutableSet.of(CHANNEL_NAME_KEY.getKey(), STATUS_CODE_KEY.getKey());

public static final AttributeKey<String> LANE_KEY = AttributeKey.stringKey("lane");
public static final String LANE_DIRECTPATH = "directpath";
public static final String LANE_CLOUDPATH = "cloudpath";
static final AttributeKey<String> POOL_MODE_KEY = AttributeKey.stringKey("pool_mode");
static final AttributeKey<String> STATE_KEY = AttributeKey.stringKey("state");
static final AttributeKey<String> POOL_INDEX_KEY = AttributeKey.stringKey("pool_index");

static final Set<String> GRPC_GCP_CHANNEL_COUNT_ATTRIBUTES =
ImmutableSet.of(
POOL_MODE_KEY.getKey(), STATE_KEY.getKey(), LANE_KEY.getKey(), POOL_INDEX_KEY.getKey());

static final Set<String> GRPC_GCP_CHANNEL_LOAD_ATTRIBUTES =
ImmutableSet.of(POOL_MODE_KEY.getKey(), LANE_KEY.getKey());

static final Map<String, Set<String>> GRPC_GCP_METRIC_ADDITIONAL_ATTRIBUTES =
ImmutableMap.<String, Set<String>>builder()
.put(EEF_FALLBACK_COUNT_NAME, GRPC_GCP_EEF_FALLBACK_COUNT_ATTRIBUTES)
.put(EEF_CALL_STATUS_NAME, GRPC_GCP_EEF_CALL_STATUS_ATTRIBUTES)
.put(CHANNEL_COUNT_NAME, GRPC_GCP_CHANNEL_COUNT_ATTRIBUTES)
.put(CHANNEL_LOAD_NAME, GRPC_GCP_CHANNEL_LOAD_ATTRIBUTES)
.build();

static final Map<String, Aggregation> GRPC_GCP_METRIC_AGGREGATIONS =
ImmutableMap.of(CHANNEL_LOAD_NAME, Aggregation.defaultAggregation());

static final Collection<String> GRPC_GCP_METRICS_TO_ENABLE =
ImmutableList.of(EEF_FALLBACK_COUNT_NAME, EEF_CALL_STATUS_NAME);
ImmutableList.of(
EEF_FALLBACK_COUNT_NAME, EEF_CALL_STATUS_NAME, CHANNEL_COUNT_NAME, CHANNEL_LOAD_NAME);

public static final String SPANNER_RESOURCE_TYPE = "spanner_instance_client";
public static final String UNDEFINED_PROJECT_ID = "undefined-project";
Expand Down Expand Up @@ -357,7 +379,7 @@ private static void defineGrpcGcpView(ImmutableMap.Builder<InstrumentSelector, V
View view =
View.builder()
.setName(BuiltInMetricsConstant.METER_NAME + '/' + metric.replace(".", "/"))
.setAggregation(Aggregation.sum())
.setAggregation(GRPC_GCP_METRIC_AGGREGATIONS.getOrDefault(metric, Aggregation.sum()))
.setAttributeFilter(attributesFilter)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ static GcpChannelPoolOptions mergeWithDefaultChannelPoolOptions(
private final RetrySettings retryAdministrativeRequestsSettings;
private final boolean trackTransactionStarter;
private final boolean enableGrpcGcpOtelMetrics;
private final boolean enableChannelPoolBuiltInMetrics;
private final BuiltInMetricsProvider builtInMetricsProvider = BuiltInMetricsProvider.INSTANCE;

/**
Expand Down Expand Up @@ -1031,6 +1032,7 @@ protected SpannerOptions(Builder builder) {
retryAdministrativeRequestsSettings = builder.retryAdministrativeRequestsSettings;
trackTransactionStarter = builder.trackTransactionStarter;
enableGrpcGcpOtelMetrics = builder.enableGrpcGcpOtelMetrics;
enableChannelPoolBuiltInMetrics = environment.isEnableChannelPoolBuiltInMetrics();
defaultQueryOptions = builder.defaultQueryOptions;
envQueryOptions = builder.getEnvironmentQueryOptions();
if (envQueryOptions.equals(QueryOptions.getDefaultInstance())) {
Expand Down Expand Up @@ -1163,6 +1165,10 @@ default boolean isEnableGrpcGcpOtelMetrics() {
return true;
}

default boolean isEnableChannelPoolBuiltInMetrics() {
return true;
}

default boolean isEnableEndToEndTracing() {
return false;
}
Expand Down Expand Up @@ -1218,6 +1224,8 @@ private static class SpannerEnvironmentImpl implements SpannerEnvironment {
"SPANNER_DISABLE_DIRECT_ACCESS_GRPC_BUILTIN_METRICS";
private static final String SPANNER_DISABLE_GRPC_GCP_OTEL_METRICS =
"SPANNER_DISABLE_GRPC_GCP_OTEL_METRICS";
private static final String SPANNER_DISABLE_CHANNEL_POOL_BUILTIN_METRICS =
"SPANNER_DISABLE_CHANNEL_POOL_BUILTIN_METRICS";
private static final String SPANNER_MONITORING_HOST = "SPANNER_MONITORING_HOST";

private SpannerEnvironmentImpl() {}
Expand Down Expand Up @@ -1279,6 +1287,11 @@ public boolean isEnableGrpcGcpOtelMetrics() {
return !Boolean.parseBoolean(System.getenv(SPANNER_DISABLE_GRPC_GCP_OTEL_METRICS));
}

@Override
public boolean isEnableChannelPoolBuiltInMetrics() {
return !Boolean.parseBoolean(System.getenv(SPANNER_DISABLE_CHANNEL_POOL_BUILTIN_METRICS));
}

@Override
public boolean isEnableEndToEndTracing() {
return Boolean.parseBoolean(System.getenv(SPANNER_ENABLE_END_TO_END_TRACING));
Expand Down Expand Up @@ -2837,6 +2850,11 @@ public boolean isGrpcGcpOtelMetricsEnabled() {
return enableGrpcGcpOtelMetrics;
}

@InternalApi
public boolean isEnableChannelPoolBuiltInMetrics() {
return enableChannelPoolBuiltInMetrics && isEnableBuiltInMetrics() && !usesNoCredentials();
}

public GcpManagedChannelOptions getGrpcGcpOptions() {
return grpcGcpOptions;
}
Expand Down
Loading
Loading