Skip to content

Commit 43a9767

Browse files
committed
add error messages
1 parent 4694a5b commit 43a9767

6 files changed

Lines changed: 53 additions & 12 deletions

File tree

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/agent/ConfigurationUpdater.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ private void handleProbesChanges(ConfigurationComparer changes, Configuration ne
183183
}
184184
List<Class<?>> changedClasses =
185185
finder.getAllLoadedChangedClasses(instrumentation.getAllLoadedClasses(), changes);
186-
changedClasses = detectMethodParameters(changedClasses);
186+
changedClasses = detectMethodParameters(changes, changedClasses);
187187
retransformClasses(changedClasses);
188188
// ensures that we have at least re-transformed 1 class
189189
if (changedClasses.size() > 0) {
@@ -197,7 +197,8 @@ private void handleProbesChanges(ConfigurationComparer changes, Configuration ne
197197
* Spring 6/Spring boot 3 rely exclusively on this attribute and may throw an exception
198198
* if no attribute found.
199199
*/
200-
private List<Class<?>> detectMethodParameters(List<Class<?>> changedClasses) {
200+
private List<Class<?>> detectMethodParameters(
201+
ConfigurationComparer changes, List<Class<?>> changedClasses) {
201202
if (JAVA_AT_LEAST_19) {
202203
// bug is fixed since JDK19, no need to perform detection
203204
return changedClasses;
@@ -220,6 +221,10 @@ private List<Class<?>> detectMethodParameters(List<Class<?>> changedClasses) {
220221
method.getName(),
221222
parameters[0].getName());
222223
// skip the class: compiled with -parameters
224+
reportError(
225+
changes,
226+
"Method Parameters detected, instrumentation not supported for "
227+
+ changedClass.getTypeName());
223228
addClass = false;
224229
}
225230
// we found at leat a method with one parameter if name is not present we can stop there
@@ -245,6 +250,16 @@ private void reportReceived(ConfigurationComparer changes) {
245250
}
246251
}
247252

253+
private void reportError(ConfigurationComparer changes, String errorMsg) {
254+
for (ProbeDefinition def : changes.getAddedDefinitions()) {
255+
if (def instanceof ExceptionProbe) {
256+
// do not report received for exception probes
257+
continue;
258+
}
259+
sink.addError(def.getProbeId(), errorMsg);
260+
}
261+
}
262+
248263
private void installNewDefinitions(Configuration newConfiguration) {
249264
DebuggerContext.initClassFilter(new DenyListHelper(newConfiguration.getDenyList()));
250265
if (appliedDefinitions.isEmpty()) {

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/agent/DebuggerTransformer.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
public class DebuggerTransformer implements ClassFileTransformer {
8383
private static final Logger LOGGER = LoggerFactory.getLogger(DebuggerTransformer.class);
8484
private static final String CANNOT_FIND_METHOD = "Cannot find method %s::%s%s";
85-
private static final String INSTRUMENTATION_FAILS = "Instrumentation fails for %s";
85+
private static final String INSTRUMENTATION_FAILS = "Instrumentation failed for %s: %s";
8686
private static final String CANNOT_FIND_LINE = "No executable code was found at %s:L%s";
8787
private static final Pattern COMMA_PATTERN = Pattern.compile(",");
8888
private static final List<Class<?>> PROBE_ORDER =
@@ -276,7 +276,7 @@ public byte[] transform(
276276
"type {} matched but no transformation for definitions: {}", classFilePath, definitions);
277277
} catch (Throwable ex) {
278278
LOGGER.warn("Cannot transform: ", ex);
279-
reportInstrumentationFails(definitions, fullyQualifiedClassName);
279+
reportInstrumentationFails(definitions, fullyQualifiedClassName, ex.toString());
280280
}
281281
return null;
282282
}
@@ -311,7 +311,7 @@ private void checkMethodParameters(ClassNode classNode) {
311311
}
312312
if (methodNode.parameters != null && !methodNode.parameters.isEmpty()) {
313313
throw new RuntimeException(
314-
"Method Parameters attribute detected, cannot instrument class " + classNode.name);
314+
"Method Parameters attribute detected, instrumentation not supported");
315315
} else {
316316
// we found at leat a method with one parameter if name is not present we can stop there
317317
break;
@@ -535,7 +535,7 @@ private byte[] writeClassFile(
535535
classNode.accept(visitor);
536536
} catch (Throwable t) {
537537
LOGGER.error("Cannot write classfile for class: {} Exception: ", classFilePath, t);
538-
reportInstrumentationFails(definitions, Strings.getClassName(classFilePath));
538+
reportInstrumentationFails(definitions, Strings.getClassName(classFilePath), t.toString());
539539
return null;
540540
}
541541
byte[] data = writer.toByteArray();
@@ -659,8 +659,9 @@ private void reportLocationNotFound(
659659
// on a separate class files because probe was set on an inner/top-level class
660660
}
661661

662-
private void reportInstrumentationFails(List<ProbeDefinition> definitions, String className) {
663-
String msg = String.format(INSTRUMENTATION_FAILS, className);
662+
private void reportInstrumentationFails(
663+
List<ProbeDefinition> definitions, String className, String errorMsg) {
664+
String msg = String.format(INSTRUMENTATION_FAILS, className, errorMsg);
664665
reportErrorForAllProbes(definitions, msg);
665666
}
666667

dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/sink/DebuggerSink.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ public void addBlocked(ProbeId probeId) {
208208
probeStatusSink.addBlocked(probeId);
209209
}
210210

211+
public void addError(ProbeId probeId, String msg) {
212+
probeStatusSink.addError(probeId, msg);
213+
}
214+
211215
public void removeDiagnostics(ProbeId probeId) {
212216
probeStatusSink.removeDiagnostics(probeId);
213217
}

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/CapturedSnapshotTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2969,7 +2969,9 @@ public void methodParametersAttribute() throws IOException, URISyntaxException {
29692969
ArgumentCaptor<String> strCaptor = ArgumentCaptor.forClass(String.class);
29702970
verify(probeStatusSink, times(1)).addError(probeIdCaptor.capture(), strCaptor.capture());
29712971
assertEquals(PROBE_ID.getId(), probeIdCaptor.getAllValues().get(0).getId());
2972-
assertEquals("Instrumentation fails for CapturedSnapshot01", strCaptor.getAllValues().get(0));
2972+
assertEquals(
2973+
"Instrumentation failed for CapturedSnapshot01: java.lang.RuntimeException: Method Parameters attribute detected, instrumentation not supported",
2974+
strCaptor.getAllValues().get(0));
29732975
}
29742976
}
29752977

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/ConfigurationUpdaterTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,13 @@ public void methodParametersAttribute()
651651
} else {
652652
verify(inst).getAllLoadedClasses();
653653
verify(inst, times(0)).retransformClasses(any());
654+
ArgumentCaptor<ProbeId> probeIdCaptor = ArgumentCaptor.forClass(ProbeId.class);
655+
ArgumentCaptor<String> strCaptor = ArgumentCaptor.forClass(String.class);
656+
verify(probeStatusSink, times(1)).addError(probeIdCaptor.capture(), strCaptor.capture());
657+
assertEquals(PROBE_ID.getId(), probeIdCaptor.getAllValues().get(0).getId());
658+
assertEquals(
659+
"Method Parameters detected, instrumentation not supported for CapturedSnapshot01",
660+
strCaptor.getAllValues().get(0));
654661
}
655662
}
656663

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/agent/DebuggerTransformerTest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,21 @@ public void classGenerationFailed() {
338338
assertEquals("logprobe1", probeIdCaptor.getAllValues().get(0).getId());
339339
assertEquals("logprobe2", probeIdCaptor.getAllValues().get(1).getId());
340340
assertEquals(PROBE_ID.getId(), probeIdCaptor.getAllValues().get(2).getId());
341-
assertEquals("Instrumentation fails for " + CLASS_NAME, strCaptor.getAllValues().get(0));
342-
assertEquals("Instrumentation fails for " + CLASS_NAME, strCaptor.getAllValues().get(1));
343-
assertEquals("Instrumentation fails for " + CLASS_NAME, strCaptor.getAllValues().get(2));
341+
assertEquals(
342+
"Instrumentation failed for "
343+
+ CLASS_NAME
344+
+ ": java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 0",
345+
strCaptor.getAllValues().get(0));
346+
assertEquals(
347+
"Instrumentation failed for "
348+
+ CLASS_NAME
349+
+ ": java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 0",
350+
strCaptor.getAllValues().get(1));
351+
assertEquals(
352+
"Instrumentation failed for "
353+
+ CLASS_NAME
354+
+ ": java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 0",
355+
strCaptor.getAllValues().get(2));
344356
}
345357

346358
@Test

0 commit comments

Comments
 (0)