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 <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Dec 16, 2023
1 parent b17bf55 commit 55cfca8
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class Connection extends PrimaryReadReplicaConnection {

protected $tableDirtyWrites = [];

protected ?float $transactionActiveSince = null;

/**
* Initializes a new instance of the Connection class.
*
Expand Down Expand Up @@ -333,6 +335,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 @@ -645,4 +648,30 @@ 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() {

Check failure on line 671 in lib/private/DB/Connection.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidReturnType

lib/private/DB/Connection.php:671:18: InvalidReturnType: No return statements were found for method OC\DB\Connection::rollBack but return type 'bool' was expected (see https://psalm.dev/011)

Check failure

Code scanning / Psalm

InvalidReturnType Error

No return statements were found for method OC\DB\Connection::rollBack but return type 'bool' was expected
$result = parent::rollBack();
if ($this->getTransactionNestingLevel() === 0) {
$this->transactionActiveSince = null;
}
}
}

0 comments on commit 55cfca8

Please sign in to comment.