Introducing Laravel Telescope

Tram Ho

This article is translated from the Introducing Laravel Telescope so the examples and illustrations I ask for permission are kept from the author.

General

Laravel Telescope is a debugging support package for Laravel application development, written by Mohamed Said and Taylor Otwell. Laravel Telescope is open source, free on GitHub and was released on October 19, 2018.

It is like a third-party depedency and installation through Composer. After successful installation, you will access it via the route /telescope in your project.

What is Laravel Telescope?

If you’ve ever used the Clockwork or Laravel Debugbar, it would be interesting to know that Laravel Telescope has a separate UI and special powers.

The telescope is like a bodyguard protecting a rich lady lady. Telescope will “pay attention” to all requests that come into your system, whether from HTTP requests, from the command line, from the scheduler or from the queue.

These “guards” will capture all kinds of information about requests or related data – things like database queries and execution time, execution times. cache (cache hits) and misses, events fired, mail and more.

Here is the “Black list” that Telescope always follows:

  • Requests
  • Commands
  • Schedule
  • Jobs
  • Exceptions
  • Logs
  • Dumps
  • Queries
  • Models
  • Events
  • Mail
  • Notifications
  • Cache
  • Redis

Tabs / watchers

Each of these tabs will display what it “tracks” for each tab. Let’s go through each tab to see what it has.

1. (HTTP) Requests

This tab allows you to see all HTTP requests coming into your system. You will be able to review all HTTP requests and related information for each request.

Each request of the page also displays any data it has from other users related to this request. For example, all database queries and query times; which user is authenticated for this request; and more.

2. Commands

The commands tab allows you to see a list of all the commands that have run and their exit codes. As you drill in, you can also see all of their arguments, options, and related items.

3. Schedule

List scheduled tasks that have been run. On the details page of each task, see all scheduling information as well as cron schedule.

4. Jobs

The jobs tab lists all jobs that have been run or are running. It is similar to Horizon, but Horizon is Redis-only and has no user interface, besides, it also interacts with the running queue works. On the other hand, Telescope has a user interface, and also works with all queue drivers.

On the jobs page, you will see the job name, queue, connection and status when the job runs.

On the job detail page you will be able to see all job data and a few other things: hostname, job’s fully-qualified class name, connection, queue, # of tries, timeout, tags.

Jobs is automatically tagged with any attached Eloquent model (e.g. AppVideo:1 ), with the user if there is an attachment, etc.

Tags.

Items such as requests, commands, etc. will be automatically tagged by Telescope (for example, if it is a user’s request, it will automatically be assigned the Auth:1 tag to User 1; you can click on the tag and it will will only filter items tagged with User 1, etc.)

As with HTTP requests, you can view all sorts of information related to this job, such as the database queries it triggered, the job ran, and the logs it generated.

If you kick off a closure instead of seeing AppJobsRenderVideo then you will see the Closure (web.php:43) is displayed.

New queued closures.

Taylor built a new library to bring queued closures back, which Laravel once had but disappeared some time ago. With this new library, if you use a model to import it into closures, it will store the model ID, not the entire model.

5. Exceptions

Keep a record of all exceptions and allow you to check for each exception. This tab will show you the same data as other tabs, like hostname, type, request, tags, authenticated user.

You will see the code position, highlighted, with a few lines of code above and below it to easily visualize what the paragraph is doing.

You may also receive a link to an exception detail page from the request when it was thrown.

NOTE: In many tabs, if you’re on an individual page (eg the page for a given exception) you will get a link to the request page that generated that one

If the same exception happens multiple times, they will be grouped on the list page, but you can still drill down into individual exceptions from the exception display page.

6. Logs

On this logs tab will show basic log message, level, when it happened.

When you visit the logs’ details page, you can view more information including all the context data you have passed to the log entries (as an array).

Beautiful and easier to understand than before, compared to digging through the heaps of previous log characters

If passed into an array you can see all of that data, in addition to see which requests trigger the log, which users have enabled.

7. Dump screen

This is one of my favorite features

If you use the dump() function and you open it in Telescope, you will see that the data is dumped in Telescope and not on your system interface. Similarly, with dd() , you will also see the data in Telescope without interruption when you operate on the system. Each dump() function will also link to the request that created it.

If you do not use this tab anymore, then the dump appears on your system interface.

8. Queries

A list of all queries to your DB – like Debugbar. See the full query, which request has been called, how long it takes etc.

The view has been nicely formatted.

It is possible to set a limit for queries in a service provider. Once a query takes longer, it will be tagged slow and also marked in red in the list.

9. Models

You can see actions like create, update, delete to see what changes have been made.

10. Events

Display the list of all events, you can see which events were broadcast with a tag, see the list of all generated listeners.

11. Mail

Displays a list of all emails that have been sent, who is the recipient, when they are sent, whether it is queue and after completion of the queue. You can view subject email and when you go deeper you will see an email preview like MailTrap.

You can even download the raw .eml file and open it on your device.

12. Notifications

Display all notifications, notification types, etc.

There is no preview because some notifications are not previewable, but if it’s an email notification, you can preview it.

If the notification is placed in the queue, you can also view it in the Jobs section on the request.

13. Cache

Show cache hits and misses and updates etc.

Display key, data, when it expires, see which request calls to it and also on the request page, you can view all cache / misses accesses.

14. Redis

Similar to cache.

You’ll also see how long it takes, when it calls, what requests to call, etc.

15. Authenticated user

Get verified user information on any tab.

16. Authorization

The list of emails in the telescope service provider can be viewed to see which users have access rights in the system.

Or use viewTelecope to determine if a specific user can access it.

17. Filtering

You may not want to store everything that happens in produciton, so you can filter them out, in the Telescope service provider, run Telescope::filter(function ($entry))

You can fix this if you like.

18. Monitored tags

This section helps you track a certain tag. You can see all recorded requests related to that tag until you unfollow that tag.

Note: Horizon and Telescope are both good on this part, if you are using Redis queues.

19. Prune

The Schedule job will remove the old entries from Telescope. Can run nightly if you want to delete content older than x hours.

Also a setting in config / telescope

Can enable or disable any watchers. For example, WatchersCacheWatcher::class can be disabled.

In addition, TELESCOPE_LIMIT defaults to 100, which means holding 100 queries at a time, 100 Redis, and so on. This can be configured in .env .

20. Miscellaneous

Laravel Telescope also has night mode, you can turn it on with Telescope::night()

Q&A

  • Where is the data stored? Redis, in addition there are 6-7 ways to choose from.
  • How much data storage? About 100 each time.
  • Is it possible to get notifications for Slack? Have.
  • Is it possible to filter by time? Not now, but it is open source you can fix it.
  • Has its impact on boot time (has speed effect)? Only one query will execute at a time. We should only use it on staging environment, since it is a debug tool.
  • Is it possible to test multiple applications in one user interface? Yes, just show them their login information in the same database.
  • Which version of Laravel is compatible with it? 5.7.7+.

References

https://mattstauffer.com/blog/introducing-laravel-telescope/

My article was translated from the link above. Thank you for reading (bow).

Share the news now

Source : Viblo