Skip to content

Commit

Permalink
Fix issue with SQL and URI truncation
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Oct 11, 2023
1 parent a7196bf commit 9bc32e7
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
8 changes: 5 additions & 3 deletions database/migrations/2023_06_07_000001_create_pulse_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ public function up(): void
Schema::create('pulse_slow_queries', function (Blueprint $table) {
$table->datetime('date');
$table->string('user_id')->nullable();
$table->string('sql');
$table->text('sql');
$table->char('sql_hash', 16)->charset('binary')->virtualAs('UNHEX(MD5(`sql`))');
$table->unsignedInteger('duration');

$table->index(['date', 'sql', 'duration']); // slow_queries
$table->index(['date', 'sql_hash', 'duration']); // slow_queries
});

Schema::create('pulse_jobs', function (Blueprint $table) {
Expand All @@ -87,7 +88,8 @@ public function up(): void

Schema::create('pulse_outgoing_requests', function (Blueprint $table) {
$table->datetime('date');
$table->string('uri');
$table->text('uri');
$table->char('uri_hash', 16)->charset('binary')->virtualAs('UNHEX(MD5(`uri`))');
$table->unsignedInteger('duration');
$table->string('user_id')->nullable();
// TODO: indexes?
Expand Down
4 changes: 2 additions & 2 deletions src/Queries/SlowOutgoingRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public function __invoke(Interval $interval): Collection
$now = new CarbonImmutable;

return $this->connection()->table('pulse_outgoing_requests')
->selectRaw('`uri`, COUNT(*) as count, MAX(duration) AS slowest')
->selectRaw('MAX(uri) AS uri, COUNT(*) AS count, MAX(duration) AS slowest')
->where('date', '>=', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->where('duration', '>=', $this->config->get('pulse.slow_outgoing_request_threshold'))
->groupBy('uri')
->groupBy('uri_hash')
->orderByDesc('slowest')
->get();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Queries/SlowQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function __invoke(Interval $interval): Collection
$now = new CarbonImmutable;

return $this->connection()->table('pulse_slow_queries')
->selectRaw('`sql`, COUNT(*) as count, MAX(duration) AS slowest')
->selectRaw('MAX(`sql`) AS `sql`, COUNT(*) AS count, MAX(duration) AS slowest')
->where('date', '>=', $now->subSeconds((int) $interval->totalSeconds)->toDateTimeString())
->groupBy('sql')
->groupBy('sql_hash')
->orderByDesc('slowest')
->get();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/OutgoingRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
expect(Pulse::entries())->toHaveCount(0);
$requests = Pulse::ignore(fn () => DB::table('pulse_outgoing_requests')->get());
expect($requests)->toHaveCount(1);
expect((array) $requests[0])->toEqual([
expect((array) $requests[0])->toMatchArray([
'date' => '2000-01-02 03:04:05',
'user_id' => null,
'uri' => 'GET https://laravel.com',
Expand All @@ -47,7 +47,7 @@

$requests = Pulse::ignore(fn () => DB::table('pulse_outgoing_requests')->get());
expect($requests)->toHaveCount(1);
expect((array) $requests[0])->toEqual([
expect((array) $requests[0])->toMatchArray([
'date' => '2000-01-02 03:04:05',
'user_id' => null,
'uri' => 'GET https://laravel.com',
Expand All @@ -64,7 +64,7 @@

$requests = Pulse::ignore(fn () => DB::table('pulse_outgoing_requests')->get());
expect($requests)->toHaveCount(1);
expect((array) $requests[0])->toEqual([
expect((array) $requests[0])->toMatchArray([
'date' => '2000-01-02 03:04:05',
'user_id' => null,
'uri' => 'GET https://laravel.com',
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/SlowQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
expect(Pulse::entries())->toHaveCount(0);
$queries = Pulse::ignore(fn () => DB::table('pulse_slow_queries')->get());
expect($queries)->toHaveCount(1);
expect((array) $queries[0])->toEqual([
expect((array) $queries[0])->toMatchArray([
'date' => '2000-01-02 03:04:00',
'user_id' => null,
'sql' => 'select * from users',
Expand Down

0 comments on commit 9bc32e7

Please sign in to comment.