Skip to content

Commit 765fdbe

Browse files
committed
Slow threshold highlight on queries
1 parent 83cc125 commit 765fdbe

File tree

5 files changed

+15
-4
lines changed

5 files changed

+15
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
],
1919
"require": {
2020
"php": "^8.1",
21-
"php-debugbar/php-debugbar": "~2.2.0",
21+
"php-debugbar/php-debugbar": "^2.2.4",
2222
"illuminate/routing": "^9|^10|^11|^12",
2323
"illuminate/session": "^9|^10|^11|^12",
2424
"illuminate/support": "^9|^10|^11|^12",

config/debugbar.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@
232232
],
233233
'hints' => env('DEBUGBAR_OPTIONS_DB_HINTS', false), // Show hints for common mistakes
234234
'show_copy' => env('DEBUGBAR_OPTIONS_DB_SHOW_COPY', true), // Show copy button next to the query,
235-
'slow_threshold' => env('DEBUGBAR_OPTIONS_DB_SLOW_THRESHOLD', false), // Only track queries that last longer than this time in ms
235+
'only_slow_queries' => env('DEBUGBAR_OPTIONS_DB_ONLY_SLOW_QUERIES', true), // Only track queries that last longer than `slow_threshold`
236+
'slow_threshold' => env('DEBUGBAR_OPTIONS_DB_SLOW_THRESHOLD', false), // Max query execution time (ms). Exceeding queries will be highlighted
236237
'memory_usage' => env('DEBUGBAR_OPTIONS_DB_MEMORY_USAGE', false), // Show queries memory usage
237238
'soft_limit' => (int) env('DEBUGBAR_OPTIONS_DB_SOFT_LIMIT', 100), // After the soft limit, no parameters/backtrace are captured
238239
'hard_limit' => (int) env('DEBUGBAR_OPTIONS_DB_HARD_LIMIT', 500), // After the hard limit, queries are ignored

src/DataCollector/QueryCollector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ public function collect()
517517
'start' => $query['start'] ?? null,
518518
'duration' => $query['time'],
519519
'duration_str' => ($query['type'] == 'transaction') ? '' : $this->formatDuration($query['time']),
520+
'slow' => $this->slowThreshold && $this->slowThreshold <= $query['time'],
520521
'memory' => $query['memory'],
521522
'memory_str' => $query['memory'] ? $this->getDataFormatter()->formatBytes($query['memory']) : null,
522523
'filename' => $this->getDataFormatter()->formatSource($source, true),

src/LaravelDebugbar.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,11 @@ function (\Illuminate\Log\Events\MessageLogged $log) use ($logger) {
363363
$queryCollector->setLimits($config->get('debugbar.options.db.soft_limit'), $config->get('debugbar.options.db.hard_limit'));
364364
$queryCollector->setDurationBackground($config->get('debugbar.options.db.duration_background'));
365365

366+
$threshold = $config->get('debugbar.options.db.slow_threshold', false);
367+
if ($threshold && !$config->get('debugbar.options.db.only_slow_queries', true)) {
368+
$queryCollector->setSlowThreshold($threshold);
369+
}
370+
366371
if ($config->get('debugbar.options.db.with_params')) {
367372
$queryCollector->setRenderSqlWithParams(true);
368373
}
@@ -402,9 +407,10 @@ function (\Illuminate\Database\Events\QueryExecuted $query) {
402407
return; // Issue 776 : We've turned off collecting after the listener was attached
403408
}
404409

405-
//allow collecting only queries slower than a specified amount of milliseconds
406410
$threshold = app('config')->get('debugbar.options.db.slow_threshold', false);
407-
if (!$threshold || $query->time > $threshold) {
411+
$onlyThreshold = app('config')->get('debugbar.options.db.only_slow_queries', true);
412+
//allow collecting only queries slower than a specified amount of milliseconds
413+
if ((!$onlyThreshold || !$threshold) || ($threshold && $query->time > $threshold)) {
408414
$this['queries']->addQuery($query);
409415
}
410416
}

src/Resources/queries/widget.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,9 @@
231231
.attr('data-duplicate', false)
232232
.append($('<strong />').addClass(csscls('sql name')).text(statement.sql));
233233
} else {
234+
if (statement.slow) {
235+
$li.addClass(csscls('sql-slow'));
236+
}
234237
const $code = $('<code />').html(PhpDebugBar.Widgets.highlight(statement.sql, 'sql')).addClass(csscls('sql')),
235238
duplicated = this.duplicateQueries.has(statement);
236239
$li.attr('data-connection', statement.connection)

0 commit comments

Comments
 (0)