initial commit
This commit is contained in:
commit
bf1a4e1879
17 changed files with 822 additions and 0 deletions
72
src/PrometheusServiceProvider.php
Normal file
72
src/PrometheusServiceProvider.php
Normal file
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace Superbalist\LaravelPrometheusExporter;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Prometheus\CollectorRegistry;
|
||||
use Prometheus\Storage\Adapter;
|
||||
|
||||
class PrometheusServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Perform post-registration booting of services.
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->publishes([
|
||||
__DIR__ . '/../config/prometheus.php' => config_path('prometheus.php'),
|
||||
]);
|
||||
|
||||
if (config('prometheus.metrics_route_enabled')) {
|
||||
$this->loadRoutesFrom(__DIR__ . '/routes.php');
|
||||
}
|
||||
|
||||
$exporter = $this->app->make(PrometheusExporter::class); /** @var PrometheusExporter $exporter */
|
||||
foreach (config('prometheus.collectors') as $class) {
|
||||
$collector = $this->app->make($class);
|
||||
$exporter->registerCollector($collector);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register bindings in the container.
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->mergeConfigFrom(__DIR__ . '/../config/prometheus.php', 'prometheus');
|
||||
|
||||
$this->app->singleton(PrometheusExporter::class, function ($app) {
|
||||
$adapter = $app['prometheus.storage_adapter'];
|
||||
$prometheus = new CollectorRegistry($adapter);
|
||||
return new PrometheusExporter(config('prometheus.namespace'), $prometheus);
|
||||
});
|
||||
$this->app->alias(PrometheusExporter::class, 'prometheus');
|
||||
|
||||
$this->app->bind('prometheus.storage_adapter_factory', function ($app) {
|
||||
return new StorageAdapterFactory($app);
|
||||
});
|
||||
|
||||
$this->app->bind(Adapter::class, function ($app) {
|
||||
$factory = $app['prometheus.storage_adapter_factory']; /** @var StorageAdapterFactory $factory */
|
||||
$driver = config('prometheus.storage_adapter');
|
||||
$configs = config('storage_adapters');
|
||||
$config = array_get($configs, $driver, []);
|
||||
return $factory->make($driver, $config);
|
||||
});
|
||||
$this->app->alias(Adapter::class, 'prometheus.storage_adapter');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the services provided by the provider.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function provides()
|
||||
{
|
||||
return [
|
||||
'prometheus',
|
||||
'prometheus.storage_adapter_factory',
|
||||
'prometheus.storage_adapter',
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue