Skip to content

Commit

Permalink
refactor(core): Support zlodes/prometheus-client 2.x
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Configuration changed
  • Loading branch information
zlodes committed Oct 15, 2023
1 parent ab09e30 commit 86a5c28
Show file tree
Hide file tree
Showing 21 changed files with 1,257 additions and 689 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ composer require zlodes/prometheus-client-laravel

### Register a route for the metrics controller

Your application is responsible for metrics route registration. There is a ready to use [controller](src/Http/MetricsExporterController.php). You can configure groups, middleware or prefixes as you want.
Your application is responsible for metrics route registration.
There is a [controller](src/Http/MetricsExporterController.php) ready to use.
You can configure groups, middleware or prefixes as you want.

Example:

Expand All @@ -25,14 +27,15 @@ use Zlodes\PrometheusClient\Laravel\Http\MetricsExporterController;
Route::get('/metrics', MetricsExporterController::class);
```

### Configure a Storage for metrics [optional]
### Configure Storage for metrics [optional]

By-default, it uses [RedisStorage](src/Storage/RedisStorage.php). If you want to use other storage, you can do it easily following these three steps:
By default, it uses Redis storage.
If you want to use other storage, you can do it easily following these three steps:

1. Create a class implements `Storage` interface.
2. Publish a config:
```shell
php artisan vendor:publish --tag=prometheus-exporter
php artisan vendor:publish --tag=prometheus-client
```
3. Set your `storage` class in the config.

Expand Down Expand Up @@ -94,6 +97,14 @@ $this->callAfterResolving(
| `php artisan metrics:clear` | Clears metrics storage |
| `metrics:collect-scheduled` | Runs `ScheduledCollectors`. Using by Scheduler |

## Upgrade guide

### From 1.x to 2.x

1. Run `php artisan vendor:publish --tag=prometheus-client` to publish a brand-new config
2. Configure the new config based on the previous one (`prometheus-exporter.php`)
3. Drop legacy config (`prometheus-exporter.php`)

## Testing

### Run tests
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"ext-redis": "*",
"laravel/framework": "^9.0 || ^10.0",
"webmozart/assert": "^1.11",
"zlodes/prometheus-client": "^1.1.2"
"zlodes/prometheus-client": "2.x-dev"
},
"require-dev": {
"ergebnis/composer-normalize": "dev-main",
Expand Down
10 changes: 3 additions & 7 deletions config/prometheus-exporter.php → config/prometheus-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
'enabled' => (bool) env('PROMETHEUS_CLIENT_ENABLED', true),

/**
* Here you can configure a Storage for metrics
*
* Available options:
* - \Zlodes\PrometheusClient\Storage\InMemoryStorage::class
* - \Zlodes\PrometheusClient\Laravel\Storage\RedisStorage::class
* - Your own storage implements Storage interface
* Here you can configure a Storage for metrics.
* Available options: "null", "in_memory", "redis"
*/
'storage' => \Zlodes\PrometheusClient\Laravel\Storage\RedisStorage::class,
'storage' => env('PROMETHEUS_CLIENT_STORAGE', 'redis'),

/**
* Here you can specify a list of your SchedulableCollectors
Expand Down
18 changes: 14 additions & 4 deletions src/Command/ClearMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,25 @@
namespace Zlodes\PrometheusClient\Laravel\Command;

use Illuminate\Console\Command;
use Zlodes\PrometheusClient\Storage\Storage;
use Zlodes\PrometheusClient\Storage\Contracts\CounterStorage;
use Zlodes\PrometheusClient\Storage\Contracts\GaugeStorage;
use Zlodes\PrometheusClient\Storage\Contracts\HistogramStorage;
use Zlodes\PrometheusClient\Storage\Contracts\SummaryStorage;

final class ClearMetrics extends Command
{
protected $signature = 'metrics:clear';
protected $description = 'Drop all the metrics values';

public function handle(Storage $storage): void
{
$storage->clear();
public function handle(
CounterStorage $counterStorage,
GaugeStorage $gaugeStorage,
HistogramStorage $histogramStorage,
SummaryStorage $summaryStorage,
): void {
$counterStorage->clearCounters();
$gaugeStorage->clearGauges();
$histogramStorage->clearHistograms();
$summaryStorage->clearSummaries();
}
}
12 changes: 3 additions & 9 deletions src/Command/ListMetrics.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
namespace Zlodes\PrometheusClient\Laravel\Command;

use Illuminate\Console\Command;
use JsonException;
use Zlodes\PrometheusClient\Registry\Registry;

final class ListMetrics extends Command
{
protected $signature = 'metrics:list';
protected $description = 'Outputs a table with all the registered metrics';

/**
* @throws JsonException
*/
public function handle(Registry $registry): void
{
$metrics = [];
Expand All @@ -26,10 +22,9 @@ public function handle(Registry $registry): void

$metrics[] = [
$counter,
$metric->getName(),
$metric->getType()->value,
$metric->getHelp(),
json_encode($metric->getInitialLabels(), JSON_THROW_ON_ERROR | JSON_FORCE_OBJECT),
$metric->name,
$metric->getPrometheusType(),
$metric->help,
];
}

Expand All @@ -38,7 +33,6 @@ public function handle(Registry $registry): void
'Name',
'Type',
'Help',
'Initial labels',
];

$this->table($tableHeader, $metrics);
Expand Down
46 changes: 12 additions & 34 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use Webmozart\Assert\Assert;
use Zlodes\PrometheusClient\Collector\CollectorFactory;
use Zlodes\PrometheusClient\Exporter\Exporter;
use Zlodes\PrometheusClient\Exporter\StoredMetricsExporter;
use Zlodes\PrometheusClient\Exporter\FetcherExporter;
use Zlodes\PrometheusClient\Fetcher\Fetcher;
use Zlodes\PrometheusClient\Fetcher\StoredMetricsFetcher;
use Zlodes\PrometheusClient\KeySerialization\JsonSerializer;
use Zlodes\PrometheusClient\KeySerialization\Serializer;
use Zlodes\PrometheusClient\Laravel\Command\ClearMetrics;
Expand All @@ -20,28 +22,27 @@
use Zlodes\PrometheusClient\Laravel\ScheduledCollector\SchedulableCollector;
use Zlodes\PrometheusClient\Laravel\ScheduledCollector\SchedulableCollectorArrayRegistry;
use Zlodes\PrometheusClient\Laravel\ScheduledCollector\SchedulableCollectorRegistry;
use Zlodes\PrometheusClient\Laravel\Storage\StorageConfigurator;
use Zlodes\PrometheusClient\Registry\ArrayRegistry;
use Zlodes\PrometheusClient\Registry\Registry;
use Zlodes\PrometheusClient\Storage\NullStorage;
use Zlodes\PrometheusClient\Storage\Storage;

final class ServiceProvider extends BaseServiceProvider
{
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/../config/prometheus-exporter.php', 'prometheus-exporter');
$this->mergeConfigFrom(__DIR__ . '/../config/prometheus-client.php', 'prometheus-client');

$this->app->singleton(CollectorFactory::class);
$this->app->singleton(Registry::class, ArrayRegistry::class);
$this->app->singleton(Exporter::class, StoredMetricsExporter::class);
$this->app->singleton(Fetcher::class, StoredMetricsFetcher::class);
$this->app->singleton(Exporter::class, FetcherExporter::class);

$this->app->singleton(Serializer::class, JsonSerializer::class);

$this->registerStorage();
$this->registerSchedulableCollectors();
}

public function boot(): void
public function boot(StorageConfigurator $storageConfigurator): void
{
if ($this->app->runningInConsole()) {
$this->commands([
Expand All @@ -51,34 +52,11 @@ public function boot(): void
]);

$this->publishes([
__DIR__ . '/../config/prometheus-exporter.php' => config_path('prometheus-exporter.php'),
], 'prometheus-exporter');
__DIR__ . '/../config/prometheus-client.php' => config_path('prometheus-client.php'),
], 'prometheus-client');
}
}

private function registerStorage(): void
{
$this->app->singleton(Storage::class, static function (Application $app): Storage {
/** @var Repository $config */
$config = $app->make(Repository::class);

$clientEnabled = $config->get('prometheus-exporter.enabled') ?? true;
Assert::boolean($clientEnabled);

if ($clientEnabled === false) {
return new NullStorage();
}

/** @psalm-var class-string<Storage> $storageClass */
$storageClass = $config->get('prometheus-exporter.storage');
Assert::true(
is_a($storageClass, Storage::class, true),
'Config value in prometheus-exporter.storage must be a class-string<Storage>'
);

/** @var Storage */
return $app->make($storageClass);
});
$storageConfigurator->configure();
}

private function registerSchedulableCollectors(): void
Expand All @@ -92,7 +70,7 @@ static function (SchedulableCollectorRegistry $registry, Application $app): void
$config = $app->make(Repository::class);

/** @psalm-var list<class-string<SchedulableCollector>> $collectors */
$collectors = $config->get('prometheus-exporter.schedulable_collectors');
$collectors = $config->get('prometheus-client.schedulable_collectors');
Assert::allStringNotEmpty($collectors);

foreach ($collectors as $collectorClass) {
Expand Down
Loading

0 comments on commit 86a5c28

Please sign in to comment.