feat: add request id as comment to all queries

Signed-off-by: Robin Appelman <robin@icewind.nl>
pull/44884/head
Robin Appelman 1 month ago
parent a86c1131d7
commit 8c10c78099
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB

@ -159,6 +159,13 @@ $CONFIG = [
['user' => 'nextcloud', 'password' => 'password2', 'host' => 'replica2', 'dbname' => ''], ['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 whether the Nextcloud instance was installed successfully; ``true``
* indicates a successful installation, and ``false`` indicates an unsuccessful * indicates a successful installation, and ``false`` indicates an unsuccessful
@ -1965,7 +1972,7 @@ $CONFIG = [
/** /**
* Blacklist characters from being used in filenames. This is useful if you * 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. * have a filesystem or OS which does not support certain characters like windows.
* *
* The '/' and '\' characters are always forbidden. * The '/' and '\' characters are always forbidden.
* *
* Example for windows systems: ``array('?', '<', '>', ':', '*', '|', '"', chr(0), "\n", "\r")`` * Example for windows systems: ``array('?', '<', '>', ':', '*', '|', '"', chr(0), "\n", "\r")``
@ -2311,7 +2318,7 @@ $CONFIG = [
/** /**
* Timeout for the login form, after this time the login form is reset. * 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. * 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. * Default is 5 minutes (300 seconds), a value of 0 means no timeout.
*/ */
'login_form_timeout' => 300, 'login_form_timeout' => 300,

@ -91,6 +91,9 @@ class Connection extends PrimaryReadReplicaConnection {
/** @var array<string, int> */ /** @var array<string, int> */
protected $tableDirtyWrites = []; protected $tableDirtyWrites = [];
protected bool $logRequestId;
protected string $requestId;
/** /**
* Initializes a new instance of the Connection class. * Initializes a new instance of the Connection class.
* *
@ -119,6 +122,9 @@ class Connection extends PrimaryReadReplicaConnection {
$this->clock = Server::get(ClockInterface::class); $this->clock = Server::get(ClockInterface::class);
$this->logger = Server::get(LoggerInterface::class); $this->logger = 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 */ /** @var \OCP\Profiler\IProfiler */
$profiler = Server::get(IProfiler::class); $profiler = Server::get(IProfiler::class);
if ($profiler->isEnabled()) { if ($profiler->isEnabled()) {
@ -249,8 +255,7 @@ class Connection extends PrimaryReadReplicaConnection {
$platform = $this->getDatabasePlatform(); $platform = $this->getDatabasePlatform();
$sql = $platform->modifyLimitQuery($sql, $limit, $offset); $sql = $platform->modifyLimitQuery($sql, $limit, $offset);
} }
$statement = $this->replaceTablePrefix($sql); $statement = $this->finishQuery($sql);
$statement = $this->adapter->fixupStatement($statement);
return parent::prepare($statement); return parent::prepare($statement);
} }
@ -307,8 +312,7 @@ class Connection extends PrimaryReadReplicaConnection {
$this->ensureConnectedToPrimary(); $this->ensureConnectedToPrimary();
} }
$sql = $this->replaceTablePrefix($sql); $sql = $this->finishQuery($sql);
$sql = $this->adapter->fixupStatement($sql);
$this->queriesExecuted++; $this->queriesExecuted++;
$this->logQueryToFile($sql); $this->logQueryToFile($sql);
return parent::executeQuery($sql, $params, $types, $qcp); return parent::executeQuery($sql, $params, $types, $qcp);
@ -328,8 +332,7 @@ class Connection extends PrimaryReadReplicaConnection {
* @throws Exception * @throws Exception
*/ */
public function executeUpdate(string $sql, array $params = [], array $types = []): int { public function executeUpdate(string $sql, array $params = [], array $types = []): int {
$sql = $this->replaceTablePrefix($sql); $sql = $this->finishQuery($sql);
$sql = $this->adapter->fixupStatement($sql);
$this->queriesExecuted++; $this->queriesExecuted++;
$this->logQueryToFile($sql); $this->logQueryToFile($sql);
return parent::executeUpdate($sql, $params, $types); return parent::executeUpdate($sql, $params, $types);
@ -354,8 +357,7 @@ class Connection extends PrimaryReadReplicaConnection {
foreach ($tables as $table) { foreach ($tables as $table) {
$this->tableDirtyWrites[$table] = $this->clock->now()->getTimestamp(); $this->tableDirtyWrites[$table] = $this->clock->now()->getTimestamp();
} }
$sql = $this->replaceTablePrefix($sql); $sql = $this->finishQuery($sql);
$sql = $this->adapter->fixupStatement($sql);
$this->queriesExecuted++; $this->queriesExecuted++;
$this->logQueryToFile($sql); $this->logQueryToFile($sql);
return (int)parent::executeStatement($sql, $params, $types); return (int)parent::executeStatement($sql, $params, $types);
@ -587,6 +589,16 @@ class Connection extends PrimaryReadReplicaConnection {
return $schema->tablesExist([$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 // internal use
/** /**
* @param string $statement * @param string $statement

Loading…
Cancel
Save