From 9bc32e7929f5db65259304910aa6eb5dc920b387 Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Wed, 11 Oct 2023 13:29:01 +1000 Subject: [PATCH] Fix issue with SQL and URI truncation --- .../migrations/2023_06_07_000001_create_pulse_tables.php | 8 +++++--- src/Queries/SlowOutgoingRequests.php | 4 ++-- src/Queries/SlowQueries.php | 4 ++-- tests/Feature/OutgoingRequestsTest.php | 6 +++--- tests/Feature/SlowQueriesTest.php | 2 +- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/database/migrations/2023_06_07_000001_create_pulse_tables.php b/database/migrations/2023_06_07_000001_create_pulse_tables.php index 9735e112d..c43403df8 100644 --- a/database/migrations/2023_06_07_000001_create_pulse_tables.php +++ b/database/migrations/2023_06_07_000001_create_pulse_tables.php @@ -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) { @@ -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? diff --git a/src/Queries/SlowOutgoingRequests.php b/src/Queries/SlowOutgoingRequests.php index 660483a28..ded19b621 100644 --- a/src/Queries/SlowOutgoingRequests.php +++ b/src/Queries/SlowOutgoingRequests.php @@ -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(); } diff --git a/src/Queries/SlowQueries.php b/src/Queries/SlowQueries.php index e100b1394..7cc540d90 100644 --- a/src/Queries/SlowQueries.php +++ b/src/Queries/SlowQueries.php @@ -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(); } diff --git a/tests/Feature/OutgoingRequestsTest.php b/tests/Feature/OutgoingRequestsTest.php index 86d0abf98..0ed27bdab 100644 --- a/tests/Feature/OutgoingRequestsTest.php +++ b/tests/Feature/OutgoingRequestsTest.php @@ -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', @@ -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', @@ -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', diff --git a/tests/Feature/SlowQueriesTest.php b/tests/Feature/SlowQueriesTest.php index e6d8552b0..5cf8da570 100644 --- a/tests/Feature/SlowQueriesTest.php +++ b/tests/Feature/SlowQueriesTest.php @@ -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',