Skip to content

Commit db98e02

Browse files
committed
use reflection to read the message size
1 parent 0e0e2e1 commit db98e02

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

instrumentation/grpc-1.6/javaagent/build.gradle.kts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ muzzle {
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

instrumentation/grpc-1.6/library/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ val grpcVersion = "1.6.0"
77

88
dependencies {
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

instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/BodySizeUtil.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,42 @@
55

66
package 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

1011
final 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
}

0 commit comments

Comments
 (0)