Skip to content

Commit

Permalink
feat: add request id as comment to all queries
Browse files Browse the repository at this point in the history
Signed-off-by: Robin Appelman <robin@icewind.nl>
  • Loading branch information
icewind1991 committed Apr 22, 2024
1 parent 277052a commit 299af63
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
7 changes: 7 additions & 0 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@
*/
'dbpersistent' => '',

/**
* Add request id to the database query in a comment.
*
* This can be enabled to assist in mapping database logs to Nextcloud logs.
*/
'db.log_request_id' => false,

/**
* Indicates whether the Nextcloud instance was installed successfully; ``true``
* indicates a successful installation, and ``false`` indicates an unsuccessful
Expand Down
28 changes: 20 additions & 8 deletions lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class Connection extends \Doctrine\DBAL\Connection {
/** @var DbDataCollector|null */
protected $dbDataCollector = null;

protected bool $logRequestId;
protected string $requestId;

/**
* Initializes a new instance of the Connection class.
*
Expand Down Expand Up @@ -106,6 +109,9 @@ public function __construct(
$this->systemConfig = \OC::$server->getSystemConfig();
$this->logger = \OC::$server->get(LoggerInterface::class);

$this->logRequestId = $this->systemConfig->getValue('db.log_request_id', false);
$this->requestId = Server::get(IRequestId::class)->getId();

Check failure

Code scanning / Psalm

UndefinedClass Error

Class, interface or enum named OC\DB\Server does not exist

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

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedClass

lib/private/DB/Connection.php:113:22: UndefinedClass: Class, interface or enum named OC\DB\Server does not exist (see https://psalm.dev/019)

/** @var \OCP\Profiler\IProfiler */
$profiler = \OC::$server->get(IProfiler::class);
if ($profiler->isEnabled()) {
Expand Down Expand Up @@ -233,8 +239,7 @@ public function prepare($statement, $limit = null, $offset = null): Statement {
$platform = $this->getDatabasePlatform();
$statement = $platform->modifyLimitQuery($statement, $limit, $offset);
}
$statement = $this->replaceTablePrefix($statement);
$statement = $this->adapter->fixupStatement($statement);
$statement = $this->finishQuery($statement);

return parent::prepare($statement);
}
Expand All @@ -255,8 +260,7 @@ public function prepare($statement, $limit = null, $offset = null): Statement {
* @throws \Doctrine\DBAL\Exception
*/
public function executeQuery(string $sql, array $params = [], $types = [], QueryCacheProfile $qcp = null): Result {
$sql = $this->replaceTablePrefix($sql);
$sql = $this->adapter->fixupStatement($sql);
$sql = $this->finishQuery($sql);
$this->queriesExecuted++;
$this->logQueryToFile($sql);
return parent::executeQuery($sql, $params, $types, $qcp);
Expand All @@ -266,8 +270,7 @@ public function executeQuery(string $sql, array $params = [], $types = [], Query
* @throws Exception
*/
public function executeUpdate(string $sql, array $params = [], array $types = []): int {
$sql = $this->replaceTablePrefix($sql);
$sql = $this->adapter->fixupStatement($sql);
$sql = $this->finishQuery($sql);
$this->queriesExecuted++;
$this->logQueryToFile($sql);
return parent::executeUpdate($sql, $params, $types);
Expand All @@ -288,8 +291,7 @@ public function executeUpdate(string $sql, array $params = [], array $types = []
* @throws \Doctrine\DBAL\Exception
*/
public function executeStatement($sql, array $params = [], array $types = []): int {
$sql = $this->replaceTablePrefix($sql);
$sql = $this->adapter->fixupStatement($sql);
$sql = $this->finishQuery($sql);
$this->queriesExecuted++;
$this->logQueryToFile($sql);
return (int)parent::executeStatement($sql, $params, $types);
Expand Down Expand Up @@ -516,6 +518,16 @@ public function tableExists($table) {
return $schema->tablesExist([$table]);
}

protected function finishQuery(string $statement): string {
$statement = $this->replaceTablePrefix($statement);
$statement = $this->adapter->fixupStatement($statement);
if ($this->logRequestId) {
return $statement . " /* reqid: " . $this->requestId . " */";
} else {
return $statement;
}
}

// internal use
/**
* @param string $statement
Expand Down

0 comments on commit 299af63

Please sign in to comment.