PHP code example of liveintent / laravel-prometheus-exporter

1. Go to this page and download the library: Download liveintent/laravel-prometheus-exporter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

liveintent / laravel-prometheus-exporter example snippets


Exporters\RequestDurationHistogramExporter::class => [
    'enabled' => env('EXPORT_REQUEST_DURATION_HISTOGRAM', true),
    'options'
        'buckets' => [
            5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200
        ],
    ],
],

Exporters\RequestMemoryUsageHistogramExporter::class => [
    'enabled' => env('EXPORT_MEMORY_USAGE_HISTOGRAM', true),
    'options'
        'buckets' => [
            5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200
        ],
    ],
],

Exporters\JobProcessTimeHistogramExporter::class => [
    'enabled' => env('EXPORT_JOB_PROCESS_TIME_HISTOGRAM', true),
    'options'
        'buckets' => [
            0.1, .3, .5, .7, 1, 2, 3, 4, 5, 10, 30, 40, 60,
        ],
    ],
],

Exporters\JobWaitTimeHistogramExporter::class => [
    'enabled' => env('EXPORT_JOB_WAIT_TIME_HISTOGRAM', true),
    'options'
        'buckets' => [
            0.1, .3, .5, .7, 1, 2, 3, 4, 5, 10, 30, 40, 60,
        ],
    ],
],

Exporters\QueryDurationHistogramExporter::class => [
    'enabled' => env('EXPORT_QUERY_DURATION_HISTOGRAM', true),
    'options'
        'buckets' => [
            5, 10, 15, 20, 30, 40, 50, 60, 70, 80, 90, 100, 200
        ],
    ],
],



namespace LiveIntent\LaravelPrometheusExporter\Exporters;

use Illuminate\Foundation\Http\Events\RequestHandled;

class HomePageVisitsCountExporter extends Exporter
{
    /**
     * Register the watcher.
     *
     * @return void
     */
    public function register()
    {
        $this->app['events']->listen(RequestHandled::class, [$this, 'export']);
    }

    /**
     * Export metrics.
     *
     * @param  \Illuminate\Foundation\Http\Events\RequestHandled  $event
     * @return void
     */
    public function export($event)
    {
        $uri =  str_replace($event->request->root(), '', $event->request->fullUrl()) ?: '/';
        
        if ($uri === '/') {
            $counter = $this->registry->getOrRegisterCounter(
                '', 'home_page_visits_counter', 'it is a silly example'
            );

            $counter->inc();
        }
    }
}
bash
php artisan metrics:install
bash
php artisan metrics:clear