Installation
Requirements
Section titled “Requirements”- PHP 8.2 or later
- Laravel 12 or 13
- For the
redisstore, the default: a Redis server, with thephpredisextension or thepredis/predispackage - For
metrics:push: a PSR-18 HTTP client. Laravel already requires Guzzle, which is one.
The package does not need the opentelemetry PHP extension. Requirements lists the dependencies and the tested versions.
Install the package
Section titled “Install the package”-
Require the package with Composer:
Terminal window composer require nwidart/laravel-metricsComposer can ask if you trust the
tbachert/spiplugin. The package does not need it: answern.Package discovery registers
Nwidart\Metrics\MetricsServiceProvider. You do not need to add it tobootstrap/providers.php. -
Pick a store. The package records from the first request, into the Redis
defaultconnection.-
In production, keep the
redisstore. Give it a Redis connection that never evicts keys: see Set up for production. -
Without Redis, such as on a development machine, use a Laravel cache store:
.env METRICS_STORE=cache
Without a reachable Redis and without
METRICS_STORE=cache, the application keeps working, but each request that records writes a warning to the PHP error log. To record nothing, setMETRICS_ENABLED=false. -
-
Pick how the metrics leave the application:
- For a Prometheus scrape, set
METRICS_PROMETHEUS=trueand protectGET /metrics. See Scrape with Prometheus. - For an OTLP push, set
OTEL_EXPORTER_OTLP_ENDPOINTand schedulemetrics:push. See Push over OTLP.
- For a Prometheus scrape, set
-
Optional: publish the configuration file to change the defaults:
Terminal window php artisan vendor:publish --tag=metrics-configThis copies the file to
config/metrics.php. The configuration reference describes every key.
Deploy with an optimized autoloader
Section titled “Deploy with an optimized autoloader”In production, install with an optimized autoloader. Without it, booting the OpenTelemetry SDK costs about 1 ms per request instead of about 75 µs:
composer install --optimize-autoloader --no-devRoute and configuration caching
Section titled “Route and configuration caching”The package reads every value through config/metrics.php, so it works with php artisan config:cache. A cached configuration does not read .env, so run php artisan config:cache again after you change a METRICS_* variable.
With cached routes, the package does not register the scrape route itself. After you change METRICS_PROMETHEUS or METRICS_PROMETHEUS_PATH, rebuild both caches:
php artisan config:cachephp artisan route:cacheNext steps
Section titled “Next steps”- New to the package? Follow the tutorial.
- Going to production? Follow Set up for production.
- Want metrics of your own? See Record your own metrics.