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 configuration option to enable/disable server timing
  • Loading branch information
devmiguelangel committed Dec 13, 2024
commit 64ba48ba113e08161c4d6effe3fe91f1e9fd2666
4 changes: 4 additions & 0 deletions ProcessMaker/Http/Middleware/ServerTimingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function __construct()
*/
public function handle(Request $request, Closure $next): Response
{
if (!config('app.server_timing.enabled')) {
return $next($request);
}

// Start time for controller execution
$startController = microtime(true);

Expand Down
10 changes: 6 additions & 4 deletions ProcessMaker/Providers/ProcessMakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ public function boot(): void

public function register(): void
{
// Listen to query events and accumulate query execution time
DB::listen(function ($query) {
self::$queryTime += $query->time;
});
if (config('app.server_timing.enabled')) {
// Listen to query events and accumulate query execution time
DB::listen(function ($query) {
self::$queryTime += $query->time;
});
}

// Dusk, if env is appropriate
// TODO Remove Dusk references and remove from composer dependencies
Expand Down
16 changes: 15 additions & 1 deletion ProcessMaker/Traits/PluginServiceProviderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ public function __construct($app)
{
parent::__construct($app);

$this->bootServerTiming();
}

/**
* The `bootServerTiming` function sets up timing measurements for the booting and booted events of the packages
*
* @return void If the condition `config('app.server_timing.enabled')` is false, nothing is being returned as the
* function will exit early.
*/
protected function bootServerTiming(): void
{
if (!config('app.server_timing.enabled')) {
return;
}

$package = $this->getPackageName();

$this->booting(function () use ($package) {
Expand All @@ -43,7 +58,6 @@ public function __construct($app)

ProcessMakerServiceProvider::setPackageBootedTime($package, self::$bootTime);
});

}

/**
Expand Down
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@
],

'server_timing' => [
'enabled' => env('SERVER_TIMING_ENABLED', true),
'min_package_time' => env('SERVER_TIMING_MIN_PACKAGE_TIME', 5), // Minimum time in milliseconds
],
];
18 changes: 18 additions & 0 deletions tests/Feature/ServerTimingMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,22 @@ public function testServerTimingOnLogin()
$this->assertStringContainsString('controller;dur=', $serverTiming[1]);
$this->assertStringContainsString('db;dur=', $serverTiming[2]);
}

public function testServerTimingIfIsDisabled()
{
config(['app.server_timing.enabled' => false]);

Route::middleware(ServerTimingMiddleware::class)->get('/test', function () {
// Simulate a query
DB::select('SELECT SLEEP(1)');

return response()->json(['message' => 'Test endpoint']);
});

// Send a GET request
$response = $this->get('/test');
$response->assertStatus(200);
// Assert the response has not the Server-Timing header
$response->assertHeaderMissing('Server-Timing');
}
}