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 @@ -41,6 +41,7 @@ public class BuiltInMetricsConstant {

public static final String METER_NAME = "spanner.googleapis.com/internal/client";
public static final String GAX_METER_NAME = OpenTelemetryMetricsRecorder.GAX_METER_NAME;
public static final String GRPC_GCP_METER_NAME = "grpc-gcp";
static final String SPANNER_METER_NAME = "spanner-java";
static final String GRPC_METER_NAME = "grpc-java";
static final String GFE_LATENCIES_NAME = "gfe_latencies";
Expand All @@ -53,6 +54,8 @@ public class BuiltInMetricsConstant {
static final String ATTEMPT_LATENCY_NAME = "attempt_latency";
static final String OPERATION_COUNT_NAME = "operation_count";
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";

public static final Set<String> SPANNER_METRICS =
ImmutableSet.of(
Expand Down Expand Up @@ -117,6 +120,29 @@ public class BuiltInMetricsConstant {
"grpc.xds_client.resource_updates_invalid",
"grpc.xds_client.resource_updates_valid");

public static final AttributeKey<String> CHANNEL_NAME_KEY =
AttributeKey.stringKey("channel_name");
public static final AttributeKey<String> FROM_CHANNEL_NAME_KEY =
AttributeKey.stringKey("from_channel_name");
public static final AttributeKey<String> TO_CHANNEL_NAME_KEY =
AttributeKey.stringKey("to_channel_name");
public static final AttributeKey<String> STATUS_CODE_KEY = AttributeKey.stringKey("status_code");

static final Set<String> GRPC_GCP_EEF_FALLBACK_COUNT_ATTRIBUTES =
ImmutableSet.of(FROM_CHANNEL_NAME_KEY.getKey(), TO_CHANNEL_NAME_KEY.getKey());

static final Set<String> GRPC_GCP_EEF_CALL_STATUS_ATTRIBUTES =
ImmutableSet.of(CHANNEL_NAME_KEY.getKey(), STATUS_CODE_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)
.build();

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

public static final String SPANNER_RESOURCE_TYPE = "spanner_instance_client";

public static final AttributeKey<String> PROJECT_ID_KEY = AttributeKey.stringKey("project_id");
Expand Down Expand Up @@ -215,6 +241,7 @@ static Map<InstrumentSelector, View> getAllViews() {
"1");
defineSpannerView(views);
defineGRPCView(views);
defineGrpcGcpView(views);
return views.build();
}

Expand Down Expand Up @@ -281,4 +308,31 @@ private static void defineGRPCView(ImmutableMap.Builder<InstrumentSelector, View
viewMap.put(selector, view);
}
}

private static void defineGrpcGcpView(ImmutableMap.Builder<InstrumentSelector, View> viewMap) {
for (String metric : GRPC_GCP_METRICS_TO_ENABLE) {
InstrumentSelector selector =
InstrumentSelector.builder()
.setName(metric)
.setMeterName(BuiltInMetricsConstant.GRPC_GCP_METER_NAME)
.build();

Set<String> attributesFilter =
BuiltInMetricsConstant.COMMON_ATTRIBUTES.stream()
.map(AttributeKey::getKey)
.collect(Collectors.toSet());

attributesFilter.addAll(
GRPC_GCP_METRIC_ADDITIONAL_ATTRIBUTES.getOrDefault(metric, ImmutableSet.of()));

View view =
View.builder()
.setName(BuiltInMetricsConstant.METER_NAME + '/' + metric.replace(".", "/"))
.setAggregation(Aggregation.sum())
.setAttributeFilter(attributesFilter)
.build();

viewMap.put(selector, view);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static com.google.api.MetricDescriptor.ValueType.INT64;
import static com.google.cloud.spanner.BuiltInMetricsConstant.ALLOWED_EXEMPLARS_ATTRIBUTES;
import static com.google.cloud.spanner.BuiltInMetricsConstant.GAX_METER_NAME;
import static com.google.cloud.spanner.BuiltInMetricsConstant.GRPC_GCP_METER_NAME;
import static com.google.cloud.spanner.BuiltInMetricsConstant.GRPC_METER_NAME;
import static com.google.cloud.spanner.BuiltInMetricsConstant.PROJECT_ID_KEY;
import static com.google.cloud.spanner.BuiltInMetricsConstant.SPANNER_METER_NAME;
Expand Down Expand Up @@ -86,7 +87,8 @@ static List<TimeSeries> convertToSpannerTimeSeries(
// Get metrics data from GAX library, GRPC library and Spanner library
if (!(metricData.getInstrumentationScopeInfo().getName().equals(GAX_METER_NAME)
|| metricData.getInstrumentationScopeInfo().getName().equals(SPANNER_METER_NAME)
|| metricData.getInstrumentationScopeInfo().getName().equals(GRPC_METER_NAME))) {
|| metricData.getInstrumentationScopeInfo().getName().equals(GRPC_METER_NAME)
|| metricData.getInstrumentationScopeInfo().getName().equals(GRPC_GCP_METER_NAME))) {
Comment on lines 88 to +91
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The chain of || conditions is getting long and can be hard to read. To improve readability and maintainability, consider using a Set of allowed meter names for this check.

You could define a public static Set<String> in BuiltInMetricsConstant.java:

// In BuiltInMetricsConstant.java
public static final String SPANNER_METER_NAME = "spanner-java"; // Make public
public static final String GRPC_METER_NAME = "grpc-java"; // Make public

public static final java.util.Set<String> ALLOWED_BUILT_IN_METRICS_METERS =
    com.google.common.collect.ImmutableSet.of(
        GAX_METER_NAME, SPANNER_METER_NAME, GRPC_METER_NAME, GRPC_GCP_METER_NAME);

Then, you can simplify this check to:

if (!BuiltInMetricsConstant.ALLOWED_BUILT_IN_METRICS_METERS.contains(
    metricData.getInstrumentationScopeInfo().getName())) {
  // ...
}

// Filter out metric data for instruments that are not part of the spanner metrics list
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2380,6 +2380,13 @@ public ApiTracerFactory getApiTracerFactory() {
return createApiTracerFactory(false, false);
}

/** Returns the internal OpenTelemetry instance used for built-in metrics. */
@InternalApi
public OpenTelemetry getBuiltInOpenTelemetry() {
return this.builtInMetricsProvider.getOrCreateOpenTelemetry(
this.getProjectId(), getCredentials(), this.monitoringHost, getUniverseDomain());
}

public void enablegRPCMetrics(InstantiatingGrpcChannelProvider.Builder channelProviderBuilder) {
if (SpannerOptions.environment.isEnableGRPCBuiltInMetrics()) {
this.builtInMetricsProvider.enableGrpcMetrics(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
import io.grpc.ManagedChannelBuilder;
import io.grpc.MethodDescriptor;
import io.grpc.auth.MoreCallCredentials;
import io.opentelemetry.api.OpenTelemetry;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
Expand Down Expand Up @@ -567,6 +568,17 @@ GcpFallbackChannelOptions createFallbackChannelOptions(
.build();
}

@VisibleForTesting
OpenTelemetry getFallbackOpenTelemetry(SpannerOptions options) {
if (options.isEnableBuiltInMetrics()) {
OpenTelemetry builtInOtel = options.getBuiltInOpenTelemetry();
if (builtInOtel != null) {
return builtInOtel;
}
}
return OpenTelemetry.noop();
}

private static KeyAwareChannel extractKeyAwareChannel(TransportChannel transportChannel) {
if (transportChannel instanceof GrpcTransportChannel) {
Channel channel = ((GrpcTransportChannel) transportChannel).getChannel();
Expand Down Expand Up @@ -671,7 +683,7 @@ public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(

GcpFallbackOpenTelemetry fallbackTelemetry =
GcpFallbackOpenTelemetry.newBuilder()
.withSdk(options.getOpenTelemetry())
.withSdk(getFallbackOpenTelemetry(options))
.disableAllMetrics()
.enableMetrics(Arrays.asList("fallback_count", "call_status"))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,11 @@ public TestableGapicSpannerRpc(SpannerOptions options) {
super(options);
}

@Override
OpenTelemetry getFallbackOpenTelemetry(SpannerOptions options) {
return options.getOpenTelemetry();
}

@Override
GcpFallbackChannelOptions createFallbackChannelOptions(
GcpFallbackOpenTelemetry fallbackTelemetry, int minFailedCalls) {
Expand Down Expand Up @@ -1222,6 +1227,11 @@ public TestableGapicSpannerRpcWithLowerMinFailedCalls(SpannerOptions options) {
super(options);
}

@Override
OpenTelemetry getFallbackOpenTelemetry(SpannerOptions options) {
return options.getOpenTelemetry();
}

@Override
GcpFallbackChannelOptions createFallbackChannelOptions(
GcpFallbackOpenTelemetry fallbackTelemetry, int minFailedCalls) {
Expand Down
Loading