Skip to content

feat: enable grpc configurator for client-side tracing #1886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
4 changes: 4 additions & 0 deletions google-cloud-datastore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@
<artifactId>opentelemetry-context</artifactId>
<version>${opentelemetry.version}</version>
</dependency>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-grpc-1.6</artifactId>
</dependency>
<!-- END OpenTelemetry -->

<!-- Test dependencies -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static com.google.cloud.datastore.Validator.validateNamespace;

import com.google.api.core.ApiFunction;
import com.google.api.core.BetaApi;
import com.google.api.gax.grpc.ChannelPoolSettings;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
Expand All @@ -35,6 +36,7 @@
import com.google.cloud.http.HttpTransportOptions;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableSet;
import io.grpc.ManagedChannelBuilder;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Objects;
Expand Down Expand Up @@ -222,20 +224,37 @@ private DatastoreOptions(Builder builder) {
throw new IllegalArgumentException(
"Only gRPC transport allows setting of channel provider or credentials provider");
} else if (getTransportOptions() instanceof GrpcTransportOptions) {
// For grpc transport options, configure default gRPC Connection pool with minChannelCount = 1
// and maxChannelCount = 4
this.channelProvider =
builder.channelProvider != null
? builder.channelProvider
: GrpcTransportOptions.setUpChannelProvider(
if (builder.channelProvider == null) {
/*
The default gRPC connection pool is configured with a minimum of 1 channel.
The maximum channel count automatically defaults to 200 (Defined in gax-grpc).
*/
ChannelPoolSettings datastoreChannelPoolSettings =
ChannelPoolSettings.builder()
.setInitialChannelCount(INIT_CHANNEL_COUNT)
.setMinChannelCount(MIN_CHANNEL_COUNT)
.build();

ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> channelConfigurator =
this.traceUtil.getChannelConfigurator();
if (channelConfigurator == null) {
this.channelProvider =
GrpcTransportOptions.setUpChannelProvider(
DatastoreSettings.defaultGrpcTransportProviderBuilder()
.setChannelPoolSettings(
ChannelPoolSettings.builder()
.setInitialChannelCount(INIT_CHANNEL_COUNT)
.setMinChannelCount(MIN_CHANNEL_COUNT)
.setMaxChannelCount(MAX_CHANNEL_COUNT)

Choose a reason for hiding this comment

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

Is there a reason to switch to the grpc default instead of explicitly setting MAX_CHANNEL_COUNT?

.build()),
.setChannelPoolSettings(datastoreChannelPoolSettings),
this);
} else {
// Intercept the grpc channel calls to add telemetry info.
this.channelProvider =
GrpcTransportOptions.setUpChannelProvider(
DatastoreSettings.defaultGrpcTransportProviderBuilder()
.setChannelPoolSettings(datastoreChannelPoolSettings)
.setChannelConfigurator(channelConfigurator),
this);
}
} else {
this.channelProvider = builder.channelProvider;
}
}
}

Expand All @@ -256,6 +275,7 @@ protected String getDefaultProject() {
}

