PHP code example of aduzenko / laravel-configurable-prometheus

1. Go to this page and download the library: Download aduzenko/laravel-configurable-prometheus 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/ */

    

aduzenko / laravel-configurable-prometheus example snippets


'endpoint' => 'prometheus',

namespace App\Metrics;

use AnatolyDuzenko\ConfigurablePrometheus\DTO\MetricDefinition;
use AnatolyDuzenko\ConfigurablePrometheus\Enums\MetricType;
use AnatolyDuzenko\ConfigurablePrometheus\Contracts\MetricGroup;

class ApiMetrics implements MetricGroup
{
    public function definitions(): array
    {
        return [
            new MetricDefinition(
                namespace: 'api',
                name: 'response_time_seconds',
                helpText: 'API response time',
                type: MetricType::Histogram,
                labelNames: ['route'],
                buckets: [0.1, 0.3, 0.5, 1, 2, 5]
            )
        ];
    }
}

'groups' => [
    \App\Metrics\ApiMetrics::class,
],

// In your class constructor, use
public function __construct(protected MetricManager $metrics)
    {}
// then
$this->metrics->inc('users', 'user_logins_total', ['web']);
$this->metrics->set('users', 'active_users', 42, ['web']);
$this->metrics->observe('api', 'response_time_seconds', 0.32, ['/api/v1']);

// In your method
public function index(Request $request, MetricManager $metrics)
{
    // ....
    $metrics->inc('users', 'user_logins_total', ['web']);
    $metrics->set('users', 'active_users', 42, ['web']);
    $metrics->observe('api', 'response_time_seconds', 0.32, ['/api/v1']);
}
bash
php artisan vendor:publish --tag=prometheus-config