Mastering Application Monitoring with Laravel Pulse

Mastering Application Monitoring with Laravel Pulse

Introduction

Laravel Pulse revolutionizes the way developers monitor their applications by providing comprehensive insights into performance and usage. With Pulse, you gain the ability to pinpoint bottlenecks, track slow jobs and endpoints, and identify your most active users. This article serves as a guide to help you seamlessly integrate Laravel Pulse into your Laravel project, from installation to customization.

Installation

Laravel Pulse's first-party storage relies on MySQL or PostgreSQL databases. If you're using a different database engine, you'll need a separate MySQL or PostgreSQL database for Pulse data. As Pulse is currently in beta, adjust your composer.json file to allow beta package releases:

"minimum-stability": "beta",
"prefer-stable": true

Use Composer to install Pulse into your Laravel project:

composer require laravel/pulse

Publish Pulse configuration and migration files:

php artisan vendor:publish --provider="Laravel\Pulse\PulseServiceProvider"

Run migrations to create tables for Pulse data:

php artisan migrate

Access the Pulse dashboard via the /pulse route after running Pulse's database migrations.

If you prefer not to store Pulse data in your primary database, specify a dedicated database connection.

Configuration

Many of Pulse's configuration options are controlled via environment variables. To view available options, register new recorders, or configure advanced settings, publish the config/pulse.php configuration file:

php artisan vendor:publish --tag=pulse-config

Dashboard Authorization

Access the Pulse dashboard through the /pulse route, initially limited to the local environment. Configure authorization for production environments by customizing the 'viewPulse' authorization gate in App\Providers\AuthServiceProvider.php:

use App\Models\User;
use Illuminate\Support\Facades\Gate;

public function boot(): void
{
    Gate::define('viewPulse', function (User $user) {
        return $user->isAdmin();
    });

    // ...
}

Customization

Configure Pulse dashboard cards and layout by publishing the dashboard view:

php artisan vendor:publish --tag=pulse-dashboard

Customize cards and layout using the Livewire-powered dashboard. Cards accept properties like cols, rows, and expand:

<livewire:pulse.usage cols="4" rows="2" />
<livewire:pulse.slow-queries expand />

Resolving Users

For user-related cards, Pulse records only the user's ID. Resolve and customize user information using the Pulse::user method within App\Providers\AppServiceProvider.php:

use Laravel\Pulse\Facades\Pulse;

public function boot(): void
{
    Pulse::user(fn ($user) => [
        'name' => $user->name,
        'extra' => $user->email,
        'avatar' => $user->avatar_url,
    ]);

    // ...
}

Cards Overview

Pulse offers a range of pre-built cards for monitoring various aspects of your application. These include:

  • Servers

  • Application Usage

  • Exceptions

  • Queues

  • Slow Requests

  • Slow Jobs

  • Slow Queries

  • Slow Outgoing Requests

  • Cache Interactions

Recorders

Pulse uses recorders to capture and store entries from your application. Customize Pulse behavior through recorders configured in config/pulse.php. Common recorders include:

  • Cache Interactions

  • Exceptions

  • Queues

  • Slow Jobs

  • Slow Outgoing Requests

  • Slow Queries

  • Slow Requests

  • Servers

  • User Jobs

  • User Requests

Performance

For high-traffic applications, customize Pulse to reduce its impact:

  • Use a Different Database

  • Redis Ingest

  • Sampling

  • Trimming

Handling Pulse Exceptions

Customize the handling of Pulse exceptions by providing a closure to the handleExceptionsUsing method:

use Laravel\Pulse\Facades\Pulse;
use Illuminate\Support\Facades\Log;

Pulse::handleExceptionsUsing(function ($e) {
    Log::debug('An exception happened in Pulse', [
        'message' => $e->getMessage(),
        'stack' => $e->getTraceAsString(),
    ]);
});

Custom Cards

Extend Pulse's functionality with custom cards using Livewire. Create a dedicated CSS entry point for styling:

<x-pulse>
    @vite('resources/css/pulse/custom-card.css')

    ...
</x-pulse>

Include custom CSS files or leverage Laravel Vite integration for styling. Customize cards and views to suit your application's needs.

Data Capture & Aggregation

Capture and aggregate data using Pulse's efficient system. Record entries with the Pulse::record method and retrieve aggregated data using Livewire's aggregate method. Resolve and display user information using Pulse::resolveUsers.

Custom Recorders

For advanced users, create custom recorders to capture specific events or data points. Register recorders in config/pulse.php and define listeners and recording logic in the recorder class.

In conclusion, Laravel Pulse provides a robust monitoring solution for Laravel applications. Whether you rely on pre-built cards or customize your monitoring setup, Pulse empowers you to gain valuable insights into your application's performance and usage.

Did you find this article valuable?

Support Kasenda's Blog by becoming a sponsor. Any amount is appreciated!