Installation Add the bundle via Composer:
composer require 3slab/vdm-prometheus-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Vdm\PrometheusBundle\VdmPrometheusBundle::class => ['all' => true],
];
Configuration Publish the default config (optional):
php bin/console vdm:prometheus:install
Edit config/packages/vdm_prometheus.yaml to adjust:
/metrics)dev)First Use Case
Access the metrics endpoint at /metrics (or your configured path) and scrape it with Prometheus:
# prometheus.yml
scrape_configs:
- job_name: 'laravel_app'
static_configs:
- targets: ['your-app:8000']
Automatic Metrics Collection
The bundle hooks into Symfony’s kernel events (kernel.request, kernel.response) to track:
200, 500).vdm_sf_app_call_total).Custom Metrics Extend the bundle by creating custom collectors. Example:
// src/Collector/CustomCollector.php
namespace App\Collector;
use Prometheus\Collector;
class CustomCollector extends Collector {
public function collect() {
$this->metrics['app_custom_metric'] = new Gauge([
'help' => 'Custom metric example',
'label_names' => ['type'],
]);
$this->metrics['app_custom_metric']->set([], 42);
}
}
Register in config/packages/vdm_prometheus.yaml:
vdm_prometheus:
collectors:
- App\Collector\CustomCollector
Middleware Integration Use the bundle’s metrics in middleware for granular tracking:
// app/Http/Middleware/TrackCustomMetric.php
public function handle($request, Closure $next) {
$start = microtime(true);
$response = $next($request);
$duration = microtime(true) - $start;
// Manually update a custom metric (if needed)
$this->prometheus->getCollector('custom')->set([], $duration);
return $response;
}
Environment-Specific Scraping
Restrict the /metrics endpoint to specific environments (e.g., dev, staging) via config:
vdm_prometheus:
allowed_environments: ['dev', 'staging']
vdm_sf_app_response_code_total{http_code="500"}) to trigger alerts for error spikes.histogram_quantile).vdm_sf_app_response_code_total).debugbar for local development insights, then switch to Prometheus for production.Performance Overhead
Memory Leaks
vdm_prometheus:
ignore_routes:
- '^_wdt/'
- '^_profiler/'
Label Collisions
route, http_code).Prometheus Scrape Failures
403 if environment isn’t whitelisted.allowed_environments in config and check Prometheus’s scrape_interval.vdm_prometheus:
debug: true
/metrics endpoint manually:
curl http://localhost:8000/metrics
Expected output: Prometheus exposition format (as shown in README).Custom Metric Types
Add support for Histogram or Summary metrics by extending the bundle’s collector:
// src/Collector/RequestDurationHistogram.php
use Prometheus\Histogram;
class RequestDurationHistogram extends Collector {
public function collect() {
$this->metrics['request_duration_seconds'] = new Histogram([
'help' => 'Request duration in seconds',
'label_names' => ['route'],
'buckets' => [0.1, 0.5, 1, 2.5, 5],
]);
}
}
Event-Based Hooks
Listen to vdm_prometheus.collect events to inject custom logic:
// src/EventListener/CustomMetricsListener.php
public function onCollect(CollectEvent $event) {
$event->getCollector()->addMetric('custom_metric', 123);
}
Register in services.yaml:
services:
App\EventListener\CustomMetricsListener:
tags:
- { name: 'kernel.event_listener', event: 'vdm_prometheus.collect' }
Exclude Routes Skip metrics for specific routes (e.g., health checks):
vdm_prometheus:
ignore_routes:
- '^health$'
app label in metrics defaults to app. Override via:
vdm_prometheus:
app_name: 'my_laravel_app'
How can I help you explore Laravel packages today?