|
23 | 23 | import com.google.api.core.InternalApi; |
24 | 24 | import com.google.cloud.firestore.FirestoreOptions; |
25 | 25 | import com.google.common.base.Throwables; |
| 26 | +import io.grpc.ClientInterceptor; |
26 | 27 | import io.grpc.ManagedChannelBuilder; |
27 | 28 | import io.opentelemetry.api.GlobalOpenTelemetry; |
28 | 29 | import io.opentelemetry.api.OpenTelemetry; |
|
34 | 35 | import io.opentelemetry.api.trace.Tracer; |
35 | 36 | import io.opentelemetry.api.trace.TracerProvider; |
36 | 37 | import io.opentelemetry.instrumentation.grpc.v1_6.GrpcTelemetry; |
| 38 | +import java.lang.reflect.InvocationTargetException; |
| 39 | +import java.lang.reflect.Method; |
37 | 40 | import java.util.Map; |
38 | 41 | import javax.annotation.Nonnull; |
39 | 42 | import javax.annotation.Nullable; |
@@ -73,13 +76,44 @@ public OpenTelemetry getOpenTelemetry() { |
73 | 76 | return openTelemetry; |
74 | 77 | } |
75 | 78 |
|
| 79 | + // See https://github.com/googleapis/google-cloud-java/issues/13095 |
| 80 | + private static final Method GRPC_CLIENT_INTERCEPTOR_METHOD = resolveGrpcClientInterceptorMethod(); |
| 81 | + |
| 82 | + private static Method resolveGrpcClientInterceptorMethod() { |
| 83 | + try { |
| 84 | + return GrpcTelemetry.class.getMethod("createClientInterceptor"); |
| 85 | + } catch (NoSuchMethodException ignored) { |
| 86 | + // Fall back to the pre-2.25.0 method name. |
| 87 | + } |
| 88 | + try { |
| 89 | + return GrpcTelemetry.class.getMethod("newClientInterceptor"); |
| 90 | + } catch (NoSuchMethodException e) { |
| 91 | + throw new IllegalStateException( |
| 92 | + "Neither GrpcTelemetry#createClientInterceptor nor GrpcTelemetry#newClientInterceptor" |
| 93 | + + " is available on the classpath. Incompatible" |
| 94 | + + " opentelemetry-instrumentation-grpc-1.6 version.", |
| 95 | + e); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + private static ClientInterceptor newGrpcClientInterceptor(GrpcTelemetry grpcTelemetry) { |
| 100 | + try { |
| 101 | + return (ClientInterceptor) GRPC_CLIENT_INTERCEPTOR_METHOD.invoke(grpcTelemetry); |
| 102 | + } catch (IllegalAccessException e) { |
| 103 | + throw new IllegalStateException(e); |
| 104 | + } catch (InvocationTargetException e) { |
| 105 | + Throwables.throwIfUnchecked(e.getCause()); |
| 106 | + throw new RuntimeException(e.getCause()); |
| 107 | + } |
| 108 | + } |
| 109 | + |
76 | 110 | // The gRPC channel configurator that intercepts gRPC calls for tracing purposes. |
77 | 111 | public class OpenTelemetryGrpcChannelConfigurator |
78 | 112 | implements ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder> { |
79 | 113 | @Override |
80 | 114 | public ManagedChannelBuilder apply(ManagedChannelBuilder managedChannelBuilder) { |
81 | 115 | GrpcTelemetry grpcTelemetry = GrpcTelemetry.create(getOpenTelemetry()); |
82 | | - return managedChannelBuilder.intercept(grpcTelemetry.newClientInterceptor()); |
| 116 | + return managedChannelBuilder.intercept(newGrpcClientInterceptor(grpcTelemetry)); |
83 | 117 | } |
84 | 118 | } |
85 | 119 |
|
|
0 commit comments