Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
feat: add server timing header for service provider time execution
  • Loading branch information
devmiguelangel committed Dec 10, 2024
commit 625e5d994408d33f82d05d3b54b3f02bdded792b
9 changes: 7 additions & 2 deletions ProcessMaker/Http/Middleware/ServerTimingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use ProcessMaker\Providers\ProcessMakerServiceProvider;
use Symfony\Component\HttpFoundation\Response;

class ServerTimingMiddleware
Expand Down Expand Up @@ -36,11 +37,15 @@ public function handle(Request $request, Closure $next): Response
$controllerTime = (microtime(true) - $startController) * 1000; // Convert to ms
$endpointTime = (microtime(true) - $startEndpoint) * 1000; // Convert to ms

// Add Server-Timing header
// Fetch service provider boot time
$serviceProviderTime = ProcessMakerServiceProvider::getBootTime() ?? 0;

// Add Server-Timing headers
$response->headers->set('Server-Timing', [
"providers;dur={$serviceProviderTime}",
"endpoint;dur={$endpointTime}",
"controller;dur={$controllerTime}",
"db;dur={$queryTime}"
"db;dur={$queryTime}",
]);

return $response;
Expand Down
21 changes: 21 additions & 0 deletions ProcessMaker/Providers/ProcessMakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
*/
class ProcessMakerServiceProvider extends ServiceProvider
{
// Track the start time for service providers boot
private static $bootStart;
// Track the boot time for service providers
private static $bootTime;

public function boot(): void
{
$this->app->singleton(Menu::class, function ($app) {
Expand All @@ -52,10 +57,16 @@ public function boot(): void
$this->setupFactories();

parent::boot();

// Hook after service providers boot
self::$bootTime = (microtime(true) - self::$bootStart) * 1000; // Convert to milliseconds
}

public function register(): void
{
// Track the start time for service providers boot
self::$bootStart = microtime(true);

// Dusk, if env is appropriate
// TODO Remove Dusk references and remove from composer dependencies
if (!$this->app->environment('production')) {
Expand Down Expand Up @@ -358,4 +369,14 @@ public static function forceHttps(): void
URL::forceScheme('https');
}
}

/**
* Get the boot time for service providers.
*
* @return float|null
*/
public static function getBootTime(): ?float
{
return self::$bootTime;
}
}