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/resources/views/livewire/slow-jobs.blade.php b/resources/views/livewire/slow-jobs.blade.php
index a84cb65c..dcfceda6 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"
@@ -28,12 +29,18 @@
+ @if ($config['average'] === true)
+
+ @endif
Job
Count
+ @if ($config['average'] === true)
+ Average
+ @endif
Slowest
@@ -53,6 +60,15 @@
{{ number_format($job->count) }}
@endif
+ @if ($config['average'] === true)
+
+ @if ($job->average === null)
+ Unknown
+ @else
+ {{ number_format($job->average) ?: '<1' }} ms
+ @endif
+
+ @endif
@if ($job->slowest === null)
Unknown
diff --git a/src/Livewire/SlowJobs.php b/src/Livewire/SlowJobs.php
index 492ba399..b337abe9 100644
--- a/src/Livewire/SlowJobs.php
+++ b/src/Livewire/SlowJobs.php
@@ -31,14 +31,18 @@ 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) [
+ )
+ ->reject(fn ($row) => is_null($row->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..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;
}