-
Notifications
You must be signed in to change notification settings - Fork 342
Add evalution timeout for expressions #11741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
gh-worker-dd-mergequeue-cf854d
merged 5 commits into
master
from
jpbempel/cond-eval-timeout
Jul 2, 2026
+1,519
−665
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
.../debugger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/el/DebuggerScript.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| package datadog.trace.bootstrap.debugger.el; | ||
|
|
||
| import datadog.trace.bootstrap.debugger.util.TimeoutChecker; | ||
|
|
||
| /** | ||
| * A debugger EL script interface used for communication between the instrumented code and the | ||
| * debugger EL.<br> | ||
| * Because it must be reachable from the instrumented code it must be placed in bootstrap. | ||
| */ | ||
| public interface DebuggerScript<R> { | ||
| R execute(ValueReferenceResolver valueRefResolver); | ||
| R execute(ValueReferenceResolver valueRefResolver, TimeoutChecker timeoutChecker); | ||
| } |
26 changes: 26 additions & 0 deletions
26
...gger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/util/CpuTimeoutChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package datadog.trace.bootstrap.debugger.util; | ||
|
|
||
| import java.lang.management.ManagementFactory; | ||
| import java.lang.management.ThreadMXBean; | ||
| import java.time.Duration; | ||
|
|
||
| public class CpuTimeoutChecker implements TimeoutChecker { | ||
| private final Duration timeOut; | ||
| private final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); | ||
| private final long startCpuTime; | ||
|
|
||
| public CpuTimeoutChecker(Duration timeout) { | ||
| this.timeOut = timeout; | ||
| startCpuTime = threadMXBean.getCurrentThreadCpuTime(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isTimedOut() { | ||
| return threadMXBean.getCurrentThreadCpuTime() - startCpuTime >= timeOut.toNanos(); | ||
| } | ||
|
|
||
| @Override | ||
| public Duration getTimeOut() { | ||
| return timeOut; | ||
| } | ||
| } |
34 changes: 11 additions & 23 deletions
34
...ebugger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/util/TimeoutChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,21 @@ | ||
| package datadog.trace.bootstrap.debugger.util; | ||
|
|
||
| import datadog.trace.api.Config; | ||
| import java.time.Duration; | ||
| import java.time.temporal.ChronoUnit; | ||
|
|
||
| public class TimeoutChecker { | ||
| public static final Duration DEFAULT_TIME_OUT = Duration.of(100, ChronoUnit.MILLIS); | ||
| public interface TimeoutChecker { | ||
|
|
||
| private final long start; | ||
| private final Duration timeOut; | ||
| String CPU = "CPU"; | ||
| String WALL = "WALL"; | ||
|
|
||
| public TimeoutChecker(Duration timeOut) { | ||
| this.start = System.currentTimeMillis(); | ||
| this.timeOut = timeOut; | ||
| } | ||
|
|
||
| public TimeoutChecker(long start, Duration timeOut) { | ||
| this.start = start; | ||
| this.timeOut = timeOut; | ||
| } | ||
| boolean isTimedOut(); | ||
|
|
||
| public boolean isTimedOut(long currentTimeMillis) { | ||
| return (currentTimeMillis - start) > timeOut.toMillis(); | ||
| } | ||
|
|
||
| public long getStart() { | ||
| return start; | ||
| } | ||
| Duration getTimeOut(); | ||
|
|
||
| public Duration getTimeOut() { | ||
| return timeOut; | ||
| static TimeoutChecker create(Config config, Duration timeout) { | ||
| if (config.getDynamicInstrumentationTimeoutCheckerMode().equals(CPU)) { | ||
| return new CpuTimeoutChecker(timeout); | ||
| } | ||
| return new WallTimeoutChecker(timeout); | ||
| } | ||
| } | ||
37 changes: 37 additions & 0 deletions
37
...ger-bootstrap/src/main/java/datadog/trace/bootstrap/debugger/util/WallTimeoutChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| package datadog.trace.bootstrap.debugger.util; | ||
|
|
||
| import java.time.Duration; | ||
|
|
||
| public class WallTimeoutChecker implements TimeoutChecker { | ||
|
|
||
| private final long start; | ||
| private final Duration timeOut; | ||
|
|
||
| public WallTimeoutChecker(Duration timeOut) { | ||
| this.start = System.currentTimeMillis(); | ||
| this.timeOut = timeOut; | ||
| } | ||
|
|
||
| public WallTimeoutChecker(long start, Duration timeOut) { | ||
| this.start = start; | ||
| this.timeOut = timeOut; | ||
| } | ||
|
|
||
| public boolean isTimedOut(long currentTimeMillis) { | ||
| return (currentTimeMillis - start) > timeOut.toMillis(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isTimedOut() { | ||
| return isTimedOut(System.currentTimeMillis()); | ||
| } | ||
|
|
||
| public long getStart() { | ||
| return start; | ||
| } | ||
|
|
||
| @Override | ||
| public Duration getTimeOut() { | ||
| return timeOut; | ||
| } | ||
| } |
53 changes: 44 additions & 9 deletions
53
...ger-bootstrap/src/test/java/datadog/trace/bootstrap/debugger/util/TimeoutCheckerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,20 +1,55 @@ | ||
| package datadog.trace.bootstrap.debugger.util; | ||
|
|
||
| import static datadog.trace.bootstrap.debugger.util.TimeoutChecker.DEFAULT_TIME_OUT; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertInstanceOf; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import datadog.trace.api.Config; | ||
| import java.time.Duration; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.locks.LockSupport; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class TimeoutCheckerTest { | ||
| @Test | ||
| public void timedOut() { | ||
| public void wallTimedOut() { | ||
| long start = System.currentTimeMillis(); | ||
| Duration timeout = Duration.ofMillis(100); | ||
| // assume that start & timestamp captured in instance below are very close | ||
| TimeoutChecker timeoutChecker = new TimeoutChecker(DEFAULT_TIME_OUT); | ||
| Assertions.assertFalse(timeoutChecker.isTimedOut(start + 1)); | ||
| Assertions.assertTrue(timeoutChecker.isTimedOut(start + DEFAULT_TIME_OUT.toMillis() * 2)); | ||
| timeoutChecker = new TimeoutChecker(start, DEFAULT_TIME_OUT); | ||
| Assertions.assertFalse(timeoutChecker.isTimedOut(start + 1)); | ||
| Assertions.assertTrue(timeoutChecker.isTimedOut(start + DEFAULT_TIME_OUT.toMillis() * 2)); | ||
| WallTimeoutChecker timeoutChecker = new WallTimeoutChecker(timeout); | ||
| assertFalse(timeoutChecker.isTimedOut(start + 1)); | ||
| assertTrue(timeoutChecker.isTimedOut(start + timeout.toMillis() * 2)); | ||
| timeoutChecker = new WallTimeoutChecker(start, timeout); | ||
| assertFalse(timeoutChecker.isTimedOut(start + 1)); | ||
| assertTrue(timeoutChecker.isTimedOut(start + timeout.toMillis() * 2)); | ||
| } | ||
|
|
||
| @Test | ||
| public void cpuTimedOut() { | ||
| CpuTimeoutChecker cpuTimeoutChecker = new CpuTimeoutChecker(Duration.ofMillis(1)); | ||
| LockSupport.parkNanos(TimeUnit.MILLISECONDS.toNanos(10)); // not consume cpu | ||
| assertFalse(cpuTimeoutChecker.isTimedOut()); | ||
| burnCpu(5_000_000); | ||
| assertTrue(cpuTimeoutChecker.isTimedOut()); | ||
| } | ||
|
|
||
| static volatile int counter; | ||
|
|
||
| private static void burnCpu(int iterations) { | ||
| for (int i = 0; i < iterations; i++) { | ||
| counter++; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void configTimeout() { | ||
| Config config = mock(Config.class); | ||
| when(config.getDynamicInstrumentationTimeoutCheckerMode()).thenReturn(TimeoutChecker.CPU); | ||
| assertInstanceOf(CpuTimeoutChecker.class, TimeoutChecker.create(config, Duration.ofMillis(50))); | ||
| when(config.getDynamicInstrumentationTimeoutCheckerMode()).thenReturn(TimeoutChecker.WALL); | ||
| assertInstanceOf( | ||
| WallTimeoutChecker.class, TimeoutChecker.create(config, Duration.ofMillis(50))); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...a-agent/agent-debugger/debugger-el/src/main/java/com/datadog/debugger/el/EvalContext.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.datadog.debugger.el; | ||
|
|
||
| import datadog.trace.bootstrap.debugger.el.ValueReferenceResolver; | ||
| import datadog.trace.bootstrap.debugger.util.TimeoutChecker; | ||
|
|
||
| public class EvalContext { | ||
| private final ValueReferenceResolver valueRefResolver; | ||
| private final TimeoutChecker timeoutChecker; | ||
|
|
||
| public EvalContext( | ||
| final ValueReferenceResolver valueRefResolver, final TimeoutChecker timeoutChecker) { | ||
| this.valueRefResolver = valueRefResolver; | ||
| this.timeoutChecker = timeoutChecker; | ||
| } | ||
|
|
||
| public ValueReferenceResolver getValueRefResolver() { | ||
| return valueRefResolver; | ||
| } | ||
|
|
||
| public TimeoutChecker getTimeoutChecker() { | ||
| return timeoutChecker; | ||
| } | ||
| } |
4 changes: 1 addition & 3 deletions
4
...va-agent/agent-debugger/debugger-el/src/main/java/com/datadog/debugger/el/Expression.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gross. enum? 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The config is coming from
Configand put an enum in Config is complicated for nothing. so the config is a String.