From 1e4d1ffdaa1709130b75d778db60be200e3c04d5 Mon Sep 17 00:00:00 2001 From: Aryeh Raber Date: Thu, 18 Jan 2024 09:56:37 +0100 Subject: [PATCH 1/4] Add `average` metric to SlowJobs --- resources/views/livewire/slow-jobs.blade.php | 10 ++++++++++ src/Livewire/SlowJobs.php | 4 +++- src/Recorders/SlowJobs.php | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/resources/views/livewire/slow-jobs.blade.php b/resources/views/livewire/slow-jobs.blade.php index a84cb65c..55a647f4 100644 --- a/resources/views/livewire/slow-jobs.blade.php +++ b/resources/views/livewire/slow-jobs.blade.php @@ -13,6 +13,7 @@ label="Sort by" :options="[ 'slowest' => 'slowest', + 'average' => 'average', 'count' => 'count', ]" @change="loading = true" @@ -29,11 +30,13 @@ + Job Count + Average Slowest @@ -53,6 +56,13 @@ {{ number_format($job->count) }} @endif + + @if ($job->average === null) + Unknown + @else + {{ number_format($job->average) ?: '<1' }} ms + @endif + @if ($job->slowest === null) Unknown diff --git a/src/Livewire/SlowJobs.php b/src/Livewire/SlowJobs.php index 492ba399..209148cc 100644 --- a/src/Livewire/SlowJobs.php +++ b/src/Livewire/SlowJobs.php @@ -31,14 +31,16 @@ public function render(): Renderable [$slowJobs, $time, $runAt] = $this->remember( fn () => $this->aggregate( 'slow_job', - ['max', 'count'], + ['max', 'avg', 'count'], match ($this->orderBy) { 'count' => 'count', + 'average' => 'avg', default => 'max', }, )->map(fn ($row) => (object) [ 'job' => $row->key, 'slowest' => $row->max, + 'average' => $row->avg, 'count' => $row->count, ]), $this->orderBy, diff --git a/src/Recorders/SlowJobs.php b/src/Recorders/SlowJobs.php index e548bb93..5eae00f8 100644 --- a/src/Recorders/SlowJobs.php +++ b/src/Recorders/SlowJobs.php @@ -86,7 +86,7 @@ public function record(JobReleasedAfterException|JobFailed|JobProcessed|JobProce key: $name, value: $duration, timestamp: $timestamp, - )->max()->count(); + )->max()->avg()->count(); }); } } From a97f4445471fdd7f82ddb72ca571eb2971ab0c49 Mon Sep 17 00:00:00 2001 From: Aryeh Raber Date: Fri, 26 Jan 2024 10:03:20 +0100 Subject: [PATCH 2/4] Record all averages --- config/pulse.php | 1 + src/Recorders/SlowJobs.php | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/config/pulse.php b/config/pulse.php index 68ee12b4..bd928663 100644 --- a/config/pulse.php +++ b/config/pulse.php @@ -171,6 +171,7 @@ 'enabled' => env('PULSE_SLOW_JOBS_ENABLED', true), 'sample_rate' => env('PULSE_SLOW_JOBS_SAMPLE_RATE', 1), 'threshold' => env('PULSE_SLOW_JOBS_THRESHOLD', 1000), + 'average' => env('PULSE_SLOW_JOBS_AVERAGE', true), 'ignore' => [ // '/^Package\\\\Jobs\\\\/', ], diff --git a/src/Recorders/SlowJobs.php b/src/Recorders/SlowJobs.php index 5eae00f8..f2e1db77 100644 --- a/src/Recorders/SlowJobs.php +++ b/src/Recorders/SlowJobs.php @@ -73,11 +73,22 @@ public function record(JobReleasedAfterException|JobFailed|JobProcessed|JobProce ]; $this->pulse->lazy(function () use ($timestamp, $timestampMs, $name, $lastJobStartedProcessingAt) { - if ( - $this->underThreshold($duration = $timestampMs - $lastJobStartedProcessingAt) || - ! $this->shouldSample() || - $this->shouldIgnore($name) - ) { + if (! $this->shouldSample() || $this->shouldIgnore($name)) { + return; + } + + $duration = $timestampMs - $lastJobStartedProcessingAt; + + if ($this->config->get('pulse.recorders.'.self::class.'.average')) { + $this->pulse->record( + type: 'slow_job', + key: $name, + value: $duration, + timestamp: $timestamp, + )->avg(); + } + + if ($this->underThreshold($duration)) { return; } @@ -86,7 +97,7 @@ public function record(JobReleasedAfterException|JobFailed|JobProcessed|JobProce key: $name, value: $duration, timestamp: $timestamp, - )->max()->avg()->count(); + )->max()->count(); }); } } From 1dca9b09de7033b5ce07a9203e071c3acffb2997 Mon Sep 17 00:00:00 2001 From: Aryeh Raber Date: Fri, 26 Jan 2024 10:03:35 +0100 Subject: [PATCH 3/4] Conditionally show `Average` column --- resources/views/livewire/slow-jobs.blade.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/resources/views/livewire/slow-jobs.blade.php b/resources/views/livewire/slow-jobs.blade.php index 55a647f4..dcfceda6 100644 --- a/resources/views/livewire/slow-jobs.blade.php +++ b/resources/views/livewire/slow-jobs.blade.php @@ -29,14 +29,18 @@ + @if ($config['average'] === true) + @endif Job Count + @if ($config['average'] === true) Average + @endif Slowest @@ -56,6 +60,7 @@ {{ number_format($job->count) }} @endif + @if ($config['average'] === true) @if ($job->average === null) Unknown @@ -63,6 +68,7 @@ {{ number_format($job->average) ?: '<1' }} ms @endif + @endif @if ($job->slowest === null) Unknown From 27e6104c2bd84e69c926bbbfcd7cb19c39d40c36 Mon Sep 17 00:00:00 2001 From: Aryeh Raber Date: Fri, 26 Jan 2024 10:04:00 +0100 Subject: [PATCH 4/4] Reject rows without a `max` value --- src/Livewire/SlowJobs.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Livewire/SlowJobs.php b/src/Livewire/SlowJobs.php index 209148cc..b337abe9 100644 --- a/src/Livewire/SlowJobs.php +++ b/src/Livewire/SlowJobs.php @@ -37,7 +37,9 @@ public function render(): Renderable 'average' => 'avg', default => 'max', }, - )->map(fn ($row) => (object) [ + ) + ->reject(fn ($row) => is_null($row->max)) + ->map(fn ($row) => (object) [ 'job' => $row->key, 'slowest' => $row->max, 'average' => $row->avg,