Laravel Telescope Configuration For Production
Programming #Laravel# Laravel Telescope

When Laravel had released Telescope for the first time back in Octorber 2018, I was quite excited. I remember trying it out within a day or two and I'm still using it for all my projects. It is such a nice tool from Laravel and setup is breeze. However, I always faced some configuration issue while using it in production such as what to keep and what to discard. So I'm going to share some of the production ready tips that I generally follow for all our production applications.

There are two things I generally do in the TelescopeServiceProvider's register() method.

1. Information to Store in Telescope

The first setting that I follow looks something like this.

// Hides sensetive information such as passwords, access_tokens etc.
$this->hideSensitiveRequestDetails();

// Allowing all logs to be saved while developing
// For production only allowing certain things that are needed.
Telescope::filter(function (IncomingEntry $entry) {
    if ($this->app->environment('local')) {
        return true;
    }

    return $entry->isReportableException() ||
           $entry->isFailedRequest() ||
           $entry->isFailedJob() ||
           $entry->isScheduledTask() ||
           $entry->isException() ||
           $entry->hasMonitoredTag() ||
           $entry->type === EntryType::LOG ||
           $entry->type === EntryType::NOTIFICATION ||
           $entry->type === EntryType::COMMAND;
});

The setting is somehow same how Laravel provides by default, but I enable the Log for all of my projects especially that helps me backtrack issues quickly. Other entry type are optional, and can be enabled if required. It helps resolving the issues faster. These are the other entry types that can be enabled / disabled if needed.

class EntryType
{
    public const BATCH = 'batch';
    public const CACHE = 'cache';
    public const COMMAND = 'command';
    public const DUMP = 'dump';
    public const EVENT = 'event';
    public const EXCEPTION = 'exception';
    public const JOB = 'job';
    public const LOG = 'log';
    public const MAIL = 'mail';
    public const MODEL = 'model';
    public const NOTIFICATION = 'notification';
    public const QUERY = 'query';
    public const REDIS = 'redis';
    public const REQUEST = 'request';
    public const SCHEDULED_TASK = 'schedule';
    public const GATE = 'gate';
    public const VIEW = 'view';
}

2. Slack Notifications

Slack Notification is something that is very useful and I'm using it on all our production applications makes it very valuable when getting realtime crash report if something goes wrong on the application. You can use other services as well off course. This is the small snippet I use that triggers a slack notification when something goes wrong in the application, helps me take quick action.

if (app()->environment('production')) {
    Telescope::afterStoring(function (array $entries, $batchId) {
        foreach ($entries as $entry) {
            if ($entry instanceof IncomingExceptionEntry) {
                logger()->channel('slack')->critical(
                    $entry->exception->getMessage(),
                    [
                    'environment' => app()->environment(),
                    'url' => app()->runningInConsole() ? 'CLI' : request()->method() . ' ' . request()->fullUrl(),
                    'user' => $entry->content['user'] ?? '-',
                    'view in Telescope' => url('telescope/exceptions/' . $entry->uuid),
                ]
                );
            }
        }
    });
}

This sends the notification to Slack after storing the Telescope entry with user information, error information, as well as Telescope link that helps me directly visit the telescope entry.

Tip

I also prune telescope entries periodically. I use 7 days, 30 days, and 60 days based on the nature of the application.

// Deletes records that are older than 7 days.
$schedule->command('telescope:prune --hours=168')->daily();

So these are the two tweaks using Telescope I generally follow for production.

I hope this helps. If you have any doubts, questions, or suggestions to improve it, please comment below or find me on twitter, I will listen to you 😉

2 minutes read 24th Jun 2021

About

A entrepreneur, running an early stage venture, VizitDoc and a MVP-First Service Based Companay ThecodeWork.

A dreamer, having care for alternative education.

Archives - 1

  • Code

    20 Articles

    List of code snippet and articles I wrote when I started blogging in early 2016 😃

Codementor badge