Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ public String toString() {
protected transient Map<DDTraceId, AtomicInteger> budget =
Collections.synchronizedMap(new WeakIdentityHashMap<>());
protected transient Sampler sampler;
protected transient Sampler errorSampler;

// no-arg constructor is required by Moshi to avoid creating instance with unsafe and by-passing
// constructors, including field initializers.
Expand Down Expand Up @@ -461,6 +462,7 @@ public void initSamplers() {
? ProbeRateLimiter.DEFAULT_SNAPSHOT_RATE
: ProbeRateLimiter.DEFAULT_LOG_RATE);
sampler = ProbeRateLimiter.createSampler(rate);
errorSampler = ProbeRateLimiter.createSampler(1.0); // errors are always sampled at 1/s rate
}

public List<CaptureExpression> getCaptureExpressions() {
Expand Down Expand Up @@ -565,9 +567,13 @@ private void sample(LogStatus logStatus, MethodLocation methodLocation) {
if (!MethodLocation.isSame(methodLocation, evaluateAt)) {
return;
}
// if condition has error and no capture Snapshot, the error is reported using errorSampler
// at 1/s rate instead of the log template one
Sampler localSampler =
logStatus.hasConditionErrors && !isCaptureSnapshot() ? errorSampler : sampler;
boolean sampled =
!logStatus.getDebugSessionStatus().isDisabled()
&& ProbeRateLimiter.tryProbe(sampler, isCaptureSnapshot());
&& ProbeRateLimiter.tryProbe(localSampler, isCaptureSnapshot());
logStatus.setSampled(sampled);
if (!sampled) {
DebuggerAgent.getSink()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.DoubleConsumer;
import java.util.stream.Collectors;
import org.jetbrains.kotlin.com.intellij.util.lang.JavaVersion;
import org.joor.Reflect;
Expand Down Expand Up @@ -1296,6 +1297,35 @@ public void nullCondition() throws IOException, URISyntaxException {
assertEquals("Cannot dereference field: fld", snapshot.getMessage());
}

@Test
public void nullConditionTemplateOnly() throws IOException, URISyntaxException {
final String CLASS_NAME = "CapturedSnapshot08";
LogProbe logProbes =
createProbeBuilder(PROBE_ID, CLASS_NAME, "doit", "int (java.lang.String)")
.when(
new ProbeCondition(
DSL.when(
DSL.eq(
DSL.getMember(
DSL.getMember(DSL.getMember(DSL.ref("nullTyped"), "fld"), "fld"),
"msg"),
DSL.value("hello"))),
"nullTyped.fld.fld.msg == 'hello'"))
.captureSnapshot(false)
.template("plain log", Collections.emptyList())
.build();
TestSnapshotListener listener = installProbes(logProbes);
Class<?> testClass = compileAndLoadClass(CLASS_NAME);
int result = Reflect.onClass(testClass).call("main", "1").get();
assertEquals(3, result);
Snapshot snapshot = assertOneSnapshot(listener);
assertEquals("Cannot dereference field: fld", snapshot.getMessage());
List<EvaluationError> evaluationErrors = snapshot.getEvaluationErrors();
assertEquals(1, evaluationErrors.size());
assertEquals("nullTyped.fld.fld", evaluationErrors.get(0).getExpr());
assertEquals("Cannot dereference field: fld", evaluationErrors.get(0).getMessage());
}

@Test
public void shortCircuitingCondition() throws IOException, URISyntaxException {
final String CLASS_NAME = "CapturedSnapshot08";
Expand Down Expand Up @@ -2676,19 +2706,47 @@ public void ensureCallingSamplingLineProbeCondition() throws IOException, URISyn
doSamplingTest(this::lineProbeCondition, 1, 1);
}

@Test
public void ensureCallingSamplingLogTemplateOnlyConditionError()
throws IOException, URISyntaxException {
doSamplingTest(this::nullConditionTemplateOnly, ProbeRateLimiter::setGlobalLogRate, 1, 0, 1);
}

private void doSamplingTest(TestMethod testRun, int expectedGlobalCount, int expectedProbeCount)
throws IOException, URISyntaxException {
doSamplingTest(
testRun,
ProbeRateLimiter::setGlobalSnapshotRate,
expectedGlobalCount,
expectedProbeCount,
0);
}

private void doSamplingTest(
TestMethod testRun,
DoubleConsumer globalRateSetter,
int expectedGlobalCount,
int expectedProbeCount,
int expectedErrorCount)
throws IOException, URISyntaxException {
MockSampler probeSampler = new MockSampler();
MockSampler errorSampler = new MockSampler();
MockSampler globalSampler = new MockSampler();
ProbeRateLimiter.setSamplerSupplier(rate -> rate < 101 ? probeSampler : globalSampler);
ProbeRateLimiter.setGlobalSnapshotRate(1000);
ProbeRateLimiter.setSamplerSupplier(
rate -> {
if (rate < 2) return errorSampler;
if (rate < 101) return probeSampler;
return globalSampler;
});
globalRateSetter.accept(1000);
try {
testRun.run();
} finally {
ProbeRateLimiter.setSamplerSupplier(null);
}
assertEquals(expectedGlobalCount, globalSampler.getCallCount());
assertEquals(expectedProbeCount, probeSampler.getCallCount());
assertEquals(expectedErrorCount, errorSampler.getCallCount());
}

@Test
Expand Down