-
Notifications
You must be signed in to change notification settings - Fork 324
apply rate limit to queue events #7823
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
850b12a
apply rate limit to queue events
richardstartin 60ecdaa
test for PerRecordingRateLimiter
richardstartin 715316c
graal native image
richardstartin 58474f8
remove debugging code
richardstartin 116c7d8
no queue time on graal native image - requires too much build time in…
richardstartin 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
29 changes: 28 additions & 1 deletion
29
...c/main/java/datadog/trace/bootstrap/instrumentation/java/concurrent/QueueTimerHelper.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
2 changes: 2 additions & 0 deletions
2
.../java11/datadog/trace/bootstrap/instrumentation/jfr/backpressure/BackpressureSampler.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
2 changes: 2 additions & 0 deletions
2
...datadog/trace/bootstrap/instrumentation/jfr/directallocation/DirectAllocationSampler.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
2 changes: 2 additions & 0 deletions
2
.../main/java11/datadog/trace/bootstrap/instrumentation/jfr/exceptions/ExceptionSampler.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
38 changes: 38 additions & 0 deletions
38
internal-api/src/main/java/datadog/trace/api/sampling/PerRecordingRateLimiter.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,38 @@ | ||
| package datadog.trace.api.sampling; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.temporal.ChronoUnit; | ||
|
|
||
| public class PerRecordingRateLimiter { | ||
|
|
||
| private final AdaptiveSampler sampler; | ||
|
|
||
| public PerRecordingRateLimiter(Duration windowDuration, int limit, Duration recordingLength) { | ||
| this(windowDuration, limit, recordingLength, 16); | ||
| } | ||
|
|
||
| public PerRecordingRateLimiter( | ||
| Duration windowDuration, int limit, Duration recordingLength, int budgetLookback) { | ||
| int lookback = samplingWindowsPerRecording(recordingLength.getSeconds(), windowDuration); | ||
| int samplesPerWindow = | ||
| limit / samplingWindowsPerRecording(recordingLength.getSeconds(), windowDuration); | ||
| sampler = new AdaptiveSampler(windowDuration, samplesPerWindow, lookback, budgetLookback, true); | ||
| } | ||
|
|
||
| public boolean permit() { | ||
| return sampler.sample(); | ||
| } | ||
|
|
||
| public static int samplingWindowsPerRecording(long uploadPeriodSeconds, Duration samplingWindow) { | ||
| /* | ||
| * Java8 doesn't have dividedBy#Duration so we have to implement poor man's version. | ||
| * None of these durations should be big enough to warrant dealing with bigints. | ||
| * We also do not care about nanoseconds here. | ||
| */ | ||
| return (int) | ||
| Math.min( | ||
| Duration.of(uploadPeriodSeconds, ChronoUnit.SECONDS).toMillis() | ||
| / samplingWindow.toMillis(), | ||
| Integer.MAX_VALUE); | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
internal-api/src/test/java/datadog/trace/api/sampling/PerRecordingRateLimiterTest.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,34 @@ | ||
| package datadog.trace.api.sampling; | ||
|
|
||
| import static java.util.concurrent.TimeUnit.NANOSECONDS; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.Arrays; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class PerRecordingRateLimiterTest { | ||
|
|
||
| @Test | ||
| public void testLimitApplied() { | ||
| Duration window = Duration.ofMillis(100); | ||
| int limit = 20; | ||
| Duration recordingDurationSeconds = Duration.ofSeconds(1); | ||
| PerRecordingRateLimiter rateLimiter = | ||
| new PerRecordingRateLimiter(window, limit, recordingDurationSeconds, 5); | ||
| // no rate limiting is applied during the first window | ||
| int[] slots = new int[(int) (recordingDurationSeconds.toMillis() / window.toMillis())]; | ||
| long start = System.nanoTime(); | ||
| while (true) { | ||
| int slot = (int) (NANOSECONDS.toMillis(System.nanoTime() - start) / window.toMillis()); | ||
| if (slot >= slots.length) { | ||
| break; | ||
| } | ||
| if (rateLimiter.permit()) { | ||
| slots[slot]++; | ||
| } | ||
| } | ||
| assertTrue(Arrays.stream(slots).max().orElse(limit + 1) <= limit); | ||
| assertTrue(Arrays.stream(slots).sum() <= 2 * limit); | ||
| } | ||
| } |
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.
LGTM. Did you add this preemptively, or was NI analysis complaining for a build?