-
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?
Changes from all commits
64a6cc4
63bcdfd
d931ad0
dceddd7
edc39b9
de3c09c
69035c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,15 @@ | |
|
||
namespace Laravel\Nightwatch\Sensors; | ||
|
||
use Illuminate\Database\Connection; | ||
use Illuminate\Database\Events\QueryExecuted; | ||
use Laravel\Nightwatch\Clock; | ||
use Laravel\Nightwatch\Location; | ||
use Laravel\Nightwatch\Records\Query; | ||
use Laravel\Nightwatch\State\CommandState; | ||
use Laravel\Nightwatch\State\RequestState; | ||
use Laravel\Nightwatch\Types\Str; | ||
use PDO; | ||
|
||
use function hash; | ||
use function in_array; | ||
|
@@ -46,6 +48,7 @@ public function __invoke(QueryExecuted $event, array $trace): array | |
line: $line ?? 0, | ||
duration: $durationInMicroseconds, | ||
connection: $event->connectionName ?? '', // @phpstan-ignore nullCoalesce.property | ||
connectionType: $this->connectionType($event) ?? '', | ||
), | ||
function () use ($event, $record) { | ||
$this->executionState->queries++; | ||
|
@@ -68,6 +71,7 @@ function () use ($event, $record) { | |
'line' => $record->line, | ||
'duration' => $record->duration, | ||
'connection' => Str::tinyText($record->connection), | ||
'connection_type' => $record->connectionType, | ||
]; | ||
}, | ||
]; | ||
|
@@ -87,4 +91,34 @@ private function hash(QueryExecuted $event, Query $record): string | |
|
||
return hash('xxh128', "{$record->connection},{$sql}"); | ||
} | ||
|
||
/** | ||
* Get the read or write connection type if configured. | ||
* | ||
* @return 'read'|'write'|null | ||
*/ | ||
private function connectionType(QueryExecuted $event): ?string | ||
{ | ||
$connection = $event->connection; | ||
$readPdo = $connection->getRawReadPdo(); | ||
$writePdo = $connection->getRawPdo(); | ||
|
||
if ($readPdo === null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return null; | ||
} | ||
|
||
if (! $readPdo instanceof PDO) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When |
||
return 'write'; | ||
} | ||
|
||
if (! $writePdo instanceof PDO) { | ||
return 'read'; | ||
} | ||
|
||
if ($connection->getReadPdo() === $writePdo) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return 'write'; | ||
} | ||
|
||
return 'read'; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
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 ??