Skip to content

Commit

Permalink
feat: use persistent storage for prometheus metrics, enabling counters
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Aug 6, 2024
1 parent 4b7684b commit dc934a3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
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
11 changes: 6 additions & 5 deletions app/Http/Controllers/WikiEntityImportController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,25 @@
use App\WikiEntityImport;
use App\Jobs\WikiEntityImportJob;
use Carbon\Carbon;
use LKDevelopment\HorizonPrometheusExporter\Repository\ExporterRepository;
use Prometheus\CollectorRegistry;
use Prometheus\Counter;

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

public function __construct()
public function __construct(CollectorRegistry $registry)
{
ExporterRepository::load();
$this->successfulCounter = ExporterRepository::getRegistry()->getCounter(
$this->successfulCounter = $registry->getOrRegisterCounter(
config('horizon-exporter.namespace'),
'wiki_entity_imports_successful',
'The number of successful Entity import records.',
);
$this->failedCounter = ExporterRepository::getRegistry()->getCounter(
$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
Expand Down
10 changes: 0 additions & 10 deletions app/Metrics/WikiEntityImports.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ public function metrics(CollectorRegistry $collectorRegistry)
'wiki_entity_imports_pending',
'The number of pending Entity imports currently being processed.',
);
$this->successful = $collectorRegistry->getOrRegisterCounter(
config('horizon-exporter.namespace'),
'wiki_entity_imports_successful',
'The number of successful Entity import records.',
);
$this->failed = $collectorRegistry->getOrRegisterCounter(
config('horizon-exporter.namespace'),
'wiki_entity_imports_failed',
'The number of failed Entity import records.',
);
}


Expand Down
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
6 changes: 6 additions & 0 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,12 @@
'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),
'database' => env('REDIS_METRICS_DB', 2),
],
],

];

0 comments on commit dc934a3

Please sign in to comment.