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
30 changes: 30 additions & 0 deletions java-firestore/google-cloud-firestore/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,36 @@
</dependency>
</dependencies>
</profile>
<!--
Matrix-test profiles for opentelemetry-instrumentation-grpc-1.6.
GrpcTelemetry.newClientInterceptor() was renamed createClientInterceptor();
these profiles run tests under both APIs to verify the reflection-based fallback.
See https://github.com/googleapis/google-cloud-java/issues/13095
-->
<profile>
<id>otel-grpc-old</id>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-grpc-1.6</artifactId>
<version>2.1.0-alpha</version>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
<profile>
<id>otel-grpc-new</id>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-grpc-1.6</artifactId>
<version>2.25.0-alpha</version>
</dependency>
</dependencies>
</dependencyManagement>
</profile>
<profile>
<!-- JDK17 set up for tests in src/test-jdk17 -->
<id>java17-test</id>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.api.core.InternalApi;
import com.google.cloud.firestore.FirestoreOptions;
import com.google.common.base.Throwables;
import io.grpc.ClientInterceptor;
import io.grpc.ManagedChannelBuilder;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
Expand All @@ -34,6 +35,8 @@
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.TracerProvider;
import io.opentelemetry.instrumentation.grpc.v1_6.GrpcTelemetry;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -73,13 +76,44 @@ public OpenTelemetry getOpenTelemetry() {
return openTelemetry;
}

// See https://github.com/googleapis/google-cloud-java/issues/13095
private static final Method GRPC_CLIENT_INTERCEPTOR_METHOD = resolveGrpcClientInterceptorMethod();

private static Method resolveGrpcClientInterceptorMethod() {
try {
return GrpcTelemetry.class.getMethod("createClientInterceptor");
} catch (NoSuchMethodException ignored) {
// Fall back to the pre-2.25.0 method name.
}
try {
return GrpcTelemetry.class.getMethod("newClientInterceptor");
} catch (NoSuchMethodException e) {
throw new IllegalStateException(
"Neither GrpcTelemetry#createClientInterceptor nor GrpcTelemetry#newClientInterceptor"
+ " is available on the classpath. Incompatible"
+ " opentelemetry-instrumentation-grpc-1.6 version.",
e);
}
}

private static ClientInterceptor newGrpcClientInterceptor(GrpcTelemetry grpcTelemetry) {
try {
return (ClientInterceptor) GRPC_CLIENT_INTERCEPTOR_METHOD.invoke(grpcTelemetry);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e);
} catch (InvocationTargetException e) {
Throwables.throwIfUnchecked(e.getCause());
throw new RuntimeException(e.getCause());
}
}

// 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());
return managedChannelBuilder.intercept(newGrpcClientInterceptor(grpcTelemetry));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

import static com.google.common.truth.Truth.assertThat;

import com.google.api.core.ApiFunction;
import com.google.cloud.firestore.FirestoreOpenTelemetryOptions;
import com.google.cloud.firestore.FirestoreOptions;
import io.grpc.ManagedChannelBuilder;
import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.trace.TracerProvider;
import io.opentelemetry.sdk.OpenTelemetrySdk;
Expand Down Expand Up @@ -111,6 +113,28 @@ public void openTelemetryInstanceRegistersGrpcChannelConfigurator() {
assertThat(traceUtil.getChannelConfigurator()).isNotNull();
}

// Exercises the reflection fallback that resolves either
// GrpcTelemetry#createClientInterceptor (>= 2.25.0) or #newClientInterceptor (< 2.25.0).
// See https://github.com/googleapis/google-cloud-java/issues/13095
@Test
public void channelConfiguratorAppliesInterceptorAcrossOtelGrpcVersions() {
OpenTelemetrySdk myOpenTelemetrySdk = OpenTelemetrySdk.builder().build();
FirestoreOptions firestoreOptions =
getBaseOptions()
.setOpenTelemetryOptions(
FirestoreOpenTelemetryOptions.newBuilder()
.setOpenTelemetry(myOpenTelemetrySdk)
.build())
.build();
EnabledTraceUtil traceUtil = new EnabledTraceUtil(firestoreOptions);
ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> configurator =
traceUtil.getChannelConfigurator();
assertThat(configurator).isNotNull();

ManagedChannelBuilder<?> builder = ManagedChannelBuilder.forAddress("localhost", 9999);
assertThat(configurator.apply(builder)).isNotNull();
}

@Test
public void usesEnabledContext() {
assertThat(defaultEnabledTraceUtil().currentContext() instanceof EnabledTraceUtil.Context)
Expand Down
Loading