File tree Expand file tree Collapse file tree 3 files changed +34
-8
lines changed
src/main/java/io/opentelemetry/instrumentation/grpc/v1_6 Expand file tree Collapse file tree 3 files changed +34
-8
lines changed Original file line number Diff line number Diff line change 88 module.set(" grpc-core" )
99 versions.set(" [1.6.0,)" )
1010 assertInverse.set(true )
11- extraDependency(" io.grpc:grpc-protobuf:1.5.0" )
1211 }
1312}
1413
@@ -22,7 +21,7 @@ dependencies {
2221 testInstrumentation(project(" :instrumentation:netty:netty-4.1:javaagent" ))
2322
2423 testLibrary(" io.grpc:grpc-netty:$grpcVersion " )
25- library (" io.grpc:grpc-protobuf:$grpcVersion " )
24+ testLibrary (" io.grpc:grpc-protobuf:$grpcVersion " )
2625 testLibrary(" io.grpc:grpc-services:$grpcVersion " )
2726 testLibrary(" io.grpc:grpc-stub:$grpcVersion " )
2827
Original file line number Diff line number Diff line change @@ -7,9 +7,9 @@ val grpcVersion = "1.6.0"
77
88dependencies {
99 library(" io.grpc:grpc-core:$grpcVersion " )
10- library(" io.grpc:grpc-protobuf:$grpcVersion " )
1110
1211 testLibrary(" io.grpc:grpc-netty:$grpcVersion " )
12+ testLibrary(" io.grpc:grpc-protobuf:$grpcVersion " )
1313 testLibrary(" io.grpc:grpc-services:$grpcVersion " )
1414 testLibrary(" io.grpc:grpc-stub:$grpcVersion " )
1515
Original file line number Diff line number Diff line change 55
66package io .opentelemetry .instrumentation .grpc .v1_6 ;
77
8- import com .google .protobuf .MessageLite ;
8+ import java .lang .reflect .Method ;
9+ import javax .annotation .Nullable ;
910
1011final class BodySizeUtil {
12+ @ Nullable private static final Class <?> messageLiteClass = getMessageLiteClass ();
13+
14+ @ Nullable
15+ private static final Method serializedSizeMethod =
16+ messageLiteClass != null ? getSerializedSizeMethod (messageLiteClass ) : null ;
17+
18+ private static Class <?> getMessageLiteClass () {
19+ try {
20+ return Class .forName ("com.google.protobuf.MessageLite" );
21+ } catch (Exception ignore ) {
22+ return null ;
23+ }
24+ }
25+
26+ private static Method getSerializedSizeMethod (Class <?> clazz ) {
27+ try {
28+ return clazz .getMethod ("getSerializedSize" );
29+ } catch (NoSuchMethodException ignore ) {
30+ return null ;
31+ }
32+ }
1133
1234 static <T > Long getBodySize (T message ) {
13- if (message instanceof MessageLite ) {
14- return (long ) ((MessageLite ) message ).getSerializedSize ();
15- } else {
16- // Message is not a protobuf message
35+ if (messageLiteClass == null || serializedSizeMethod == null ) {
36+ return null ;
37+ }
38+ if (!messageLiteClass .isInstance (message )) {
39+ return null ;
40+ }
41+ try {
42+ return ((Integer ) serializedSizeMethod .invoke (message )).longValue ();
43+ } catch (Throwable ignore ) {
1744 return null ;
1845 }
1946 }
You can’t perform that action at this time.
0 commit comments