-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add circuit breaker for Exception Debugging (#7074)
To avoid having a spike of exceptions to analyze (fingerprint/instrument) we put in front of handling the exception a circuit breaker which open when we reach more than 100 (configurable via DD_EXCEPTION_REPLAY_MAX_EXCEPTION_ANALYSIS_LIMIT) per second
- Loading branch information
Showing
9 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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
41 changes: 41 additions & 0 deletions
41
dd-java-agent/agent-debugger/src/main/java/com/datadog/debugger/util/CircuitBreaker.java
This file contains 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,41 @@ | ||
package com.datadog.debugger.util; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
// CircuitBreaker is a simple circuit breaker implementation that allows a certain number of trips | ||
// within a time window. If the number of trips exceeds the limit, the circuit breaker will trip and | ||
// return false until the time window has passed. | ||
public class CircuitBreaker { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(CircuitBreaker.class); | ||
|
||
private final int maxTrips; | ||
private final Duration timeWindow; | ||
private AtomicInteger count = new AtomicInteger(0); | ||
private volatile long lastResetTime = System.currentTimeMillis(); | ||
private volatile long lastLoggingTime = System.currentTimeMillis(); | ||
|
||
public CircuitBreaker(int maxTrips, Duration timeWindow) { | ||
this.maxTrips = maxTrips; | ||
this.timeWindow = timeWindow; | ||
} | ||
|
||
public boolean trip() { | ||
int localCount = count.incrementAndGet(); | ||
if (localCount > maxTrips) { | ||
long currentTime = System.currentTimeMillis(); | ||
if (currentTime - lastLoggingTime > Duration.ofMinutes(1).toMillis()) { | ||
lastLoggingTime = currentTime; | ||
LOGGER.debug("Circuit breaker opened"); | ||
} | ||
if (currentTime - lastResetTime > timeWindow.toMillis()) { | ||
lastResetTime = currentTime; | ||
localCount = 1; | ||
count.set(localCount); | ||
} | ||
} | ||
return localCount <= maxTrips; | ||
} | ||
} |
This file contains 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 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
37 changes: 37 additions & 0 deletions
37
dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/util/CircuitBreakerTest.java
This file contains 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 com.datadog.debugger.util; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.locks.LockSupport; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class CircuitBreakerTest { | ||
|
||
@Test | ||
void noBreaker() { | ||
CircuitBreaker cb = new CircuitBreaker(3, Duration.ofMillis(10)); | ||
for (int i = 0; i < 10; i++) { | ||
assertTrue(cb.trip()); | ||
LockSupport.parkNanos(Duration.ofMillis(50).toNanos()); | ||
} | ||
} | ||
|
||
@Test | ||
void breaker() { | ||
CircuitBreaker cb = new CircuitBreaker(3, Duration.ofMillis(200)); | ||
for (int i = 0; i < 3; i++) { | ||
assertTrue(cb.trip()); | ||
} | ||
for (int i = 0; i < 100; i++) { | ||
assertFalse(cb.trip()); | ||
} | ||
LockSupport.parkNanos(Duration.ofMillis(250).toNanos()); | ||
for (int i = 0; i < 3; i++) { | ||
assertTrue(cb.trip()); | ||
} | ||
for (int i = 0; i < 100; i++) { | ||
assertFalse(cb.trip()); | ||
} | ||
} | ||
} |
This file contains 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 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 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