Skip to content

Commit

Permalink
fix NPE in other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
evanchooly committed Jan 23, 2025
1 parent ce49eb9 commit ec59f6f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ public String toString() {
private final Capture capture;
private final Sampling sampling;
private transient Consumer<Snapshot> snapshotProcessor;
private transient Map<DDTraceId, AtomicInteger> budget = new WeakIdentityHashMap<>();
protected transient Map<DDTraceId, AtomicInteger> budget = new WeakIdentityHashMap<>();

// no-arg constructor is required by Moshi to avoid creating instance with unsafe and by-passing
// constructors, including field initializers.
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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));
}

Expand All @@ -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,
Expand All @@ -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<Arguments> statusValues() {
Expand All @@ -260,15 +257,15 @@ 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));
}

@Test
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"));
Expand All @@ -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());
Expand All @@ -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)
Expand Down

0 comments on commit ec59f6f

Please sign in to comment.