Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more data to timeline #1726

Merged
merged 1 commit into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/debugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@
'hard_limit' => 500, // After the hard limit, queries are ignored
],
'mail' => [
'timeline' => false, // Add mails to the timeline
'timeline' => true, // Add mails to the timeline
'show_body' => true,
],
'views' => [
'timeline' => false, // Add the views to the timeline (Experimental)
'timeline' => true, // Add the views to the timeline
'data' => false, // True for all data, 'keys' for only names, false for no parameters.
'group' => 50, // Group duplicate views. Pass value to auto-group, or true/false to force
'exclude_paths' => [ // Add the paths which you don't want to appear in the views
Expand Down
7 changes: 2 additions & 5 deletions src/DataCollector/EventCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class EventCollector extends TimeDataCollector
public function __construct($requestStartTime = null, $collectValues = false)
{
parent::__construct($requestStartTime);
$this->previousTime = microtime(true);
$this->collectValues = $collectValues;
$this->setDataFormatter(new SimpleFormatter());
}
Expand All @@ -32,8 +31,7 @@ public function onWildcardEvent($name = null, $data = [])
$currentTime = microtime(true);

if (! $this->collectValues) {
$this->addMeasure($name, $this->previousTime, $currentTime);
$this->previousTime = $currentTime;
$this->addMeasure($name, $currentTime, $currentTime, [], null, 'Events');

return;
}
Expand Down Expand Up @@ -74,8 +72,7 @@ public function onWildcardEvent($name = null, $data = [])

$params['listeners.' . $i] = $listener;
}
$this->addMeasure($name, $this->previousTime, $currentTime, $params);
$this->previousTime = $currentTime;
$this->addMeasure($name, $currentTime, $currentTime, $params, null, 'Events');
}

public function subscribe(Dispatcher $events)
Expand Down
2 changes: 1 addition & 1 deletion src/DataCollector/QueryCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function addQuery($query)
];

if ($this->timeCollector !== null) {
$this->timeCollector->addMeasure(Str::limit($sql, 100), $startTime, $endTime, [], 'db');
$this->timeCollector->addMeasure(Str::limit($sql, 100), $startTime, $endTime, [], 'db', 'Database Query');
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/DataCollector/ViewCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;
use DebugBar\DataCollector\TimeDataCollector;
use Illuminate\Support\Str;
use Illuminate\View\View;

class ViewCollector extends DataCollector implements Renderable, AssetProvider
Expand All @@ -15,21 +17,24 @@ class ViewCollector extends DataCollector implements Renderable, AssetProvider
protected $collect_data;
protected $exclude_paths;
protected $group;
protected $timeCollector;

/**
* Create a ViewCollector
*
* @param bool|string $collectData Collects view data when true
* @param string[] $excludePaths Paths to exclude from collection
* @param int|bool $group Group the same templates together
* @param TimeDataCollector|null TimeCollector
* */
public function __construct($collectData = true, $excludePaths = [], $group = true)
public function __construct($collectData = true, $excludePaths = [], $group = true, ?TimeDataCollector $timeCollector = null)
{
$this->setDataFormatter(new SimpleFormatter());
$this->collect_data = $collectData;
$this->templates = [];
$this->exclude_paths = $excludePaths;
$this->group = $group;
$this->timeCollector = $timeCollector;
}

public function getName()
Expand Down Expand Up @@ -103,6 +108,11 @@ public function addView(View $view)
}

$this->addTemplate($name, $data, $type, $path);

if ($this->timeCollector !== null) {
$time = microtime(true);
$this->timeCollector->addMeasure('View: ' . $name, $time, $time, [], 'views', 'View');
}
}

private function getInertiaView(string $name, array $data, ?string $path)
Expand Down
27 changes: 24 additions & 3 deletions src/LaravelDebugbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,31 @@ public function boot()
$this['time']->showMemoryUsage();
}

if (! $this->isLumen() && $startTime) {
if ($startTime) {
$app->booted(
function () use ($startTime) {
$this->addMeasure('Booting', $startTime, microtime(true), [], 'time');
}
);
}

$this->startMeasure('application', 'Application', 'time');
if ($events) {
$events->listen(\Illuminate\Routing\Events\Routing::class, function() {
$this->startMeasure('Routing');
});
$events->listen(\Illuminate\Routing\Events\RouteMatched::class, function() {
$this->stopMeasure('Routing');
});

$events->listen(\Illuminate\Routing\Events\PreparingResponse::class, function() {
$this->startMeasure('Preparing Response');
});
$events->listen(\Illuminate\Routing\Events\ResponsePrepared::class, function() {
$this->stopMeasure('Preparing Response');
});
} else {
$this->startMeasure('application', 'Application', 'time');
}
}

if ($this->shouldCollect('memory', true)) {
Expand Down Expand Up @@ -253,7 +269,12 @@ function () use ($startTime) {
$collectData = $config->get('debugbar.options.views.data', true);
$excludePaths = $config->get('debugbar.options.views.exclude_paths', []);
$group = $config->get('debugbar.options.views.group', true);
$this->addCollector(new ViewCollector($collectData, $excludePaths, $group));
if ($this->hasCollector('time') && $config->get('debugbar.options.views.timeline', false)) {
$timeCollector = $this['time'];
} else {
$timeCollector = null;
}
$this->addCollector(new ViewCollector($collectData, $excludePaths, $group, $timeCollector));
$events->listen(
'composing:*',
function ($event, $params) {
Expand Down
36 changes: 0 additions & 36 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,42 +48,6 @@ function ($app) {
}
);

$this->app->extend(
'view',
function (Factory $factory, Container $application): Factory {
$laravelDebugbar = $application->make(LaravelDebugbar::class);

$shouldTrackViewTime = $laravelDebugbar->isEnabled() &&
$laravelDebugbar->shouldCollect('time', true) &&
$laravelDebugbar->shouldCollect('views', true) &&
$application['config']->get('debugbar.options.views.timeline', false);

if (! $shouldTrackViewTime) {
/* Do not swap the engine to save performance */
return $factory;
}

$extensions = array_reverse($factory->getExtensions());
$engines = array_flip($extensions);
$enginesResolver = $application->make('view.engine.resolver');

foreach ($engines as $engine => $extension) {
$resolved = $enginesResolver->resolve($engine);

$factory->addExtension($extension, $engine, function () use ($resolved, $laravelDebugbar) {
return new DebugbarViewEngine($resolved, $laravelDebugbar);
});
}

// returns original order of extensions
foreach ($extensions as $extension => $engine) {
$factory->addExtension($extension, $engine);
}

return $factory;
}
);

Collection::macro('debug', function () {
debug($this);
return $this;
Expand Down
Loading