From ec59f6f1489a0fa400ec8442f81038cc7f9b0b0b Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Thu, 23 Jan 2025 17:28:43 -0500 Subject: [PATCH] fix NPE in other tests --- .../com/datadog/debugger/probe/LogProbe.java | 5 ++- .../datadog/debugger/probe/LogProbeTest.java | 41 +++++++++---------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/LogProbe.java b/dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/LogProbe.java index fb463b6e508..055d02d0948 100644 --- a/dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/LogProbe.java +++ b/dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/probe/LogProbe.java @@ -283,7 +283,7 @@ public String toString() { private final Capture capture; private final Sampling sampling; private transient Consumer snapshotProcessor; - private transient Map budget = new WeakIdentityHashMap<>(); + protected transient Map budget = new WeakIdentityHashMap<>(); // no-arg constructor is required by Moshi to avoid creating instance with unsafe and by-passing // constructors, including field initializers. @@ -863,7 +863,8 @@ public String toString() { } private boolean inBudget() { - AgentSpan span = AgentTracer.activeSpan(); + TracerAPI tracer = AgentTracer.get(); + AgentSpan span = tracer != null ? tracer.activeSpan() : null; return span == null || budget .computeIfAbsent(span.getLocalRootSpan().getTraceId(), id -> new AtomicInteger()) diff --git a/dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/probe/LogProbeTest.java b/dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/probe/LogProbeTest.java index 21ba8bf519d..9c5d7830e98 100644 --- a/dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/probe/LogProbeTest.java +++ b/dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/probe/LogProbeTest.java @@ -43,18 +43,18 @@ public class LogProbeTest { @Test public void testCapture() { - LogProbe.Builder builder = createLog(null); + Builder builder = createLog(null); LogProbe snapshotProbe = builder.capture(1, 420, 255, 20).build(); - Assertions.assertEquals(1, snapshotProbe.getCapture().getMaxReferenceDepth()); - Assertions.assertEquals(420, snapshotProbe.getCapture().getMaxCollectionSize()); - Assertions.assertEquals(255, snapshotProbe.getCapture().getMaxLength()); + assertEquals(1, snapshotProbe.getCapture().getMaxReferenceDepth()); + assertEquals(420, snapshotProbe.getCapture().getMaxCollectionSize()); + assertEquals(255, snapshotProbe.getCapture().getMaxLength()); } @Test public void testSampling() { - LogProbe.Builder builder = createLog(null); + Builder builder = createLog(null); LogProbe snapshotProbe = builder.sampling(0.25).build(); - Assertions.assertEquals(0.25, snapshotProbe.getSampling().getEventsPerSecond(), 0.01); + assertEquals(0.25, snapshotProbe.getSampling().getEventsPerSecond(), 0.01); } @Test @@ -111,8 +111,6 @@ private void runTrace(TracerAPI tracer) { for (int i = 0; i < 20; i++) { logProbe.commit(entryContext, exitContext, emptyList()); } - tracer.startSpan("budget testing", "test span"); - tracer.activateNext(span); } } @@ -190,12 +188,11 @@ public void fillSnapshot_shouldSend(String methodLocation) { LogProbe logProbe = createLog(null).evaluateAt(MethodLocation.valueOf(methodLocation)).build(); CapturedContext entryContext = new CapturedContext(); CapturedContext exitContext = new CapturedContext(); - LogProbe.LogStatus logEntryStatus = - prepareContext(entryContext, logProbe, MethodLocation.ENTRY); + LogStatus logEntryStatus = prepareContext(entryContext, logProbe, MethodLocation.ENTRY); logEntryStatus.setSampled(true); // force sampled to avoid rate limiting executing tests! - LogProbe.LogStatus logExitStatus = prepareContext(exitContext, logProbe, MethodLocation.EXIT); + LogStatus logExitStatus = prepareContext(exitContext, logProbe, MethodLocation.EXIT); logExitStatus.setSampled(true); // force sampled to avoid rate limiting executing tests! - Snapshot snapshot = new Snapshot(Thread.currentThread(), logProbe, 10); + Snapshot snapshot = new Snapshot(currentThread(), logProbe, 10); assertTrue(logProbe.fillSnapshot(entryContext, exitContext, null, snapshot)); } @@ -210,16 +207,16 @@ public void fillSnapshot( LogProbe logProbe = createLog(null).evaluateAt(MethodLocation.EXIT).build(); CapturedContext entryContext = new CapturedContext(); CapturedContext exitContext = new CapturedContext(); - LogProbe.LogStatus entryStatus = prepareContext(entryContext, logProbe, MethodLocation.ENTRY); + LogStatus entryStatus = prepareContext(entryContext, logProbe, MethodLocation.ENTRY); fillStatus(entryStatus, sampled, condition, conditionErrors, logTemplateErrors); - LogProbe.LogStatus exitStatus = prepareContext(exitContext, logProbe, MethodLocation.EXIT); + LogStatus exitStatus = prepareContext(exitContext, logProbe, MethodLocation.EXIT); fillStatus(exitStatus, sampled, condition, conditionErrors, logTemplateErrors); - Snapshot snapshot = new Snapshot(Thread.currentThread(), logProbe, 10); + Snapshot snapshot = new Snapshot(currentThread(), logProbe, 10); assertEquals(shouldCommit, logProbe.fillSnapshot(entryContext, exitContext, null, snapshot)); } private void fillStatus( - LogProbe.LogStatus entryStatus, + LogStatus entryStatus, boolean sampled, boolean condition, boolean conditionErrors, @@ -231,10 +228,10 @@ private void fillStatus( entryStatus.setLogTemplateErrors(logTemplateErrors); } - private LogProbe.LogStatus prepareContext( + private LogStatus prepareContext( CapturedContext context, LogProbe logProbe, MethodLocation methodLocation) { context.evaluate(PROBE_ID.getEncodedId(), logProbe, "", 0, methodLocation); - return (LogProbe.LogStatus) context.getStatus(PROBE_ID.getEncodedId()); + return (LogStatus) context.getStatus(PROBE_ID.getEncodedId()); } private static Stream statusValues() { @@ -260,7 +257,7 @@ public void fillSnapshot_shouldSend_exit() { prepareContext(entryContext, logProbe, MethodLocation.ENTRY); CapturedContext exitContext = new CapturedContext(); prepareContext(exitContext, logProbe, MethodLocation.EXIT); - Snapshot snapshot = new Snapshot(Thread.currentThread(), logProbe, 10); + Snapshot snapshot = new Snapshot(currentThread(), logProbe, 10); assertTrue(logProbe.fillSnapshot(entryContext, exitContext, null, snapshot)); } @@ -268,7 +265,7 @@ public void fillSnapshot_shouldSend_exit() { public void fillSnapshot_shouldSend_evalErrors() { LogProbe logProbe = createLog(null).evaluateAt(MethodLocation.EXIT).build(); CapturedContext entryContext = new CapturedContext(); - LogProbe.LogStatus logStatus = prepareContext(entryContext, logProbe, MethodLocation.ENTRY); + LogStatus logStatus = prepareContext(entryContext, logProbe, MethodLocation.ENTRY); logStatus.addError(new EvaluationError("expr", "msg1")); logStatus.setLogTemplateErrors(true); entryContext.addThrowable(new RuntimeException("errorEntry")); @@ -277,7 +274,7 @@ public void fillSnapshot_shouldSend_evalErrors() { logStatus.addError(new EvaluationError("expr", "msg2")); logStatus.setLogTemplateErrors(true); exitContext.addThrowable(new RuntimeException("errorExit")); - Snapshot snapshot = new Snapshot(Thread.currentThread(), logProbe, 10); + Snapshot snapshot = new Snapshot(currentThread(), logProbe, 10); assertTrue(logProbe.fillSnapshot(entryContext, exitContext, null, snapshot)); assertEquals(2, snapshot.getEvaluationErrors().size()); assertEquals("msg1", snapshot.getEvaluationErrors().get(0).getMessage()); @@ -288,7 +285,7 @@ public void fillSnapshot_shouldSend_evalErrors() { "errorExit", snapshot.getCaptures().getReturn().getCapturedThrowable().getMessage()); } - private LogProbe.Builder createLog(String template) { + private Builder createLog(String template) { return LogProbe.builder() .language(LANGUAGE) .probeId(PROBE_ID)