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>

[skip ci]
  • Loading branch information
icewind1991 authored and backportbot[bot] committed Apr 22, 2024
1 parent a7b52f8 commit ad11420
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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 @@ -105,6 +108,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();

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

return parent::prepare($statement);
}
Expand Down Expand Up @@ -265,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 Down Expand Up @@ -515,6 +519,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 ad11420

Please sign in to comment.