Skip to content

Commit

Permalink
add horizon collectors
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jun 1, 2023
1 parent af9307f commit 06336dd
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 1 deletion.
6 changes: 6 additions & 0 deletions resources/stubs/PrometheusServiceProvider.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use Spatie\Prometheus\Collectors\Horizon\CurrentMasterSupervisorCollector;
use Spatie\Prometheus\Collectors\Horizon\CurrentProcessesPerQueueCollector;
use Spatie\Prometheus\Collectors\Horizon\CurrentWorkloadCollector;
use Spatie\Prometheus\Collectors\Horizon\FailedJobsPerHourCollector;
use Spatie\Prometheus\Collectors\Horizon\HorizonStatusCollector;
use Spatie\Prometheus\Collectors\Horizon\JobsPerMinuteCollector;
use Spatie\Prometheus\Collectors\Horizon\RecentJobsCollector;
use Spatie\Prometheus\Facades\Prometheus;

class PrometheusServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -35,6 +38,9 @@ class PrometheusServiceProvider extends ServiceProvider
CurrentProcessesPerQueueCollector::class,
CurrentWorkloadCollector::class,
FailedJobsPerHourCollector::class,
HorizonStatusCollector::class,
JobsPerMinuteCollector::class,
RecentJobsCollector::class,
]);

return $this;
Expand Down
22 changes: 21 additions & 1 deletion src/Collectors/Horizon/HorizonStatusCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,32 @@

namespace Spatie\Prometheus\Collectors\Horizon;

use Laravel\Horizon\Contracts\MasterSupervisorRepository;
use Spatie\Prometheus\Collectors\Collector;
use Spatie\Prometheus\Facades\Prometheus;

class HorizonStatusCollector implements Collector
{
protected const INACTIVE = -1;
protected const PAUSED = 0;
protected const RUNNING = 1;


public function register(): void
{
// TODO: Implement register() method.
Prometheus::addGauge('Horizon status')
->helpText('The status of Horizon, -1 = inactive, 0 = paused, 1 = running')
->value(function () {
if (!$masters = app(MasterSupervisorRepository::class)->all()) {
return self::INACTIVE;
}

$isPaused = collect($masters)
->contains(fn($master) => $master->status === 'paused');

return $isPaused
? self::PAUSED
: self::RUNNING;
});
}
}
17 changes: 17 additions & 0 deletions src/Collectors/Horizon/JobsPerMinuteCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Spatie\Prometheus\Collectors\Horizon;

use Laravel\Horizon\Contracts\MetricsRepository;
use Spatie\Prometheus\Collectors\Collector;
use Spatie\Prometheus\Facades\Prometheus;

class JobsPerMinuteCollector implements Collector
{
public function register(): void
{
Prometheus::addGauge('jobs per minute')
->helpText('The number of jobs per minute')
->value(fn() => app(MetricsRepository::class)->jobsProcessedPerMinute());
}
}
17 changes: 17 additions & 0 deletions src/Collectors/Horizon/RecentJobsCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Spatie\Prometheus\Collectors\Horizon;

use Laravel\Horizon\Contracts\JobRepository;
use Spatie\Prometheus\Collectors\Collector;
use Spatie\Prometheus\Facades\Prometheus;

class RecentJobsCollector implements Collector
{
public function register(): void
{
Prometheus::addGauge('Recent jobs')
->helpText('The number of recent jobs')
->value(fn() => app(JobRepository::class)->countRecent());
}
}
9 changes: 9 additions & 0 deletions tests/Collectors/Horizon/HorizonStatusCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Spatie\Prometheus\Collectors\Horizon\HorizonStatusCollector;

it('can register the horizon status collector', function () {
app(HorizonStatusCollector::class)->register();

assertPrometheusResultsMatchesSnapshot();
});
9 changes: 9 additions & 0 deletions tests/Collectors/Horizon/JobsPerMinuteCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Spatie\Prometheus\Collectors\Horizon\JobsPerMinuteCollector;

it('can register the jobs per minute collector', function () {
app(JobsPerMinuteCollector::class)->register();

assertPrometheusResultsMatchesSnapshot();
});
9 changes: 9 additions & 0 deletions tests/Collectors/Horizon/RecentJobsCollectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Spatie\Prometheus\Collectors\Horizon\RecentJobsCollector;

it('can register the recent jobs collector', function () {
app(RecentJobsCollector::class)->register();

assertPrometheusResultsMatchesSnapshot();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# HELP app_horizon_status The status of Horizon, -1 = inactive, 0 = paused, 1 = running
# TYPE app_horizon_status gauge
app_horizon_status -1
# HELP php_info Information about the PHP environment.
# TYPE php_info gauge
php_info{version="8.2.0"} 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# HELP app_jobs_per_minute The number of jobs per minute
# TYPE app_jobs_per_minute gauge
app_jobs_per_minute 0
# HELP php_info Information about the PHP environment.
# TYPE php_info gauge
php_info{version="8.2.0"} 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# HELP app_recent_jobs The number of recent jobs
# TYPE app_recent_jobs gauge
app_recent_jobs 0
# HELP php_info Information about the PHP environment.
# TYPE php_info gauge
php_info{version="8.2.0"} 1

0 comments on commit 06336dd

Please sign in to comment.