paulund

Monitoring & Alerting

Monitoring & Alerting

A Laravel application in production needs more than just error logs. You need visibility into whether the application is healthy, whether it is performing well, and whether something has gone wrong — before your users notice. This guide covers the key layers of monitoring and how to wire them together.

Error Tracking

The first line of defence is an error tracking service. When an unhandled exception occurs, Laravel can report it automatically.

Sentry is the most popular choice. Install the SDK and add your DSN to .env:

composer require sentry/sentry-laravel
SENTRY_DSN=https://[email protected]/456

Sentry captures stack traces, request context, and user information without any further configuration. You can also report errors manually:

use Sentry\Facades\Sentry;

try {
    $this->processPayment($order);
} catch (\Exception $e) {
    Sentry::captureException($e);
}

Health Check Endpoints

A health check endpoint lets your load balancer, uptime monitor, or orchestrator verify that the application is alive and its dependencies are reachable. Laravel 11 ships with a built-in health check route:

// routes/web.php
Route::get('/health', function () {
    return response()->json(['status' => 'ok']);
});

For a more thorough check, verify the database and cache are reachable:

Route::get('/health', function () {
    $checks = [
        'database' => (function () {
            try {
                \DB::connection()->getPdo();
                return true;
            } catch (\Exception) {
                return false;
            }
        })(),
        'cache' => (function () {
            try {
                \Cache::put('health_check', true, 5);
                return \Cache::get('health_check') === true;
            } catch (\Exception) {
                return false;
            }
        })(),
    ];

    $healthy = !in_array(false, $checks);

    return response()->json([
        'status' => $healthy ? 'ok' : 'degraded',
        'checks' => $checks,
    ], $healthy ? 200 : 503);
});

Dedicated packages such as spatie/laravel-health provide a more structured approach with built-in checks for common services.

Uptime Monitoring

Use an external uptime monitor (UptimeRobot, Better Uptime, or similar) to poll your health endpoint every 60 seconds. If it does not respond with a 200, you get an alert. This catches problems that internal monitoring might miss — such as the entire server going down.

Application Performance Monitoring (APM)

APM tools profile your requests and reveal slow queries, slow middleware, and overall response times. Datadog, New Relic, and Scout APM all have Laravel integrations. Scout, in particular, is Laravel-native:

composer require laravel/scout

Scout automatically instruments your queries and highlights N+1 problems and slow endpoints without any code changes.

Alerting Strategies

Layer your alerts by severity:

Severity Example Alert destination
Critical Application returns 500 errors PagerDuty / phone call
High Error rate spikes above baseline Slack #alerts channel
Medium Response time exceeds threshold Slack #monitoring channel
Low Disk space above 80% Email digest

Configure Laravel's logging channels to fan critical errors out to Slack or a webhook (see the Logging guide). For broader alerting, combine your error tracker with a dedicated incident management tool.

Key Takeaways

  • Start with an error tracker (Sentry) and a health endpoint — these two alone cover the majority of production issues.
  • Add uptime monitoring for external availability checks.
  • Introduce APM once you need visibility into query and request performance.
  • Keep alert thresholds tuned to your application's baseline; alert fatigue is a real problem.