Skip to content

FOUR-20465 ensure out-of-the-box server timing header support #7827

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

Merged
merged 1 commit into from
Dec 16, 2024
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: 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');
}
}
Loading