Skip to content

Commit

Permalink
Conditionally take snapshots into account
Browse files Browse the repository at this point in the history
  • Loading branch information
rennokki committed Sep 25, 2021
1 parent fb60a8a commit 215754e
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 7 deletions.
21 changes: 21 additions & 0 deletions config/horizon-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,25 @@

'middleware' => ['web'],

/*
|--------------------------------------------------------------------------
| Include Past Snapshots
|--------------------------------------------------------------------------
|
| If you periodically run the horizon:snapshot command, it will reset
| the values exposed to Prometheus to 0, this leading to Prometheus
| reading counters starting from 0 each time it is ran.
|
| You can prevent that by enabling this setting. On the current values
| for throughput, jobs, etc., the values from snapshots will also be
| included. This may lead to graphs showing more jobs if you enable
| this after you had it disabled.
|
| It is recommended to keep it enabled, or to disable it and
| make sure the horizon:snapshot is never called.
|
*/

'include_snapshots' => (bool) env('HORIZON_EXPORTER_SNAPSHOTS', true),

];
6 changes: 5 additions & 1 deletion src/Metrics/JobsRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ public function update(): void
{
if ($jobs = app(MetricsRepository::class)->measuredJobs()) {
foreach ($jobs as $job) {
$value = collect(config('horizon-exporter.include_snapshots') ? app(MetricsRepository::class)->snapshotsForJob($job) : [])
->merge([(object) ['runtime' => app(MetricsRepository::class)->runtimeForJob($job)]])
->average('runtime');

$this->collector->set(
value: app(MetricsRepository::class)->runtimeForJob($job),
value: $value,
labels: [
'job' => $job,
],
Expand Down
10 changes: 9 additions & 1 deletion src/Metrics/JobsThroughput.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ public function update(): void
{
if ($jobs = app(MetricsRepository::class)->measuredJobs()) {
foreach ($jobs as $job) {
$value = app(MetricsRepository::class)->throughputForJob($job);

if (config('horizon-exporter.include_snapshots')) {
foreach (app(MetricsRepository::class)->snapshotsForJob($job) as $snapshot) {
$value += $snapshot->throughput;
}
}

$this->collector->set(
value: app(MetricsRepository::class)->throughputForJob($job),
value: $value,
labels: [
'job' => $job,
],
Expand Down
6 changes: 5 additions & 1 deletion src/Metrics/QueuesRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ public function update(): void
{
if ($queues = app(MetricsRepository::class)->measuredQueues()) {
foreach ($queues as $queue) {
$value = collect(config('horizon-exporter.include_snapshots') ? app(MetricsRepository::class)->snapshotsForQueue($queue) : [])
->merge([(object) ['runtime' => app(MetricsRepository::class)->runtimeForQueue($queue)]])
->average('runtime');

$this->collector->set(
value: app(MetricsRepository::class)->runtimeForQueue($queue),
value: $value,
labels: [
'queue' => $queue,
],
Expand Down
10 changes: 9 additions & 1 deletion src/Metrics/QueuesThroughput.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ public function update(): void
{
if ($queues = app(MetricsRepository::class)->measuredQueues()) {
foreach ($queues as $queue) {
$value = app(MetricsRepository::class)->throughputForQueue($queue);

if (config('horizon-exporter.include_snapshots')) {
foreach (app(MetricsRepository::class)->snapshotsForQueue($queue) as $snapshot) {
$value += $snapshot->throughput;
}
}

$this->collector->set(
value: app(MetricsRepository::class)->throughputForQueue($queue),
value: $value,
labels: [
'queue' => $queue,
],
Expand Down
51 changes: 48 additions & 3 deletions tests/MetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Queue;
use Laravel\Horizon\Contracts\HorizonCommandQueue;
use Laravel\Horizon\Contracts\MasterSupervisorRepository;
use Laravel\Horizon\Contracts\MetricsRepository;
use Laravel\Horizon\Contracts\SupervisorRepository;
use Laravel\Horizon\MasterSupervisorCommands\AddSupervisor;
use Laravel\Horizon\Supervisor;
Expand Down Expand Up @@ -71,10 +72,20 @@ public function test_queues_throughput_metric()
$this->work();
$this->work();

resolve(MetricsRepository::class)->snapshot();

Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);

$this->work();
$this->work();
$this->work();

$response = HorizonExporter::exportAsPlainText();

$this->assertStringContainsString(
'horizon_queue_throughput{queue="default"} 3',
'horizon_queue_throughput{queue="default"} 6',
$response,
);
}
Expand All @@ -95,15 +106,27 @@ public function test_jobs_throughput_metric()
$this->work();
$this->work();

resolve(MetricsRepository::class)->snapshot();

Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob2);

$this->work();
$this->work();
$this->work();
$this->work();

$response = HorizonExporter::exportAsPlainText();

$this->assertStringContainsString(
'horizon_job_throughput{job="RenokiCo\\\\HorizonExporter\\\\Test\\\\Jobs\\\\BasicJob"} 3',
'horizon_job_throughput{job="RenokiCo\\\\HorizonExporter\\\\Test\\\\Jobs\\\\BasicJob"} 6',
$response,
);

$this->assertStringContainsString(
'horizon_job_throughput{job="RenokiCo\\\\HorizonExporter\\\\Test\\\\Jobs\\\\BasicJob2"} 1',
'horizon_job_throughput{job="RenokiCo\\\\HorizonExporter\\\\Test\\\\Jobs\\\\BasicJob2"} 2',
$response,
);
}
Expand All @@ -122,6 +145,16 @@ public function test_queues_runtime_metric()
$this->work();
$this->work();

resolve(MetricsRepository::class)->snapshot();

Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);

$this->work();
$this->work();
$this->work();

$response = HorizonExporter::exportAsPlainText();

$this->assertStringContainsString(
Expand All @@ -146,6 +179,18 @@ public function test_jobs_runtime_metric()
$this->work();
$this->work();

resolve(MetricsRepository::class)->snapshot();

Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob);
Queue::push(new Jobs\BasicJob2);

$this->work();
$this->work();
$this->work();
$this->work();

$response = HorizonExporter::exportAsPlainText();

$this->assertStringContainsString(
Expand Down

0 comments on commit 215754e

Please sign in to comment.