private static class DatastoreDefaults implements ServiceDefaults<Datastore, DatastoreOptions> {

private final TransportOptions TRANSPORT_OPTIONS = getDefaultTransportOptionsBuilder().build();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public GrpcDatastoreRpc(DatastoreOptions datastoreOptions) throws IOException {
? getClientContextForEmulator(datastoreOptions)
: getClientContext(datastoreOptions);

/* For grpc transport options, configure default gRPC Connection pool with minChannelCount = 1 and maxChannelCount = 4 */
/* For grpc transport options, configure default gRPC Connection pool with minChannelCount = 1 */
DatastoreStubSettings datastoreStubSettings =
DatastoreStubSettings.newBuilder(clientContext)
.applyToAllUnaryMethods(retrySettingSetter(datastoreOptions))
Expand All @@ -85,7 +85,6 @@ public GrpcDatastoreRpc(DatastoreOptions datastoreOptions) throws IOException {
ChannelPoolSettings.builder()
.setInitialChannelCount(DatastoreOptions.INIT_CHANNEL_COUNT)
.setMinChannelCount(DatastoreOptions.MIN_CHANNEL_COUNT)
.setMaxChannelCount(DatastoreOptions.MAX_CHANNEL_COUNT)
.build())
.build())
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.TracerProvider;
import io.opentelemetry.instrumentation.grpc.v1_6.GrpcTelemetry;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -66,14 +68,30 @@ public OpenTelemetry getOpenTelemetry() {
return openTelemetry;
}

// The gRPC channel configurator that intercepts gRPC calls for tracing purposes.
public class OpenTelemetryGrpcChannelConfigurator
implements ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> {

@Override
public ManagedChannelBuilder apply(ManagedChannelBuilder managedChannelBuilder) {
GrpcTelemetry grpcTelemetry = GrpcTelemetry.create(getOpenTelemetry());
return managedChannelBuilder.intercept(grpcTelemetry.newClientInterceptor());
}
}

@Override
@Nullable
public ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> getChannelConfigurator() {
// TODO(jimit) Update this to return a gRPC Channel Configurator after gRPC upgrade.
return null;
// Note: using `==` rather than `.equals` since OpenTelemetry has only 1 static instance of
// `TracerProvider.noop`.
if (openTelemetry.getTracerProvider() == TracerProvider.noop()) {
return null;
}
return new OpenTelemetryGrpcChannelConfigurator();
}

static class Span implements TraceUtil.Span {

private final io.opentelemetry.api.trace.Span span;
private final String spanName;

Expand Down Expand Up @@ -198,6 +216,7 @@ public Scope makeCurrent() {
}

static class Scope implements TraceUtil.Scope {

private final io.opentelemetry.context.Scope scope;

Scope(io.opentelemetry.context.Scope scope) {
Expand All @@ -211,6 +230,7 @@ public void close() {
}

static class Context implements TraceUtil.Context {

private final io.opentelemetry.context.Context context;

Context(io.opentelemetry.context.Context context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class DatastoreOptionsTest {
private static final String PROJECT_ID = "project-id";
private static final String DATABASE_ID = "database-id";
private static final int PORT = 8080;
private static final int DEFAULT_MAX_CHANNEL_COUNT = 200;
private DatastoreRpcFactory datastoreRpcFactory;
private DatastoreRpc datastoreRpc;
private DatastoreOptions.Builder options;
Expand Down Expand Up @@ -119,7 +120,7 @@ public void testGrpcDefaultChannelConfigurations() {
.getChannelPoolSettings();
assertEquals(channelPoolSettings.getInitialChannelCount(), DatastoreOptions.INIT_CHANNEL_COUNT);
assertEquals(channelPoolSettings.getMinChannelCount(), DatastoreOptions.MIN_CHANNEL_COUNT);
assertEquals(channelPoolSettings.getMaxChannelCount(), DatastoreOptions.MAX_CHANNEL_COUNT);
assertEquals(channelPoolSettings.getMaxChannelCount(), DEFAULT_MAX_CHANNEL_COUNT);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ public void usesOpenTelemetryFromOptions() {
@Test
public void usesGlobalOpenTelemetryIfOpenTelemetryInstanceNotProvided() {
OpenTelemetrySdk ignored = OpenTelemetrySdk.builder().buildAndRegisterGlobal();
DatastoreOptions firestoreOptions =
DatastoreOptions datastoreOptions =
getBaseOptions()
.setOpenTelemetryOptions(
DatastoreOpenTelemetryOptions.newBuilder().setTracingEnabled(true).build())
.build();
EnabledTraceUtil traceUtil = new EnabledTraceUtil(firestoreOptions);
EnabledTraceUtil traceUtil = new EnabledTraceUtil(datastoreOptions);
assertThat(traceUtil.getOpenTelemetry()).isEqualTo(GlobalOpenTelemetry.get());
}

Expand All @@ -80,6 +80,34 @@ public void enabledTraceUtilProvidesChannelConfigurator() {
assertThat(newEnabledTraceUtil().getChannelConfigurator()).isNull();
}

@Test
public void openTelemetryInstanceRegistersGrpcChannelConfigurator() {
OpenTelemetrySdk myOpenTelemetrySdk = OpenTelemetrySdk.builder().build();
DatastoreOptions firestoreOptions =
getBaseOptions()
.setOpenTelemetryOptions(
DatastoreOpenTelemetryOptions.newBuilder()
.setTracingEnabled(true)
.setOpenTelemetry(myOpenTelemetrySdk)
.build())
.build();
EnabledTraceUtil traceUtil = new EnabledTraceUtil(firestoreOptions);
assertThat(traceUtil.getChannelConfigurator()).isNotNull();
}

@Test
public void globalOpenTelemetryRegistersGrpcChannelConfigurator() {

OpenTelemetrySdk.builder().buildAndRegisterGlobal();
DatastoreOptions datastoreOptions =
getBaseOptions()
.setOpenTelemetryOptions(
DatastoreOpenTelemetryOptions.newBuilder().setTracingEnabled(true).build())
.build();
EnabledTraceUtil traceUtil = new EnabledTraceUtil(datastoreOptions);
assertThat(traceUtil.getChannelConfigurator()).isNotNull();
}

@Test
public void usesEnabledContext() {
assertThat(newEnabledTraceUtil().getCurrentContext() instanceof EnabledTraceUtil.Context)
Expand Down