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 13, 2023
1 parent 7cd5b64 commit c36e42f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 9 deletions.
10 changes: 6 additions & 4 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,10 +88,11 @@ 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();
$table->index(['uri', 'date', 'duration']);
$table->index(['uri_hash', 'date', 'duration']);
});

Schema::create('pulse_queue_sizes', function (Blueprint $table) {
Expand Down
4 changes: 2 additions & 2 deletions src/Queries/SlowOutgoingRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,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.recorders.'.OutgoingRequests::class.'.threshold'))
->groupBy('uri')
->groupBy('uri_hash')
->orderByDesc('slowest')
->limit(101)
->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
2 changes: 1 addition & 1 deletion tests/Feature/SlowQueriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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($queries[0])->toHaveProperties([
'date' => '2000-01-02 03:04:00',
'user_id' => null,
'sql' => 'select * from users',
Expand Down

0 comments on commit c36e42f

Please sign in to comment.