Set up for production
This guide sets up the redis store for production. It assumes the application already runs Redis through config/database.php.
1. Install the package
Section titled “1. Install the package”composer require nwidart/laravel-metricsThe service provider registers itself. Recording starts at once, into the Redis default connection.
2. Give the store its own Redis connection
Section titled “2. Give the store its own Redis connection”The store needs a Redis that never evicts keys. A cache Redis with an eviction policy loses series without a warning.
-
Add a connection to the
redisarray inconfig/database.php:config/database.php 'redis' => [// ...'metrics' => ['url' => env('REDIS_METRICS_URL'),'host' => env('REDIS_METRICS_HOST', '127.0.0.1'),'username' => env('REDIS_METRICS_USERNAME'),'password' => env('REDIS_METRICS_PASSWORD'),'port' => env('REDIS_METRICS_PORT', '6379'),'database' => env('REDIS_METRICS_DB', '0'),'persistent' => true,],], -
Point the store at it in
.env:.env METRICS_REDIS_CONNECTION=metrics -
Set the eviction policy of that Redis to
noeviction. Inredis.conf:redis.conf maxmemory-policy noevictionOn a managed Redis, such as Amazon ElastiCache, set
maxmemory-policyin the parameter group.
'persistent' => true keeps the connection open between requests, so a merge does not pay a handshake.
3. Pick the Redis client
Section titled “3. Pick the Redis client”The store works with phpredis and with predis. Laravel picks the client with REDIS_CLIENT.
Install the redis PHP extension, then:
REDIS_CLIENT=phprediscomposer require predis/predisREDIS_CLIENT=predisA phpredis cluster connection is not supported: the merge fails and logs a warning. Use one Redis instance.
4. Deploy with an optimized autoloader
Section titled “4. Deploy with an optimized autoloader”Without an optimized autoloader, booting the SDK costs about 1 ms per request instead of about 75 µs. In the deployment script:
composer install --optimize-autoloader --no-devphp artisan config:cachephp artisan route:cacheThe package reads every value through config/metrics.php, so it works with a cached configuration. Run php artisan config:cache again after you change a METRICS_* variable.
5. Restart the queue workers
Section titled “5. Restart the queue workers”Queue workers keep the configuration they booted with. After each deployment:
php artisan queue:restartWith Horizon, run php artisan horizon:terminate instead. Each worker merges after each job.
6. Leave out health probes
Section titled “6. Leave out health probes”Load balancers and orchestrators call a health route many times a minute. Leave it out of http.server.request.duration. Publish the configuration, then set ignore_routes:
php artisan vendor:publish --tag=metrics-config'ignore_routes' => ['/up'],See Ignore routes for other patterns.
7. Send the metrics out
Section titled “7. Send the metrics out”The package records, but nothing leaves the application yet. Do one of these:
- To let Prometheus scrape the application, follow Scrape with Prometheus.
- To push over OTLP to a Collector or a backend, follow Push over OTLP.