Skip to content

Commit

Permalink
refactor: use counter for modeling failed/successful import job metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Aug 1, 2024
1 parent f890904 commit 4b7684b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
24 changes: 24 additions & 0 deletions app/Http/Controllers/WikiEntityImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,26 @@
use App\WikiEntityImport;
use App\Jobs\WikiEntityImportJob;
use Carbon\Carbon;
use LKDevelopment\HorizonPrometheusExporter\Repository\ExporterRepository;
use Prometheus\Counter;

class WikiEntityImportController extends Controller
{
private Counter $successfulCounter;
private Counter $failedCounter;

public function __construct()
{
ExporterRepository::load();
$this->successfulCounter = ExporterRepository::getRegistry()->getCounter(
config('horizon-exporter.namespace'),
'wiki_entity_imports_successful',
);
$this->failedCounter = ExporterRepository::getRegistry()->getCounter(
config('horizon-exporter.namespace'),
'wiki_entity_imports_failed',
);
}
public function get(Request $request): \Illuminate\Http\JsonResponse
{
$validatedInput = $request->validate([
Expand Down Expand Up @@ -95,6 +112,13 @@ public function update(Request $request): \Illuminate\Http\JsonResponse
abort(400, 'Import has to be pending if updated');
}

if ($validatedInput['status'] === WikiEntityImportStatus::Failed->value) {
$this->failedCounter->inc();
}
if ($validatedInput['status'] === WikiEntityImportStatus::Success->value) {
$this->successfulCounter->inc();
}

$import->update([
'status' => $validatedInput['status'],
'finished_at' => Carbon::now(),
Expand Down
11 changes: 3 additions & 8 deletions app/Metrics/WikiEntityImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ public function metrics(CollectorRegistry $collectorRegistry)
'wiki_entity_imports_pending',
'The number of pending Entity imports currently being processed.',
);
$this->successful = $collectorRegistry->getOrRegisterGauge(
$this->successful = $collectorRegistry->getOrRegisterCounter(
config('horizon-exporter.namespace'),
'wiki_entity_imports_successful',
'The number of successful Entity import records.',
);
$this->failed = $collectorRegistry->getOrRegisterGauge(
$this->failed = $collectorRegistry->getOrRegisterCounter(
config('horizon-exporter.namespace'),
'wiki_entity_imports_failed',
'The number of failed Entity import records.',
Expand All @@ -36,14 +36,9 @@ public function metrics(CollectorRegistry $collectorRegistry)

public function collect()
{
// counters for failed / success are incremented in the HTTP controller
$this->pending->set(
WikiEntityImport::where(['status' => WikiEntityImportStatus::Pending])->count()
);
$this->failed->set(
WikiEntityImport::where(['status' => WikiEntityImportStatus::Failed])->count()
);
$this->successful->set(
WikiEntityImport::where(['status' => WikiEntityImportStatus::Success])->count()
);
}
}
2 changes: 2 additions & 0 deletions tests/Routes/Wiki/EntityImportBackendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use App\Wiki;
use App\WikiEntityImport;
use App\WikiManager;
use Mockery\MockInterface;
use Prometheus\CollectorRegistry;

class EntityImportBackendTest extends TestCase
{
Expand Down

0 comments on commit 4b7684b

Please sign in to comment.