Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable29] feat: add request id as comment to all queries #44959

Merged
merged 1 commit into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions config/config.sample.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,13 @@
['user' => 'nextcloud', 'password' => 'password2', 'host' => 'replica2', 'dbname' => ''],
],

/**
* 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 Expand Up @@ -1965,7 +1972,7 @@
/**
* Blacklist characters from being used in filenames. This is useful if you
* have a filesystem or OS which does not support certain characters like windows.
*
*
* The '/' and '\' characters are always forbidden.
*
* Example for windows systems: ``array('?', '<', '>', ':', '*', '|', '"', chr(0), "\n", "\r")``
Expand Down Expand Up @@ -2311,7 +2318,7 @@
/**
* Timeout for the login form, after this time the login form is reset.
* This prevents password leaks on public devices if the user forgots to clear the form.
*
*
* Default is 5 minutes (300 seconds), a value of 0 means no timeout.
*/
'login_form_timeout' => 300,
Expand Down
31 changes: 22 additions & 9 deletions lib/private/DB/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
use OCP\IRequestId;
use OCP\PreConditionNotMetException;
use OCP\Profiler\IProfiler;
use OCP\Server;
use Psr\Clock\ClockInterface;
use Psr\Log\LoggerInterface;
use function in_array;
Expand Down Expand Up @@ -90,6 +91,9 @@ class Connection extends PrimaryReadReplicaConnection {
/** @var array<string, int> */
protected $tableDirtyWrites = [];

protected bool $logRequestId;
protected string $requestId;

/**
* Initializes a new instance of the Connection class.
*
Expand All @@ -115,9 +119,12 @@ public function __construct(
$this->tablePrefix = $params['tablePrefix'];

$this->systemConfig = \OC::$server->getSystemConfig();
$this->clock = \OCP\Server::get(ClockInterface::class);
$this->clock = Server::get(ClockInterface::class);
$this->logger = \OC::$server->get(LoggerInterface::class);

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

/** @var \OCP\Profiler\IProfiler */
$profiler = \OC::$server->get(IProfiler::class);
if ($profiler->isEnabled()) {
Expand Down Expand Up @@ -248,8 +255,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 @@ -306,8 +312,7 @@ public function executeQuery(string $sql, array $params = [], $types = [], ?Quer
$this->ensureConnectedToPrimary();
}

$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 @@ -327,8 +332,7 @@ private function getQueriedTables(string $sql): array {
* @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 @@ -353,8 +357,7 @@ public function executeStatement($sql, array $params = [], array $types = []): i
foreach ($tables as $table) {
$this->tableDirtyWrites[$table] = $this->clock->now()->getTimestamp();
}
$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 @@ -586,6 +589,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
Loading