Scrape with Prometheus
To let Prometheus read the metrics, turn on the scrape route, protect it, and add one scrape target.
1. Turn on the route
Section titled “1. Turn on the route”METRICS_PROMETHEUS=trueGET /metrics now serves the export in the Prometheus text format.
To serve it at another path:
METRICS_PROMETHEUS_PATH=internal/metricsIf the application caches its routes, rebuild both caches after each change to these two variables. With cached routes, the package does not register the route itself, and the route cache is built from the cached configuration.
php artisan config:cachephp artisan route:cache2. Protect the route
Section titled “2. Protect the route”The route is open by default once it is on. Protect it with middleware, or keep it off the public network.
To require a bearer token:
-
Create a middleware:
app/Http/Middleware/EnsureMetricsToken.php <?phpnamespace App\Http\Middleware;use Closure;use Illuminate\Http\Request;use Symfony\Component\HttpFoundation\Response;class EnsureMetricsToken{public function handle(Request $request, Closure $next): Response{$token = (string) config('services.metrics.token');abort_unless($token !== '' && hash_equals($token, (string) $request->bearerToken()), 403);return $next($request);}} -
Add the token to
config/services.phpand.env:config/services.php 'metrics' => ['token' => env('METRICS_TOKEN'),], -
Publish the package configuration and add the middleware to the route:
Terminal window php artisan vendor:publish --tag=metrics-configconfig/metrics.php 'prometheus' => ['enabled' => env('METRICS_PROMETHEUS', false),'path' => env('METRICS_PROMETHEUS_PATH', 'metrics'),'middleware' => [\App\Http\Middleware\EnsureMetricsToken::class],'labels' => [],],
prometheus.middleware takes aliases or class names. To allow only some addresses instead, write a middleware that checks $request->ip(), or block the path at the web server or the load balancer.
3. Add a scrape target
Section titled “3. Add a scrape target”Every worker and every host merges into one store, so every address serves the same numbers. Scrape one address, such as the load balancer or the public host name. Scraping each server gives the same series several times, under different instance labels. Only the metrics read on export, such as OPcache and PHP-FPM, describe the server that answers the scrape.
With a plain prometheus.yml:
scrape_configs: - job_name: shop scheme: https metrics_path: /metrics authorization: type: Bearer credentials_file: /etc/prometheus/shop-metrics-token static_configs: - targets: ['shop.example.com']On Kubernetes
Section titled “On Kubernetes”A ServiceMonitor or a pod monitor scrapes every pod, which duplicates every series. Scrape the Service instead, with one static target. With the Prometheus Operator, use a ScrapeConfig:
apiVersion: monitoring.coreos.com/v1alpha1kind: ScrapeConfigmetadata: name: shopspec: metricsPath: /metrics authorization: type: Bearer credentials: name: shop-metrics-token key: token staticConfigs: - targets: ['shop.default.svc.cluster.local:80']4. Leave the scrape out of the HTTP metrics
Section titled “4. Leave the scrape out of the HTTP metrics”The scrape is an HTTP request like the others. It appears in http.server.request.duration with http.route = /metrics. To leave it out, ignore the route by its name:
'ignore_routes' => ['metrics.prometheus'],5. Check the result
Section titled “5. Check the result”curl -H "Authorization: Bearer $METRICS_TOKEN" https://shop.example.com/metricsThe answer holds lines such as http_server_request_duration_seconds_bucket{...}. A 503 means the store cannot be read. See Troubleshoot.
Add labels to every sample
Section titled “Add labels to every sample”To tell applications apart in one Prometheus, add a label to every sample:
'labels' => ['service' => 'shop'],Prometheus can also add it with labels in static_configs.
Example queries
Section titled “Example queries”Request rate per route:
sum by (http_route) (rate(http_server_request_duration_seconds_count[5m]))95th percentile of the request duration, per route:
histogram_quantile(0.95, sum by (le, http_route) (rate(http_server_request_duration_seconds_bucket[5m])))Share of requests that answer 5xx:
sum(rate(http_server_request_duration_seconds_count{http_response_status_code=~"5.."}[5m])) / sum(rate(http_server_request_duration_seconds_count[5m]))Cache hit ratio:
sum(rate(laravel_cache_operations_total{laravel_cache_result="hit"}[5m])) / sum(rate(laravel_cache_operations_total{laravel_cache_operation="read"}[5m]))