Skip to content

Commit

Permalink
feat: Add logging for transaction time
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <[email protected]>
  • Loading branch information
juliusknorr committed Dec 18, 2023
1 parent 79c4986 commit 9a9b437
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class Connection extends PrimaryReadReplicaConnection {
/** @var DbDataCollector|null */
protected $dbDataCollector = null;

protected ?float $transactionActiveSince = null;

/**
* Initializes a new instance of the Connection class.
*
Expand Down Expand Up @@ -306,6 +308,7 @@ protected function logQueryToFile(string $sql): void {
// FIXME: Improve to log the actual target db host
$isPrimary = $this->connections['primary'] === $this->_conn;
$prefix .= ' ' . ($isPrimary === true ? 'primary' : 'replica') . ' ';
$prefix .= ' ' . $this->getTransactionNestingLevel() . ' ';

file_put_contents(
$this->systemConfig->getValue('query_log_file', ''),
Expand Down Expand Up @@ -618,4 +621,31 @@ protected function performConnect(?string $connectionName = null): bool {
}
return $result;
}

public function beginTransaction() {
if (!$this->inTransaction()) {
$this->transactionActiveSince = microtime(true);
}
return parent::beginTransaction();
}

public function commit() {
$result = parent::commit();
if ($this->getTransactionNestingLevel() === 0) {
$timeTook = microtime(true) - $this->transactionActiveSince;
$this->transactionActiveSince = null;
if ($timeTook > 1) {
$this->logger->warning('Transaction took longer then 1s: ' . $timeTook, ['exception' => new \Exception('Long running transaction')]);
}
}
return $result;
}

public function rollBack() {
$result = parent::rollBack();
if ($this->getTransactionNestingLevel() === 0) {
$this->transactionActiveSince = null;
}
return $result;
}
}

0 comments on commit 9a9b437

Please sign in to comment.