PHP code example of shureban / laravel-prometheus

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

    

shureban / laravel-prometheus example snippets


Shureban\LaravelPrometheus\PrometheusServiceProvider::class,

namespace App\Prometheus;

use Shureban\LaravelPrometheus\Counter;
use Shureban\LaravelPrometheus\Attributes\Name;
use Shureban\LaravelPrometheus\Attributes\Labels;

class AuthCounter extends Counter
{
    public function __construct()
    {
        $name   = new Name('auth');
        $labels = new Labels(['event']);
        $help   = 'Counter of auth events';

        parent::__construct($name, $labels, $help);
    }
}

use App\Prometheus\AuthCounter;

class RegisterController extends Controller
{
    public function __invoke(..., AuthCounter $counter): Response
    {
        // Registration new user logic
    
        $counter->withLabelsValues(['registration'])->inc();
    }
}

namespace App\Prometheus\Counters;

use Shureban\LaravelPrometheus\Counter;
use Shureban\LaravelPrometheus\Attributes\Name;
use Shureban\LaravelPrometheus\Attributes\Labels;

class AuthCounter extends Counter
{
    public function __construct()
    {
        //... 
    }
    
    public function registration(): void 
    {
        $this->withLabelsValues(['registration'])->inc();
    }
}

use App\Prometheus\AuthCounter;

class RegisterController extends Controller
{
    public function __invoke(..., AuthCounter $counter): Response
    {
        // Registration new user logic
    
        $counter->registration();
    }
}

use App\Prometheus\AuthCounter;
use Shureban\LaravelPrometheus\Attributes\Labels;

class RegisterController extends Controller
{
    public function __invoke(..., DynamicAuthCounter $counter): Response
    {
        // Registration new user logic
    
        $counter->withLabels(Labels::newFromArray(['event' => 'registration', 'country' => 'US']))->inc();
        $counter->withLabels(Labels::newFromArray(['event' => 'registration', 'country' => 'US', 'browser' => 'chrome']))->inc();
        $counter->withLabels(Labels::newFromCollection($user->only(['country', 'browser'])))->inc();
    }
}

$renderer = new RenderTextFormat();

return response($renderer->render(), Response::HTTP_OK, ['Content-Type' => RenderTextFormat::MIME_TYPE]);

return response(new RenderTextFormat(), Response::HTTP_OK, ['Content-Type' => RenderTextFormat::MIME_TYPE]);
shell
php artisan vendor:publish --provider="Shureban\LaravelPrometheus\PrometheusServiceProvider"
text
REDIS_CLIENT=predis
bash
# Creating counter metric CustomCounterMetricName
php artisan make:counter CustomCounterMetricName --name={name} --labels={label_1,label_2,label_N} --description={description} --dynamic

# Creating gauge metric CustomGaugeMetricName
php artisan make:gauge CustomGaugeMetricName --name={name} --labels={label_1,label_2,label_N} --description={description} --dynamic