- How do I integrate Prometheus metrics into a Laravel application with minimal setup?
- Use Composer to install the package (`composer require promphp/prometheus_client_php`), then bootstrap the `CollectorRegistry` in your `AppServiceProvider`. For HTTP metrics, add middleware to track request durations, and expose metrics via a `/metrics` endpoint. Laravel’s service container makes global access easy.
- Which Laravel versions and PHP versions are supported by this package?
- The package requires PHP 8.2+ and works with Laravel 9 or 10. Ensure your Laravel app aligns with these PHP requirements, as older versions (e.g., Laravel 8 with PHP 8.1) may cause compatibility issues.
- Can I use this package for Laravel queue workers or cron jobs?
- Yes, but you’ll need a shared storage adapter like Redis or APCu to aggregate metrics across worker processes. Redis is recommended for production due to its scalability and persistence, while APCu is simpler for single-server setups.
- How do I automatically track HTTP request metrics (e.g., response time, status codes) in Laravel?
- Create middleware that records start time, increments counters for status codes, and observes request durations with a histogram. Register the middleware in `app/Http/Kernel.php` to apply it globally. Example: `response_time_seconds->observe($duration, ['method' => $request->method()]).`
- What’s the best storage backend for production: Redis, APCu, or InMemory?
- Redis is the safest choice for production, especially in multi-server or high-cardinality environments. APCu is lightweight but risks memory bloat with many metrics. InMemory is only suitable for single-process scripts or testing.
- How do I expose metrics for Prometheus to scrape?
- Add a route like `Route::get('/metrics', [MetricsController::class, 'render'])` and use the `RenderTextFormat` class to output Prometheus exposition format. Ensure your Prometheus server targets this endpoint (e.g., `scrape_configs: [targets: ['laravel-app:8000/metrics']]`).
- Are there performance concerns with histograms or summaries in Laravel?
- Histograms and summaries have higher overhead than counters/gauges due to bucket calculations. Benchmark under load—if using Redis, ensure it’s configured with sufficient memory. For high-throughput apps, limit bucket granularity or use sampling.
- Can I use this package with Laravel’s queue jobs to track execution time?
- Yes, decorate your jobs with a trait or use job events to record start/end times. For example, observe a histogram with `job_execution_seconds->observe($duration, ['job' => $job->getJob()])`. Redis ensures metrics persist across worker restarts.
- What alternatives exist for Prometheus metrics in Laravel if Redis isn’t available?
- If Redis is unavailable, APCu is a lightweight alternative for single-server setups, but avoid it for high-cardinality metrics. For distributed systems, consider the `promphp/prometheus_push_gateway_php` companion library to push metrics to a central gateway.
- How do I test Prometheus metrics locally before deploying to production?
- Run a local Redis instance (e.g., via Docker) and configure the package to use it. Test by triggering HTTP requests, queue jobs, or CLI commands, then verify metrics appear in Prometheus (`prometheus -target=http://localhost/metrics`). Use tools like `curl` to check the `/metrics` endpoint.