Skip to content

FOUR-20464 view server timing data in LogRocket #7825

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 2 commits into from
Dec 13, 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
13 changes: 11 additions & 2 deletions ProcessMaker/Http/Middleware/ServerTimingMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use ProcessMaker\Providers\ProcessMakerServiceProvider;
use Symfony\Component\HttpFoundation\Response;

class ServerTimingMiddleware
{
// Minimum time in ms to include a package in the Server-Timing header
private static $minPackageTime;

public function __construct()
{
self::$minPackageTime = config('app.server_timing.min_package_time');
}
/**
* Handle an incoming request.
*
Expand Down Expand Up @@ -41,7 +47,10 @@ public function handle(Request $request, Closure $next): Response
foreach ($packageTimes as $package => $timing) {
$time = ($timing['end'] - $timing['start']) * 1000;

$serverTiming[] = "{$package};dur={$time}";
// Only include packages that took more than MIN_PACKAGE_TIME ms
if ($time > self::$minPackageTime) {
$serverTiming[] = "{$package};dur={$time}";
}
}

// Add Server-Timing headers
Expand Down
17 changes: 14 additions & 3 deletions ProcessMaker/Providers/ProcessMakerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Notifications\Events\NotificationSent;
use Illuminate\Support\Facades;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\URL;
use Laravel\Dusk\DuskServiceProvider;
use Laravel\Horizon\Horizon;
Expand Down Expand Up @@ -403,11 +404,16 @@ public static function getQueryTime(): float
/**
* Set the boot time for service providers.
*
* @param string $package
* @param float $time
*/
public static function setPackageBootStart(string $package, $time): void
public static function setPackageBootStart(string $package, float $time): void
{
$package = ucfirst(\Str::camel(str_replace(['ProcessMaker\Packages\\', '\\'], '', $package)));
if ($time < 0) {
Log::info("Server Timing: Invalid boot time for package: {$package}, time: {$time}");

$time = 0;
}

self::$packageBootTiming[$package] = [
'start' => $time,
Expand All @@ -418,11 +424,16 @@ public static function setPackageBootStart(string $package, $time): void
/**
* Set the boot time for service providers.
*
*
* @param float $time
*/
public static function setPackageBootedTime(string $package, $time): void
{
$package = ucfirst(\Str::camel(str_replace(['ProcessMaker\Packages\\', '\\'], '', $package)));
if (!isset(self::$packageBootTiming[$package]) || $time < 0) {
Log::info("Server Timing: Invalid booted time for package: {$package}, time: {$time}");

return;
}

self::$packageBootTiming[$package]['end'] = $time;
}
Expand Down
24 changes: 18 additions & 6 deletions ProcessMaker/Traits/PluginServiceProviderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,36 @@ public function __construct($app)
{
parent::__construct($app);

$this->booting(function () {
self::$bootStart = microtime(true);
$package = $this->getPackageName();

$package = defined('static::name') ? static::name : $this::class;
$this->booting(function () use ($package) {
self::$bootStart = microtime(true);

ProcessMakerServiceProvider::setPackageBootStart($package, self::$bootStart);
});

$this->booted(function () {
$this->booted(function () use ($package) {
self::$bootTime = microtime(true);

$package = defined('static::name') ? static::name : $this::class;

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

}

/**
* Get the package name for the Server Timing header
*
* @return string
*/
protected function getPackageName(): string
{
if (defined('static::name')) {
return ucfirst(\Str::camel(static::name));
}

return substr(static::class, strrpos(static::class, '\\') + 1);
}

/**
* Boot the PM plug-in.
*/
Expand Down
6 changes: 5 additions & 1 deletion config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@
// Process Request security log rate limit: 1 per day (86400 seconds)
'process_request_errors_rate_limit' => env('PROCESS_REQUEST_ERRORS_RATE_LIMIT', 1),
'process_request_errors_rate_limit_duration' => env('PROCESS_REQUEST_ERRORS_RATE_LIMIT_DURATION', 86400),

'default_colors' => [
'primary' => '#2773F3',
'secondary' => '#728092',
Expand All @@ -266,4 +266,8 @@
'vault_token' => env('ENCRYPTED_DATA_VAULT_TOKEN', ''),
'vault_transit_key' => env('ENCRYPTED_DATA_VAULT_TRANSIT_KEY', ''),
],

'server_timing' => [
'min_package_time' => env('SERVER_TIMING_MIN_PACKAGE_TIME', 5), // Minimum time in milliseconds
],
];