Skip to content

Commit c6ef665

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

File tree

5 files changed

+16
-6
lines changed

5 files changed

+16
-6
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: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,13 @@ 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+
//allow collecting only queries slower than a specified amount of milliseconds (`slow_threshold`)
367+
$onlyThreshold = $config->get('debugbar.options.db.only_slow_queries', true);
368+
$threshold = $config->get('debugbar.options.db.slow_threshold', false);
369+
if (!$onlyThreshold && $threshold) {
370+
$queryCollector->setSlowThreshold($threshold);
371+
}
372+
366373
if ($config->get('debugbar.options.db.with_params')) {
367374
$queryCollector->setRenderSqlWithParams(true);
368375
}
@@ -397,14 +404,12 @@ function (\Illuminate\Log\Events\MessageLogged $log) use ($logger) {
397404

398405
try {
399406
$events->listen(
400-
function (\Illuminate\Database\Events\QueryExecuted $query) {
407+
function (\Illuminate\Database\Events\QueryExecuted $query) use ($threshold, $onlyThreshold) {
401408
if (!app(static::class)->shouldCollect('db', true)) {
402409
return; // Issue 776 : We've turned off collecting after the listener was attached
403410
}
404411

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

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)