PHP code example of stepovenko / laravel-prometheus
1. Go to this page and download the library: Download stepovenko/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/ */
stepovenko / laravel-prometheus example snippets
use Ensi\LaravelPrometheus\Prometheus;
public function boot(): void
{
Prometheus::counter('http_requests_count')->labels(['endpoint', 'code']);
Prometheus::summary('http_requests_duration_seconds', 60, [0.5, 0.95, 0.99]);
}
use Closure;
use Ensi\LaravelPrometheus\Prometheus;
use Illuminate\Support\Facades\Route;
public function handle($request, Closure $next)
{
$startTime = microtime(true);
$response = $next($request);
Prometheus::update('http_requests_count', 1, [Route::current()?->uri, $response->status()]);
Prometheus::update('http_requests_duration_seconds', microtime(true) - $startTime);
return $response;
}
use Ensi\LaravelPrometheus\LabelMiddlewares\LabelMiddleware;
class TenantLabelMiddleware implements LabelMiddleware
{
public function labels(): array
{
return ['tenant'];
}
public function values(): array
{
return [Tenant::current()->id];
}
}
use Ensi\LaravelPrometheus\MetricsBag;
use Ensi\LaravelPrometheus\OnDemandMetrics\OnDemandMetric;
use Illuminate\Support\Facades\Queue;
class QueueLengthOnDemandMetric extends OnDemandMetric
{
public function register(MetricsBag $metricsBag): void
{
$metricsBag->gauge('queue_length');
}
public function update(MetricsBag $metricsBag): void
{
$metricsBag->update('queue_length', Queue::size());
}
}