Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: emit metrics about entity imports #847

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# api

## 10x.14.0 - 12 August 2024
- Add Prometheus metrics regarding Entity Import feature

## 10x.13.0 - 08 august 2024
- Fix typo in WikiController and test

Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ FROM php:8.2-apache
RUN apt-get update \
# Needed for the imagick php extension install
&& apt-get install -y --no-install-recommends libmagickwand-dev libpq-dev \
&& echo "" | pecl install imagick \
&& echo "" | pecl install imagick redis \
&& docker-php-ext-enable imagick \
&& docker-php-ext-enable redis \
&& docker-php-ext-configure pcntl --enable-pcntl \
&& docker-php-ext-install pcntl \
&& docker-php-ext-enable pcntl \
Expand Down
25 changes: 25 additions & 0 deletions app/Http/Controllers/WikiEntityImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,27 @@
use App\WikiEntityImport;
use App\Jobs\WikiEntityImportJob;
use Carbon\Carbon;
use Prometheus\CollectorRegistry;
use Prometheus\Counter;

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

public function __construct(CollectorRegistry $registry)
{
$this->successfulCounter = $registry->getOrRegisterCounter(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a tricky decision here: if we define these counters only once, it means they will be missing from the metrics if they have never been increased yet (the controller that answers the scraper will not know about it yet as it's never been persisted).

Alternatively we could:

  • duplicate this definition in the Metrics/WikiEntityImport class
  • move all metrics definitions to the service provider and have stub metrics methods on the metrics classes themselves

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand right this isn't really a big problem? It just means that instead of 0 we will have null until the first import?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly

config('horizon-exporter.namespace'),
'wiki_entity_imports_successful',
'The number of successful Entity import records.',
);
$this->failedCounter = $registry->getOrRegisterCounter(
config('horizon-exporter.namespace'),
'wiki_entity_imports_failed',
'The number of failed Entity import records.',
);
}
public function get(Request $request): \Illuminate\Http\JsonResponse
{
$validatedInput = $request->validate([
Expand Down Expand Up @@ -95,6 +113,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
31 changes: 31 additions & 0 deletions app/Metrics/WikiEntityImports.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php


namespace App\Metrics;

use LKDevelopment\HorizonPrometheusExporter\Contracts\Exporter;
use Prometheus\CollectorRegistry;
use App\WikiEntityImport;
use App\WikiEntityImportStatus;

class WikiEntityImports implements Exporter
{
protected $pending;
public function metrics(CollectorRegistry $collectorRegistry)
{
$this->pending = $collectorRegistry->getOrRegisterGauge(
config('horizon-exporter.namespace'),
'wiki_entity_imports_pending',
'The number of pending Entity imports currently being processed.',
);
}


public function collect()
{
// counters for failed / success are incremented in the HTTP controller
$this->pending->set(
WikiEntityImport::where(['status' => WikiEntityImportStatus::Pending])->count()
);
}
}
41 changes: 41 additions & 0 deletions app/Providers/CollectorRegistryProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Config;
use Prometheus\CollectorRegistry;
use Prometheus\Storage\Redis;
use Illuminate\Contracts\Foundation\Application;
use LKDevelopment\HorizonPrometheusExporter\Repository\ExporterRepository;


class CollectorRegistryProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
$this->app->bind(CollectorRegistry::class, function (Application $app) {
return new CollectorRegistry(new Redis([
'host' => Config::get('database.redis.metrics.host'),
'port' => Config::get('database.redis.metrics.port'),
'password' => Config::get('database.redis.metrics.password'),
'timeout' => 0.1, // in seconds
'read_timeout' => '10', // in seconds
'persistent_connections' => false
]));
}, true);
}

/**
* Bootstrap services.
*/
public function boot(): void
{
ExporterRepository::setRegistry(
$this->app->make(CollectorRegistry::class),
);
}
}
1 change: 1 addition & 0 deletions config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
App\Providers\KubernetesClientServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\ReCaptchaServiceProvider::class,
App\Providers\CollectorRegistryProvider::class,

/*
* Imported from LUMEN
Expand Down
5 changes: 5 additions & 0 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@
'database' => env('REDIS_CACHE_DB', 1),
],

'metrics' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
],
],

];
1 change: 1 addition & 0 deletions config/horizon-exporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
\LKDevelopment\HorizonPrometheusExporter\Exporter\RecentJobs::class,
\App\Metrics\FailedQsBatches::class,
\App\Metrics\PendingQsBatches::class,
\App\Metrics\WikiEntityImports::class,
],

/**
Expand Down
Loading