3slab/prometheus-monitoring-bundle
Installation:
composer require 3slab/prometheus-monitoring-bundle
Add to config/bundles.php:
return [
// ...
Suez\PrometheusMonitoringBundle\SuezPrometheusMonitoringBundle::class => ['all' => true],
];
First Use Case:
/metrics to expose Prometheus-formatted metrics (powered by tweedegolf/prometheus-bundle)./health for a simple endpoint compatible with Prometheus Blackbox Exporter.Configuration:
Check config/packages/suez_prometheus_monitoring.yaml for defaults. Override as needed:
suez_prometheus_monitoring:
metrics:
enabled: true
route: /metrics
health:
enabled: true
route: /health
Metrics Collection:
Prometheus\CollectorRegistry to register custom metrics:
use Prometheus\RenderTextFormat;
use Prometheus\CollectorRegistry;
$registry = new CollectorRegistry();
$registry->getOrRegisterCounter('app_requests_total', 'Total app requests', ['method']);
Health Checks:
// src/EventListener/HealthCheckListener.php
public function onKernelRequest(GetResponseEvent $event)
{
if ($event->getRequest()->getPathInfo() === '/health') {
$health = new HealthCheck();
$health->addCheck(new DatabaseCheck());
$health->addCheck(new CacheCheck());
return new Response($health->check());
}
}
Integration with Prometheus:
/metrics:
# prometheus.yml
scrape_configs:
- job_name: 'laravel_app'
scrape_interval: 15s
static_configs:
- targets: ['your-app:8000']
Dynamic Metrics: Use middleware to track request metrics:
// app/Http/Middleware/TrackMetrics.php
public function handle($request, Closure $next)
{
$start = microtime(true);
$response = $next($request);
$duration = microtime(true) - $start;
$registry->getOrRegisterHistogram('http_request_duration_seconds')
->observe($duration, ['method' => $request->method(), 'path' => $request->path()]);
return $response;
}
Custom Health Checks:
Suez\PrometheusMonitoringBundle\HealthCheck\HealthCheckInterface:
class QueueCheck implements HealthCheckInterface
{
public function check(): bool
{
return Queue::size() < 1000;
}
public function getMessage(): string
{
return 'Queue size is healthy';
}
}
Deprecated Bundle:
VdmPrometheusBundle for new projects.tweedegolf/prometheus-bundle dependencies diverge.Route Conflicts:
/metrics and /health routes don’t clash with existing routes. Override in routes.yaml:
suez_prometheus_monitoring:
health:
route: /app_health
Performance Overhead:
Metrics Not Showing?:
Prometheus\CollectorRegistry is properly injected and metrics are registered before the request lifecycle ends./_profiler) for errors during metric collection.Health Endpoint Failing:
true or implement proper error handling:
public function check(): bool
{
try {
return DB::connection()->getPdo();
} catch (\Exception $e) {
return false;
}
}
Custom Metrics:
// src/Kernel.php
protected function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(function (ContainerBuilder $container) {
$container->getDefinition('suez_prometheus_monitoring.registry')
->setMethodCalls([
['register', [new CustomMetric()]],
]);
});
}
Health Check Templates:
php artisan vendor:publish --tag=suez-prometheus-monitoring-assets
templates/health.html.twig to customize the output format.Prometheus Labels:
RequestStack:
$labels = [
'user_id' => $request->user()->id,
'locale' => $request->getLocale(),
];
$registry->getOrRegisterCounter('custom_metric')->inc($labels);
---
```markdown
### Configuration Quirks
- **Blackbox Exporter Compatibility**:
The `/health` endpoint expects a **200 OK** response for healthy checks. Non-200 responses (e.g., 500) will trigger alerts in Prometheus.
Example for Prometheus Blackbox Exporter config:
```yaml
modules:
http_2xx:
prober: http
http:
valid_status_codes: [200]
/metrics to ensure real-time data:
# config/packages/framework.yaml
framework:
http_cache:
invalidation:
paths: ['^/(?!metrics$)']
How can I help you explore Laravel packages today?