Skip to content

Commit 6e5ce7c

Browse files
committed
add a null check
1 parent 8baf39a commit 6e5ce7c

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

dd-java-agent/instrumentation/grpc-1.5/src/main/java/datadog/trace/instrumentation/grpc/server/MethodHandlersInstrumentation.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,30 @@ public static class BuildAdvice {
4545

4646
@Advice.OnMethodEnter(suppress = Throwable.class)
4747
public static void onEnter(@Advice.Argument(0) Object serviceImpl) {
48-
try {
49-
Class<?> serviceClass = serviceImpl.getClass();
50-
Class<?> superclass = serviceClass.getSuperclass();
51-
Class<?> enclosingClass = superclass.getEnclosingClass();
52-
Field serviceNameField = enclosingClass.getDeclaredField("SERVICE_NAME");
53-
String serviceName = (String) serviceNameField.get(enclosingClass);
54-
for (Method method : superclass.getDeclaredMethods()) {
55-
try {
56-
Method declaredMethod =
57-
serviceClass.getDeclaredMethod(method.getName(), method.getParameterTypes());
58-
CodeOriginInfo.entry(
59-
String.format("%s/%s", serviceName, method.getName()),
60-
serviceClass,
61-
declaredMethod.getName(),
62-
declaredMethod.getParameterTypes());
63-
} catch (NoSuchMethodException e) {
64-
// service method not override on the impl. skipping instrumentation
48+
Class<?> serviceClass = serviceImpl.getClass();
49+
Class<?> superclass = serviceClass.getSuperclass();
50+
if (superclass != null) {
51+
try {
52+
Class<?> enclosingClass = superclass.getEnclosingClass();
53+
Field serviceNameField = enclosingClass.getDeclaredField("SERVICE_NAME");
54+
String serviceName = (String) serviceNameField.get(enclosingClass);
55+
for (Method method : superclass.getDeclaredMethods()) {
56+
try {
57+
Method declaredMethod =
58+
serviceClass.getDeclaredMethod(method.getName(), method.getParameterTypes());
59+
CodeOriginInfo.entry(
60+
String.format("%s/%s", serviceName, method.getName()),
61+
serviceClass,
62+
declaredMethod.getName(),
63+
declaredMethod.getParameterTypes());
64+
} catch (NoSuchMethodException e) {
65+
// service method not override on the impl. skipping instrumentation
66+
}
6567
}
68+
} catch (ReflectiveOperationException e) {
69+
// need to find a way to log this
70+
// LOG.debug("Member not found. Not instrumenting {}", serviceClass.getName(), e);
6671
}
67-
} catch (ReflectiveOperationException e) {
68-
// need to find a way to log this
69-
// LOG.debug("Member not found. Not instrumenting {}", serviceClass.getName(), e);
7072
}
7173
}
7274
}

dd-java-agent/instrumentation/grpc-1.5/src/test/groovy/GrpcCodeOriginTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ abstract class GrpcCodeOriginTest extends VersionedNamingTestBase {
222222
codeOrigin(format(DDTags.DD_CODE_ORIGIN_FRAME, 0, "signature"))
223223

224224
for (i in 0..<8) {
225-
for (codeOrigin in ["file", "line", "method", "type"]) {
226-
codeOrigin(format(DDTags.DD_CODE_ORIGIN_FRAME, i, codeOrigin))
225+
for (label in ["file", "line", "method", "type"]) {
226+
codeOrigin(format(DDTags.DD_CODE_ORIGIN_FRAME, i, label))
227227
}
228228
}
229229
defaultTags(true)

0 commit comments

Comments
 (0)