Interface MetricsCollector
The default collector is disabledInstance(), which performs no work. inMemoryInstance() returns a
fresh counter-only collector useful for tests and ad-hoc inspection through snapshot().
Implementations must be thread-safe, non-blocking, and failure-tolerant. Pyranid catches and discards collector exceptions so metrics collection cannot affect database behavior.
- Since:
- 4.2.0
- Author:
- Mark Allen
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic enumPhysical transaction begin phase that failed.static final recordCounter snapshot for collectors that support in-process inspection.static enumTerminal outcome for an opened stream.static enumLogical outcome for a closure-based transaction. -
Method Summary
Modifier and TypeMethodDescriptiondefault voiddidAcquireStatementConnection(@NonNull StatementContext<?> ctx, @NonNull Duration acquisitionDuration) Called after Pyranid successfully acquires a statement-scopedConnection.default voiddidAcquireTransactionConnection(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration acquisitionDuration) Called after Pyranid successfully acquires the JDBC connection backing a physical transaction.default voiddidBeginPhysicalTransaction(@NonNull Transaction transaction, @NonNull TransactionIsolation isolation, @NonNull DatabaseType databaseType) Called after Pyranid successfully begins a physical JDBC transaction.default voiddidCloseStream(@NonNull StatementContext<?> ctx, @NonNull MetricsCollector.StreamTerminalOutcome outcome, @NonNull Long rowsConsumed, @NonNull Duration streamDuration, @Nullable Throwable throwable) Called after an opened stream reaches a terminal state.default voiddidCommitPhysicalTransaction(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration physicalDuration) Called after Pyranid successfully commits a physical JDBC transaction.default voiddidCreateSavepoint(@NonNull Transaction transaction, @NonNull DatabaseType databaseType) Called after Pyranid successfully creates a transaction savepoint.default voiddidEnterTransactionClosure(@NonNull Transaction transaction, @NonNull TransactionIsolation isolation, @NonNull DatabaseType databaseType) Called when Pyranid enters a closure-based transaction.default voiddidExecuteStatement(@NonNull StatementContext<?> ctx, @NonNull StatementLog<?> statementLog, @NonNull StatementResult result) Called after Pyranid successfully executes a statement.default voiddidExitTransactionClosure(@NonNull Transaction transaction, @NonNull MetricsCollector.TransactionClosureOutcome outcome, @NonNull DatabaseType databaseType, @NonNull Duration logicalDuration, @Nullable Throwable thrown) Called when Pyranid exits a closure-based transaction.default voiddidFailToAcquireStatementConnection(@NonNull StatementContext<?> ctx, @NonNull DatabaseType databaseType, @NonNull Duration acquisitionDuration, @NonNull Throwable throwable) Called after Pyranid fails to acquire a statement-scopedConnection.default voiddidFailToAcquireTransactionConnection(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration acquisitionDuration, @NonNull Throwable throwable) Called after Pyranid fails to acquire the JDBC connection backing a physical transaction.default voiddidFailToBeginPhysicalTransaction(@NonNull Transaction transaction, @NonNull TransactionIsolation isolation, @NonNull MetricsCollector.PhysicalTransactionBeginFailurePhase phase, @NonNull DatabaseType databaseType, @NonNull Throwable throwable) Called after Pyranid fails while beginning a physical JDBC transaction.default voiddidFailToCommitPhysicalTransaction(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration physicalDuration, @NonNull Throwable throwable) Called after Pyranid fails to commit a physical JDBC transaction.default voiddidFailToExecuteStatement(@NonNull StatementContext<?> ctx, @NonNull StatementLog<?> statementLog, @NonNull DatabaseType databaseType, @NonNull Throwable throwable) Called after Pyranid fails to execute a statement.default voiddidFailToOpenStream(@NonNull StatementContext<?> ctx, @NonNull DatabaseType databaseType, @NonNull Duration openDuration, @NonNull Throwable throwable) Called after Pyranid fails to open a streaming statement.default voiddidFailToReleaseStatementConnection(@NonNull StatementContext<?> ctx, @NonNull Duration heldDuration, @NonNull Throwable throwable) Called after Pyranid fails to release a statement-scopedConnection.default voiddidFailToReleaseTransactionConnection(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration heldDuration, @NonNull Throwable throwable) Called after Pyranid fails to release the JDBC connection backing a physical transaction.default voiddidFailToRollbackPhysicalTransaction(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration physicalDuration, @NonNull Throwable throwable) Called after Pyranid fails to roll back a physical JDBC transaction.default voiddidOpenStream(@NonNull StatementContext<?> ctx, @NonNull Duration openDuration) Called after Pyranid successfully opens a streaming statement.default voiddidReleaseSavepoint(@NonNull Transaction transaction, @NonNull DatabaseType databaseType) Called after Pyranid successfully releases a transaction savepoint.default voiddidReleaseStatementConnection(@NonNull StatementContext<?> ctx, @NonNull Duration heldDuration) Called after Pyranid successfully releases a statement-scopedConnection.default voiddidReleaseTransactionConnection(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration heldDuration) Called after Pyranid successfully releases the JDBC connection backing a physical transaction.default voiddidRollbackPhysicalTransaction(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration physicalDuration) Called after Pyranid successfully rolls back a physical JDBC transaction.default voiddidRollbackToSavepoint(@NonNull Transaction transaction, @NonNull DatabaseType databaseType) Called after Pyranid successfully rolls back to a transaction savepoint.default voiddidRunPostTransactionOperation(@NonNull Transaction transaction, @NonNull TransactionResult result, @NonNull DatabaseType databaseType, @NonNull Duration duration, @Nullable Throwable throwable) Called after a post-transaction operation runs.static @NonNull MetricsCollectorReturns the shared no-op metrics collector.static @NonNull MetricsCollectorCreates a fresh in-memory counter collector.default voidreset()Best-effort reset for test and ad-hoc use.default @NonNull Optional<MetricsCollector.Snapshot> snapshot()Returns a counter snapshot if this collector supports in-process inspection.default voidwillAcquireStatementConnection(@NonNull StatementContext<?> ctx) Called immediately before Pyranid attempts to acquire a statement-scopedConnection.default voidwillAcquireTransactionConnection(@NonNull Transaction transaction, @NonNull DatabaseType databaseType) Called immediately before Pyranid attempts to acquire the JDBC connection backing a physical transaction.default voidwillExecuteStatement(@NonNull StatementContext<?> ctx) Called immediately before Pyranid executes a statement.default voidwillOpenStream(@NonNull StatementContext<?> ctx) Called immediately before Pyranid opens a streaming statement.
-
Method Details
-
willAcquireStatementConnection
Called immediately before Pyranid attempts to acquire a statement-scopedConnection.Statement-scoped connections are used for standalone statement execution and stream opening outside a physical
Transaction.- Parameters:
ctx- statement context for the operation that needs a connection
-
didAcquireStatementConnection
default void didAcquireStatementConnection(@NonNull StatementContext<?> ctx, @NonNull Duration acquisitionDuration) Called after Pyranid successfully acquires a statement-scopedConnection.- Parameters:
ctx- statement context for the operation that acquired a connectionacquisitionDuration- elapsed time spent acquiring the connection
-
didFailToAcquireStatementConnection
default void didFailToAcquireStatementConnection(@NonNull StatementContext<?> ctx, @NonNull DatabaseType databaseType, @NonNull Duration acquisitionDuration, @NonNull Throwable throwable) Called after Pyranid fails to acquire a statement-scopedConnection.- Parameters:
ctx- statement context for the operation that needed a connectiondatabaseType- database type known at the time of failureacquisitionDuration- elapsed time spent attempting to acquire the connectionthrowable- failure that prevented connection acquisition
-
didReleaseStatementConnection
default void didReleaseStatementConnection(@NonNull StatementContext<?> ctx, @NonNull Duration heldDuration) Called after Pyranid successfully releases a statement-scopedConnection.- Parameters:
ctx- statement context for the operation that used the connectionheldDuration- elapsed time between successful acquisition and release
-
didFailToReleaseStatementConnection
default void didFailToReleaseStatementConnection(@NonNull StatementContext<?> ctx, @NonNull Duration heldDuration, @NonNull Throwable throwable) Called after Pyranid fails to release a statement-scopedConnection.- Parameters:
ctx- statement context for the operation that used the connectionheldDuration- elapsed time between successful acquisition and the failed release attemptthrowable- failure that prevented connection release
-
willAcquireTransactionConnection
default void willAcquireTransactionConnection(@NonNull Transaction transaction, @NonNull DatabaseType databaseType) Called immediately before Pyranid attempts to acquire the JDBC connection backing a physical transaction.Pyranid transactions acquire their JDBC connection lazily, so this callback is emitted only when transaction work first needs database access.
- Parameters:
transaction- transaction that needs a physical connectiondatabaseType- database type known at the time of acquisition
-
didAcquireTransactionConnection
default void didAcquireTransactionConnection(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration acquisitionDuration) Called after Pyranid successfully acquires the JDBC connection backing a physical transaction.- Parameters:
transaction- transaction that acquired the connectiondatabaseType- database type known at the time of acquisitionacquisitionDuration- elapsed time spent acquiring the connection
-
didFailToAcquireTransactionConnection
default void didFailToAcquireTransactionConnection(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration acquisitionDuration, @NonNull Throwable throwable) Called after Pyranid fails to acquire the JDBC connection backing a physical transaction.- Parameters:
transaction- transaction that needed a connectiondatabaseType- database type known at the time of failureacquisitionDuration- elapsed time spent attempting to acquire the connectionthrowable- failure that prevented connection acquisition
-
didReleaseTransactionConnection
default void didReleaseTransactionConnection(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration heldDuration) Called after Pyranid successfully releases the JDBC connection backing a physical transaction.- Parameters:
transaction- transaction that owned the connectiondatabaseType- database type known at the time of releaseheldDuration- elapsed time between successful acquisition and release
-
didFailToReleaseTransactionConnection
default void didFailToReleaseTransactionConnection(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration heldDuration, @NonNull Throwable throwable) Called after Pyranid fails to release the JDBC connection backing a physical transaction.- Parameters:
transaction- transaction that owned the connectiondatabaseType- database type known at the time of failureheldDuration- elapsed time between successful acquisition and the failed release attemptthrowable- failure that prevented connection release
-
didEnterTransactionClosure
default void didEnterTransactionClosure(@NonNull Transaction transaction, @NonNull TransactionIsolation isolation, @NonNull DatabaseType databaseType) Called when Pyranid enters a closure-based transaction.This is a logical transaction event and may occur even if user code never performs database work and no physical JDBC transaction is begun.
- Parameters:
transaction- transaction passed to the closure through Pyranid APIsisolation- requested transaction isolationdatabaseType- database type known at transaction entry
-
didExitTransactionClosure
default void didExitTransactionClosure(@NonNull Transaction transaction, @NonNull MetricsCollector.TransactionClosureOutcome outcome, @NonNull DatabaseType databaseType, @NonNull Duration logicalDuration, @Nullable Throwable thrown) Called when Pyranid exits a closure-based transaction.- Parameters:
transaction- transaction that is exitingoutcome- logical transaction outcomedatabaseType- database type known at transaction exitlogicalDuration- elapsed time from transaction entry to exit, including user code, physical transaction work, cleanup, and post-transaction operationsthrown- exception or error that caused the transaction to exit abnormally, ornullwhen the transaction completed without a reported failure
-
didBeginPhysicalTransaction
default void didBeginPhysicalTransaction(@NonNull Transaction transaction, @NonNull TransactionIsolation isolation, @NonNull DatabaseType databaseType) Called after Pyranid successfully begins a physical JDBC transaction.- Parameters:
transaction- transaction whose physical JDBC transaction beganisolation- requested transaction isolationdatabaseType- database type known at transaction begin
-
didFailToBeginPhysicalTransaction
default void didFailToBeginPhysicalTransaction(@NonNull Transaction transaction, @NonNull TransactionIsolation isolation, @NonNull MetricsCollector.PhysicalTransactionBeginFailurePhase phase, @NonNull DatabaseType databaseType, @NonNull Throwable throwable) Called after Pyranid fails while beginning a physical JDBC transaction.- Parameters:
transaction- transaction whose physical JDBC transaction failed to beginisolation- requested transaction isolationphase- begin phase that faileddatabaseType- database type known at the time of failurethrowable- failure that prevented the physical transaction from beginning
-
didCommitPhysicalTransaction
default void didCommitPhysicalTransaction(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration physicalDuration) Called after Pyranid successfully commits a physical JDBC transaction.- Parameters:
transaction- transaction that committeddatabaseType- database type known at commit timephysicalDuration- elapsed time between physical transaction begin and commit
-
didFailToCommitPhysicalTransaction
default void didFailToCommitPhysicalTransaction(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration physicalDuration, @NonNull Throwable throwable) Called after Pyranid fails to commit a physical JDBC transaction.- Parameters:
transaction- transaction whose commit faileddatabaseType- database type known at commit timephysicalDuration- elapsed time between physical transaction begin and the failed commit attemptthrowable- failure that prevented commit from completing normally
-
didRollbackPhysicalTransaction
default void didRollbackPhysicalTransaction(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration physicalDuration) Called after Pyranid successfully rolls back a physical JDBC transaction.- Parameters:
transaction- transaction that rolled backdatabaseType- database type known at rollback timephysicalDuration- elapsed time between physical transaction begin and rollback
-
didFailToRollbackPhysicalTransaction
default void didFailToRollbackPhysicalTransaction(@NonNull Transaction transaction, @NonNull DatabaseType databaseType, @NonNull Duration physicalDuration, @NonNull Throwable throwable) Called after Pyranid fails to roll back a physical JDBC transaction.- Parameters:
transaction- transaction whose rollback faileddatabaseType- database type known at rollback timephysicalDuration- elapsed time between physical transaction begin and the failed rollback attemptthrowable- failure that prevented rollback from completing normally
-
didRunPostTransactionOperation
default void didRunPostTransactionOperation(@NonNull Transaction transaction, @NonNull TransactionResult result, @NonNull DatabaseType databaseType, @NonNull Duration duration, @Nullable Throwable throwable) Called after a post-transaction operation runs.- Parameters:
transaction- transaction whose post-transaction operation ranresult- transaction result visible to the post-transaction operationdatabaseType- database type known at post-transaction execution timeduration- elapsed time spent running the post-transaction operationthrowable- failure thrown by the post-transaction operation, ornullwhen it completed normally
-
didCreateSavepoint
default void didCreateSavepoint(@NonNull Transaction transaction, @NonNull DatabaseType databaseType) Called after Pyranid successfully creates a transaction savepoint.- Parameters:
transaction- transaction that created the savepointdatabaseType- database type known at savepoint creation time
-
didRollbackToSavepoint
default void didRollbackToSavepoint(@NonNull Transaction transaction, @NonNull DatabaseType databaseType) Called after Pyranid successfully rolls back to a transaction savepoint.- Parameters:
transaction- transaction that rolled back to the savepointdatabaseType- database type known at savepoint rollback time
-
didReleaseSavepoint
default void didReleaseSavepoint(@NonNull Transaction transaction, @NonNull DatabaseType databaseType) Called after Pyranid successfully releases a transaction savepoint.- Parameters:
transaction- transaction that released the savepointdatabaseType- database type known at savepoint release time
-
willExecuteStatement
Called immediately before Pyranid executes a statement.- Parameters:
ctx- statement context for the statement about to execute
-
didExecuteStatement
default void didExecuteStatement(@NonNull StatementContext<?> ctx, @NonNull StatementLog<?> statementLog, @NonNull StatementResult result) Called after Pyranid successfully executes a statement.- Parameters:
ctx- statement context for the executed statementstatementLog- diagnostic statement log for the executionresult- statement execution result
-
didFailToExecuteStatement
default void didFailToExecuteStatement(@NonNull StatementContext<?> ctx, @NonNull StatementLog<?> statementLog, @NonNull DatabaseType databaseType, @NonNull Throwable throwable) Called after Pyranid fails to execute a statement.- Parameters:
ctx- statement context for the failed statementstatementLog- diagnostic statement log for the failed executiondatabaseType- database type known at statement failure timethrowable- failure thrown while executing the statement
-
willOpenStream
Called immediately before Pyranid opens a streaming statement.- Parameters:
ctx- statement context for the stream about to open
-
didOpenStream
Called after Pyranid successfully opens a streaming statement.- Parameters:
ctx- statement context for the opened streamopenDuration- elapsed time spent opening the stream
-
didFailToOpenStream
default void didFailToOpenStream(@NonNull StatementContext<?> ctx, @NonNull DatabaseType databaseType, @NonNull Duration openDuration, @NonNull Throwable throwable) Called after Pyranid fails to open a streaming statement.- Parameters:
ctx- statement context for the stream that failed to opendatabaseType- database type known at stream-open failure timeopenDuration- elapsed time spent attempting to open the streamthrowable- failure that prevented the stream from opening
-
didCloseStream
default void didCloseStream(@NonNull StatementContext<?> ctx, @NonNull MetricsCollector.StreamTerminalOutcome outcome, @NonNull Long rowsConsumed, @NonNull Duration streamDuration, @Nullable Throwable throwable) Called after an opened stream reaches a terminal state.Pyranid does not emit this callback for streams that fail before they open; those failures are reported through
didFailToOpenStream(StatementContext, DatabaseType, Duration, Throwable).- Parameters:
ctx- statement context for the streamoutcome- terminal stream outcomerowsConsumed- number of rows consumed from the streamstreamDuration- elapsed time between stream-open start and terminal close handlingthrowable- failure associated with the terminal outcome, ornullwhen no failure is available
-
snapshot
Returns a counter snapshot if this collector supports in-process inspection.Implementations may read counters independently without cross-counter atomicity. Callers that need invariant-consistent snapshots should drain in-flight work before reading.
- Returns:
- a metrics snapshot, if supported
-
reset
Best-effort reset for test and ad-hoc use.Concurrent updates may race with reset operations. Production rolling-window semantics should be implemented outside this interface.
-
disabledInstance
Returns the shared no-op metrics collector.- Returns:
- no-op metrics collector used when metrics collection is disabled
-
inMemoryInstance
Creates a fresh in-memory counter collector.This collector is intended for tests and lightweight local inspection through
snapshot().- Returns:
- new in-memory metrics collector
-