Skip to content

Conversation

avosalmon
Copy link
Contributor

@avosalmon avosalmon commented Oct 7, 2025

This PR updates QuerySensor to capture whether the query was executed using a read or write connection.
We capture this information only when the read and write connections are configured in the application.

@avosalmon avosalmon force-pushed the capture-connection-type branch from 8a15fb0 to 9f4f229 Compare October 7, 2025 11:20
@avosalmon avosalmon force-pushed the capture-connection-type branch from 9f4f229 to d931ad0 Compare October 8, 2025 03:54
@avosalmon avosalmon changed the title Capture whether the query was executed using the read connection Capture read/write connection type Oct 8, 2025
$readPdo = $connection->getRawReadPdo();
$writePdo = $connection->getRawPdo();

if ($readPdo === null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$readPdo is null when a read connection is not configured.

@avosalmon avosalmon marked this pull request as ready for review October 8, 2025 06:53
return null;
}

if (! $readPdo instanceof PDO) {
Copy link
Contributor Author

@avosalmon avosalmon Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When $readPdo is not null and not an instance of PDO, that means that the read connection has not been established. At this point, $readPdo is a closure to resolve a PDO.

return 'read';
}

if ($connection->getReadPdo() === $writePdo) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$connection->getReadPdo() returns a write PDO when a sticky connection is active or it's forced to use the write connection.

https://github.com/laravel/framework/blob/da58855c8069dbdd8f8f7c9e3c332360f9a231f8/src/Illuminate/Database/Connection.php#L1246-L1267

@avosalmon avosalmon changed the title Capture read/write connection type Capture query connection type (read or write) Oct 8, 2025
Comment on lines +7 to +9
/**
* @param 'read'|'write'|'' $connectionType
*/
Copy link
Member

@timacdonald timacdonald Oct 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a user API perspective in PHP land, I wonder if null would be a better value, rather than an empty string, to indicate that it is not configured.

Going further, I also wonder if this should be an enum (which rules out null)?

ConnectionType::Read;
ConnectionType::Write;
ConnectionType::NotConfigured; // dunno about this ??

$response->assertOk();
$ingest->assertWrittenTimes(1);
$ingest->assertLatestWrite('query:0.connection_type', 'read');
$ingest->assertLatestWrite('query:0.connection_type', 'write');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails because DB::selectFromWriteConnection doesn't update readOnWriteConnection.
https://github.com/laravel/framework/blob/975d60a5a38d75ec00f219058ad8495877058b82/src/Illuminate/Database/Connection.php#L375-L385

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants