-
Notifications
You must be signed in to change notification settings - Fork 60
Capture query connection type (read or write) #281
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
base: 1.x
Are you sure you want to change the base?
Conversation
8a15fb0
to
9f4f229
Compare
9f4f229
to
d931ad0
Compare
$readPdo = $connection->getRawReadPdo(); | ||
$writePdo = $connection->getRawPdo(); | ||
|
||
if ($readPdo === null) { |
There was a problem hiding this comment.
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.
return null; | ||
} | ||
|
||
if (! $readPdo instanceof PDO) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
/** | ||
* @param 'read'|'write'|'' $connectionType | ||
*/ |
There was a problem hiding this comment.
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'); |
There was a problem hiding this comment.
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
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.