### Install Laravel Horizon Prometheus Exporter via Composer Source: https://github.com/lkaemmerling/laravel-horizon-prometheus-exporter/blob/master/README.md Use the Composer package manager to add the exporter dependency to your Laravel project. This command downloads the package and registers it within your vendor directory. ```bash composer require lkaemmerling/laravel-horizon-prometheus-exporter ``` -------------------------------- ### GET /metrics Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt Retrieves the current state of Laravel Horizon metrics in Prometheus text format for scraping. ```APIDOC ## GET /metrics ### Description Returns all configured Horizon metrics (job throughput, queue workloads, process counts, failure rates, and system status) in Prometheus text format. ### Method GET ### Endpoint /metrics ### Parameters #### Path Parameters - None #### Query Parameters - None #### Request Body - None ### Request Example ```bash curl -X GET http://your-app.com/metrics ``` ### Response #### Success Response (200) - **Content-Type** (text/plain) - Prometheus text-based metrics format. #### Response Example ```text # HELP app_horizon_status The status of Horizon, -1 = inactive, 0 = paused, 1 = running # TYPE app_horizon_status gauge app_horizon_status 1 # HELP app_horizon_jobs_per_minute The number of jobs per minute # TYPE app_horizon_jobs_per_minute gauge app_horizon_jobs_per_minute 150 # HELP app_horizon_current_workload Current workload of all queues # TYPE app_horizon_current_workload gauge app_horizon_current_workload{queue="default"} 25 ``` ``` -------------------------------- ### Export Current Workload per Queue (PHP) Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt Exports the current queue length (pending jobs) for each queue, labeled by queue name. This metric helps identify queue backlogs and processing delays. It provides an example output format and Prometheus query examples for monitoring. ```php 1000 // Example output: // app_horizon_current_workload{queue="default"} 25 // app_horizon_current_workload{queue="emails"} 12 // app_horizon_current_workload{queue="notifications"} 8 ``` -------------------------------- ### Export Worker Processes per Queue (PHP) Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt Exports the number of worker processes currently assigned to each queue, labeled by queue name. This metric assists in monitoring resource allocation across different queues. It includes example output and Prometheus query examples. ```php all()); ``` -------------------------------- ### Export Recent Jobs Count (PHP) Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt Exports the total count of recently processed jobs. This metric offers insight into overall job activity and processing volume. It counts recent jobs using the JobRepository and includes Prometheus query examples for analysis. ```php countRecent(); ``` -------------------------------- ### Export Failed Jobs Count per Hour (PHP) Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt Exports the count of recently failed jobs. This metric is critical for alerting on job processing errors and monitoring system health. It counts recently failed jobs using the JobRepository and provides Prometheus query examples. ```php 0 // Rate of increase: increase(app_horizon_failed_jobs[1h]) // The exporter counts recently failed jobs from JobRepository $failedCount = app(JobRepository::class)->countRecentlyFailed(); ``` -------------------------------- ### Publish Package Configuration Source: https://github.com/lkaemmerling/laravel-horizon-prometheus-exporter/blob/master/README.md Run the artisan command to publish the package's configuration file to your local config directory. This allows for customization of the exporter settings. ```bash php artisan vendor:publish --provider=LKDevelopment\\HorizonPrometheusExporter\\HorizonPrometheusExporterServiceProvider ``` -------------------------------- ### Run Package Tests Source: https://github.com/lkaemmerling/laravel-horizon-prometheus-exporter/blob/master/README.md Execute the test suite using Composer to ensure the package is functioning correctly within your environment. ```bash composer test ``` -------------------------------- ### Implement Custom Metrics with Exporter Interface Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt Create custom application metrics by implementing the Exporter interface. This allows developers to define and collect specific data points that are rendered alongside standard Horizon metrics. ```php gauge = $collectorRegistry->getOrRegisterGauge( config('horizon-exporter.namespace'), 'pending_orders_count', 'Number of orders pending fulfillment', ['priority'] ); } public function collect(): void { $this->gauge->set(Order::where('status', 'pending')->where('priority', 'high')->count(), ['high']); $this->gauge->set(Order::where('status', 'pending')->where('priority', 'normal')->count(), ['normal']); } } ``` -------------------------------- ### Fetch Metrics from Endpoint Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt Use curl to verify the metrics endpoint, which returns data in the Prometheus text format for scraping. ```bash curl -X GET http://your-app.com/metrics ``` -------------------------------- ### Configure Horizon Prometheus Exporter Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt The configuration file allows defining the Prometheus namespace, enabling specific exporters, and setting up security via IP whitelisting. ```php 'app', "enabled" => env('HORIZON_PROMETHEUS_EXPORTER_ENABLED', true), "url" => 'metrics', "exporters" => [ \LKDevelopment\HorizonPrometheusExporter\Exporter\CurrentMasterSupervisors::class, \LKDevelopment\HorizonPrometheusExporter\Exporter\JobsPerMinute::class, \LKDevelopment\HorizonPrometheusExporter\Exporter\CurrentWorkload::class, \LKDevelopment\HorizonPrometheusExporter\Exporter\CurrentProcessesPerQueue::class, \LKDevelopment\HorizonPrometheusExporter\Exporter\FailedJobsPerHour::class, \LKDevelopment\HorizonPrometheusExporter\Exporter\HorizonStatus::class, \LKDevelopment\HorizonPrometheusExporter\Exporter\RecentJobs::class, ], "ip_whitelist" => [ '10.0.0.0/8', '192.168.1.100', ], "middleware" => \LKDevelopment\HorizonPrometheusExporter\Http\Middleware\IPWhitelistingMiddleware::class, "wipe_storage_after_render" => false, ]; ``` -------------------------------- ### Implement Custom Authentication Middleware Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt Replace default IP-based security with custom token-based authentication. This allows for more flexible security policies when accessing the metrics endpoint. ```php namespace App\Http\Middleware; use Illuminate\Http\Request; use Closure; class PrometheusTokenMiddleware { public function handle(Request $request, Closure $next) { $token = $request->header('Authorization'); if ($token !== 'Bearer ' . config('services.prometheus.token')) { abort(403, 'Invalid Prometheus token'); } return $next($request); } } ``` -------------------------------- ### Configure IP Whitelisting Middleware Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt Restrict access to the metrics endpoint by defining allowed IP addresses or CIDR ranges in the configuration file. This is a security best practice to prevent unauthorized access to operational data. ```php return [ "ip_whitelist" => [ '10.0.0.0/8', '172.16.0.0/12', '192.168.0.0/16', '192.168.1.50', ], "middleware" => \LKDevelopment\HorizonPrometheusExporter\Http\Middleware\IPWhitelistingMiddleware::class, ]; ``` -------------------------------- ### Configure Prometheus Scrape Settings Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt Define the scrape configuration in your prometheus.yml file to enable continuous monitoring of the Laravel application metrics endpoint. ```yaml scrape_configs: - job_name: 'laravel-horizon' scrape_interval: 15s scrape_timeout: 10s metrics_path: /metrics static_configs: - targets: - 'your-laravel-app.com:80' ``` -------------------------------- ### Export Jobs Processed Per Minute (PHP) Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt Exports the current job processing throughput in jobs per minute. This metric aids in monitoring processing capacity and identifying bottlenecks. It fetches data from Horizon's MetricsRepository. ```php jobsProcessedPerMinute(); ``` -------------------------------- ### Export Horizon Status as Gauge Metric (PHP) Source: https://context7.com/lkaemmerling/laravel-horizon-prometheus-exporter/llms.txt Exports the current status of Laravel Horizon as a gauge metric. Values represent -1 (inactive), 0 (paused), or 1 (running). This metric is crucial for alerting on Horizon availability and checks the MasterSupervisorRepository for status. ```php all(); // Returns 1 if running, 0 if any supervisor is paused, -1 if no supervisors ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.