-
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
track blocked thread event and report statistics in metrics endpoint (#…
…1649) Co-authored-by: Guillaume Grossetie <[email protected]> resolves #1653
- Loading branch information
1 parent
d10c2f5
commit cc0a6d1
Showing
10 changed files
with
340 additions
and
34 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
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
96 changes: 96 additions & 0 deletions
96
server/src/main/java/io/kroki/server/service/KrokiBlockedThreadChecker.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,96 @@ | ||
package io.kroki.server.service; | ||
|
||
import com.github.benmanes.caffeine.cache.Cache; | ||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.google.common.annotations.VisibleForTesting; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.VertxException; | ||
import io.vertx.core.VertxOptions; | ||
import io.vertx.core.impl.VertxInternal; | ||
import io.vertx.core.impl.btc.BlockedThreadEvent; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.time.Clock; | ||
import java.time.Duration; | ||
import java.time.Instant; | ||
import java.util.Objects; | ||
|
||
public class KrokiBlockedThreadChecker { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(KrokiBlockedThreadChecker.class); | ||
|
||
private final int evenLoopPoolSize; | ||
private final int workerPoolSize; | ||
private final Duration trackStatsFor; | ||
|
||
private final Cache<String, Instant> eventLoopStats; | ||
private final Cache<String, Instant> workerStats; | ||
|
||
private final Clock clock; | ||
|
||
public KrokiBlockedThreadChecker(Vertx vertx, VertxOptions options) { | ||
this(vertx, options, null); | ||
} | ||
|
||
KrokiBlockedThreadChecker(Vertx vertx, VertxOptions options, Clock clock) { | ||
this.evenLoopPoolSize = options.getEventLoopPoolSize(); | ||
this.workerPoolSize = options.getWorkerPoolSize(); | ||
this.trackStatsFor = Duration.of(options.getBlockedThreadCheckInterval(), options.getBlockedThreadCheckIntervalUnit().toChronoUnit()); | ||
|
||
eventLoopStats = Caffeine.newBuilder() | ||
.maximumSize(evenLoopPoolSize) | ||
.expireAfterWrite(trackStatsFor) | ||
.build(); | ||
workerStats = Caffeine.newBuilder() | ||
.maximumSize(workerPoolSize) | ||
.expireAfterWrite(trackStatsFor) | ||
.build(); | ||
|
||
if (vertx instanceof VertxInternal) { | ||
((VertxInternal) vertx).blockedThreadChecker().setThreadBlockedHandler((bte) -> { | ||
defaultHandlerFromVertx(bte); | ||
trackBlockedThread(bte); | ||
}); | ||
} | ||
|
||
this.clock = Objects.requireNonNullElseGet(clock, Clock::systemDefaultZone); | ||
} | ||
|
||
public long blockedWorkerThreadPercentage() { | ||
return Math.floorDiv(nonExpiredEntryCount(workerStats) * 100, workerPoolSize); | ||
} | ||
|
||
public long blockedEventLoopThreadPercentage() { | ||
return Math.floorDiv(nonExpiredEntryCount(eventLoopStats) * 100, evenLoopPoolSize); | ||
} | ||
|
||
private long nonExpiredEntryCount(Cache<String, Instant> stats) { | ||
final var now = this.clock.instant(); | ||
return stats.asMap().entrySet().stream().filter(e -> e.getValue().isAfter(now)).count(); | ||
} | ||
|
||
@VisibleForTesting | ||
void trackBlockedThread(BlockedThreadEvent bte) { | ||
if (bte.duration() > bte.warningExceptionTime()) { | ||
if (bte.thread().getName().startsWith("vert.x-worker-thread")) { | ||
workerStats.put(bte.thread().getName(), this.clock.instant().plus(trackStatsFor)); | ||
} else if (bte.thread().getName().startsWith("vert.x-eventloop-thread")) { | ||
eventLoopStats.put(bte.thread().getName(), this.clock.instant().plus(trackStatsFor)); | ||
} | ||
} | ||
} | ||
|
||
// copied from io.vertx.core.impl.btc.BlockedThreadChecker#defaultBlockedThreadHandler | ||
// because this method is private :/ | ||
private void defaultHandlerFromVertx(BlockedThreadEvent bte) { | ||
final String message = "Thread " + bte.thread() + " has been blocked for " + (bte.duration() / 1_000_000) + " ms, time limit is " + (bte.maxExecTime() / 1_000_000) + " ms"; | ||
if (bte.duration() <= bte.warningExceptionTime()) { | ||
logger.warn(message); | ||
} else { | ||
VertxException stackTrace = new VertxException("Thread blocked"); | ||
stackTrace.setStackTrace(bte.thread().getStackTrace()); | ||
logger.warn(message, stackTrace); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
server/src/main/java/io/kroki/server/service/MetricHandler.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,36 @@ | ||
package io.kroki.server.service; | ||
|
||
import io.vertx.core.Handler; | ||
import io.vertx.core.buffer.Buffer; | ||
import io.vertx.core.http.HttpHeaders; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
public class MetricHandler { | ||
|
||
private final KrokiBlockedThreadChecker blockedThreadChecker; | ||
private final String namespace; | ||
|
||
public MetricHandler(KrokiBlockedThreadChecker blockedThreadChecker) { | ||
this.blockedThreadChecker = blockedThreadChecker; | ||
this.namespace = "kroki"; | ||
} | ||
|
||
public Handler<RoutingContext> create() { | ||
String workerThreadBlockedMetricName = namespace + "_worker_thread_blocked_percentage"; | ||
String eventLoopThreadBlockedMetricName = namespace + "_event_loop_thread_blocked_percentage"; | ||
return routingContext -> { | ||
long timestamp = System.currentTimeMillis(); | ||
Buffer buffer = Buffer.buffer(); | ||
buffer.appendString("# HELP " + workerThreadBlockedMetricName + " The percentage of worker thread blocked.\n"); | ||
buffer.appendString("# TYPE " + workerThreadBlockedMetricName + " gauge\n"); | ||
buffer.appendString(String.join(" ", workerThreadBlockedMetricName, Long.toString(blockedThreadChecker.blockedWorkerThreadPercentage()), Long.toString(timestamp)) + "\n\n"); | ||
buffer.appendString("# HELP " + eventLoopThreadBlockedMetricName + " The percentage of event loop thread blocked.\n"); | ||
buffer.appendString("# TYPE " + eventLoopThreadBlockedMetricName + " gauge\n"); | ||
buffer.appendString(String.join(" ", eventLoopThreadBlockedMetricName, Long.toString(blockedThreadChecker.blockedEventLoopThreadPercentage()), Long.toString(timestamp)) + "\n\n"); | ||
routingContext | ||
.response() | ||
.putHeader(HttpHeaders.CONTENT_TYPE, "text/plain; version=0.0.4") | ||
.end(buffer); | ||
}; | ||
} | ||
} |
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
Oops, something went wrong.