Augment FoundationDB metrics to help debug epoch drill mystery

This commit is contained in:
Katherine authored and GitHub committed 2026-09-21 16:09:03 -04:00
1 parent acbf1d828c
commit 4443c31ee0
2 files changed
+31 -15

No files matched your search

@@ -28,8 +28,13 @@ public class FaultTolerantDatabase {
private final Bulkhead bulkhead;
private final LongAdder concurrentTransactionCount;
private final String name;
private static final String TRANSACTION_ERRORS_COUNTER = MetricsUtil.name(FaultTolerantDatabase.class, "transactionErrors");
private static final String TRANSACTION_COUNTER_NAME = MetricsUtil.name(FaultTolerantDatabase.class, "transactions");
private static final String CONTEXT_TAG_NAME = "context";
private static final String DATABASE_TAG_NAME = "name";
private static final String ERROR_CODE_TAG_NAME = "code";
public FaultTolerantDatabase(
final Database database,
@@ -37,7 +42,7 @@ public class FaultTolerantDatabase {
@Nullable final String circuitBreakerConfigurationName,
@Nullable final String bulkheadConfigurationName) {
this.database = database;
this.name = name;
this.circuitBreaker = circuitBreakerConfigurationName != null
? ResilienceUtil.getCircuitBreakerRegistry().circuitBreaker(name, circuitBreakerConfigurationName)
: ResilienceUtil.getCircuitBreakerRegistry().circuitBreaker(name);
@@ -47,18 +52,24 @@ public class FaultTolerantDatabase {
: ResilienceUtil.getBulkheadRegistry().bulkhead(name);
this.concurrentTransactionCount =
Metrics.gauge(MetricsUtil.name(getClass(), "concurrentTransactions"), Tags.of("name", name), new LongAdder());
Metrics.gauge(MetricsUtil.name(getClass(), "concurrentTransactions"), Tags.of(DATABASE_TAG_NAME, name), new LongAdder());
}
public String getName() {
return name;
}
public <T> T run(final Function<? super Transaction, T> retryable, final Context context) {
try {
Metrics.counter(TRANSACTION_COUNTER_NAME, CONTEXT_TAG_NAME, context.getName(), DATABASE_TAG_NAME, name).increment();
concurrentTransactionCount.increment();
return bulkhead.executeSupplier(() -> circuitBreaker.executeSupplier(() -> database.run(retryable)));
} catch (final Exception e) {
if (e instanceof final FDBException fdbException) {
Metrics.counter(TRANSACTION_ERRORS_COUNTER,
"context", context.getName(),
"code", String.valueOf(fdbException.getCode())
CONTEXT_TAG_NAME, context.getName(),
DATABASE_TAG_NAME, name,
ERROR_CODE_TAG_NAME, String.valueOf(fdbException.getCode())
).increment();
}
throw e;
@@ -77,15 +88,16 @@ public class FaultTolerantDatabase {
/// @return a cancellation-safe version of the future returned from [Database#runAsync(Function)]
public <T> CompletableFuture<T> runAsync(final Function<? super Transaction, ? extends CompletableFuture<T>> retryable,
final Context context) {
Metrics.counter(TRANSACTION_COUNTER_NAME, CONTEXT_TAG_NAME, context.getName(), DATABASE_TAG_NAME, name).increment();
concurrentTransactionCount.increment();
final CompletableFuture<T> runAsyncFuture = bulkhead.executeCompletionStage(() -> circuitBreaker.executeCompletionStage(() -> database.runAsync(retryable)
.whenComplete((_, throwable) -> {
if (throwable != null && ExceptionUtils.unwrap(throwable) instanceof final FDBException fdbException) {
Metrics.counter(TRANSACTION_ERRORS_COUNTER,
"context", context.getName(),
"code", String.valueOf(fdbException.getCode())
CONTEXT_TAG_NAME, context.getName(),
DATABASE_TAG_NAME, name,
ERROR_CODE_TAG_NAME, String.valueOf(fdbException.getCode())
).increment();
}
})
@@ -99,13 +111,15 @@ public class FaultTolerantDatabase {
public <T> T read(final Function<? super ReadTransaction, T> retryable, final Context context) {
try {
Metrics.counter(TRANSACTION_COUNTER_NAME, CONTEXT_TAG_NAME, context.getName(), DATABASE_TAG_NAME, name).increment();
concurrentTransactionCount.increment();
return bulkhead.executeSupplier(() -> circuitBreaker.executeSupplier(() -> database.read(retryable)));
} catch (final Exception e) {
if (e instanceof final FDBException fdbException) {
Metrics.counter(TRANSACTION_ERRORS_COUNTER,
"context", context.getName(),
"code", String.valueOf(fdbException.getCode())
CONTEXT_TAG_NAME, context.getName(),
DATABASE_TAG_NAME, name,
ERROR_CODE_TAG_NAME, String.valueOf(fdbException.getCode())
).increment();
}
throw e;
@@ -117,15 +131,16 @@ public class FaultTolerantDatabase {
public <T> CompletableFuture<T> readAsync(
final Function<? super ReadTransaction, ? extends CompletableFuture<T>> retryable,
final Context context) {
Metrics.counter(TRANSACTION_COUNTER_NAME, CONTEXT_TAG_NAME, context.getName(), DATABASE_TAG_NAME, name).increment();
concurrentTransactionCount.increment();
final CompletableFuture<T> readAsyncFuture = bulkhead.executeCompletionStage(() -> circuitBreaker.executeCompletionStage(() -> database.readAsync(retryable)
.whenComplete((_, throwable) -> {
if (throwable != null && ExceptionUtils.unwrap(throwable) instanceof final FDBException fdbException) {
Metrics.counter(TRANSACTION_ERRORS_COUNTER,
"context", context.getName(),
"code", String.valueOf(fdbException.getCode())
CONTEXT_TAG_NAME, context.getName(),
DATABASE_TAG_NAME, name,
ERROR_CODE_TAG_NAME, String.valueOf(fdbException.getCode())
).increment();
}
})))
@@ -91,6 +91,7 @@ class FoundationDbMessagePublisher {
private static final String WATCHES_COUNTER = MetricsUtil.name(FoundationDbMessagePublisher.class, "watches");
private static final String ACTION_TAG = "action";
private static final String DATABASE_TAG = "database";
enum State {
/// Messages are likely available in the queue. Initial state.
@@ -420,9 +421,9 @@ class FoundationDbMessagePublisher {
// again (if there is demand). When we run out of messages, this method will be called again, setting another watch,
// and so on, thus achieving a "watch for new messages -> read -> publish" loop.
watchFuture = transaction.watch(messagesAvailableWatchKey);
Metrics.counter(WATCHES_COUNTER, ACTION_TAG, "set").increment();
Metrics.counter(WATCHES_COUNTER, ACTION_TAG, "set", DATABASE_TAG, database.getName()).increment();
watchFuture.thenRun(() -> {
Metrics.counter(WATCHES_COUNTER, ACTION_TAG, "triggered").increment();
Metrics.counter(WATCHES_COUNTER, ACTION_TAG, "triggered", DATABASE_TAG, database.getName()).increment();
transitionStateOnEvent(Event.MESSAGE_AVAILABLE_WATCH_TRIGGERED);
});
}
@@ -432,7 +433,7 @@ class FoundationDbMessagePublisher {
private synchronized void cancelWatch() {
if (watchFuture != null) {
if (watchFuture.cancel(true)) {
Metrics.counter(WATCHES_COUNTER, ACTION_TAG, "cancelled").increment();
Metrics.counter(WATCHES_COUNTER, ACTION_TAG, "cancelled", DATABASE_TAG, database.getName()).increment();
}
watchFuture = null;
}