Laravel real-time code execution monitoring

0

Hi, I am Valerio software engineer from Italy and CTO at Inspector.

As a product owner, I have learned on my skin how an application problem can be so difficult to identify and resolve, creating negative impacts on the user experience or blocking potential new customers from their path. trial period.

Users don’t spend their time reporting bugs, they just stop using our app if it isn’t performing as expected, looking for another one that better suits their needs.

The idea behind Inspector is to create a monitoring environment specifically designed for software developers by avoiding any server or infrastructure setup that many developers hate to manage. Indeed, thanks to Inspector, you will never need to install things on the server or to do a complex configuration at the infrastructure level.

It works with a lightweight software library that you can install in your app like any other dependency. In the case of Laravel, you can use our official Laravel package. Developers are not always comfortable installing and configuring software at the server level, as these installations are outside of their software development lifecycle, or are even managed by external teams.

Forcing teams with different goals (developers, IT operations, cybersecurity, etc.) to work on the same tools can be confusing or compromise developers’ ability to quickly identify and resolve issues within applications.

Inspector is a code-driven monitoring environment completely under the control of software developers, helping them identify bugs and bottlenecks in their applications “before” users encounter the problem.

Its code-driven nature gives you the right tool to run a successful software development process and continually increase engagement with your product with as few interruptions as possible.

Monitoring Lavarel Code Execution: How It Works

Inspector is a composition package for adding real-time code execution monitoring to your Laravel application, allowing you to work on continuous code changes while detecting bugs and bottlenecks in real time. before users.

It takes less than a minute to get started. Let me show you how it works.

Install the composition package

Run the composer command below in your terminal:

1composer require inspector-apm/inspector-laravel

Configure the ingestion key

Get a new ingestion key by signing up to Inspector (https://app.inspector.dev/register) and creating a new project, it only takes a few seconds.

You will see the installation instructions directly on the app screen:

Put the API key in your environment file:

1INSPECTOR_API_KEY=xxxxxxxxxxxxxxx

Test everything works

Execute our test command to check if your application is able to correctly send data to the inspector:

1php artisan inspector:test

Go to https://app.inspector.dev/home to explore your demo data.


By default, Inspector monitors:

  • Interactions with the database
  • Running queued tasks
  • Handcrafted orders
  • Email sent
  • Unhandled exceptions
  • Notifications
  • Redis commands

The next step is to monitor all the execution cycles generated by the http requests.

Monitor incoming HTTP requests

To enable monitoring of http requests, you can use the WebRequestMonitoring middleware as an independent component, so you are free to decide which routes should be monitored based on your route configuration or your personal monitoring preferences.

You can basically attach the middleware in the AppHttpKernel to classify:

1/**

2 * The application's route middleware groups.

3 *

4 * @var array

5 */

6protected $middlewareGroups = [

7 'web' => [

8 ...,

9 InspectorLaravelMiddlewareWebRequestMonitoring::class,

10 ],

11 

12 'api' => [

13 ...,

14 InspectorLaravelMiddlewareWebRequestMonitoring::class,

15 ]

16]

Deploy your code and walk through the execution flow

The next step is to deploy your code to the production environment and see how Inspector automatically creates a visual representation of what’s going on inside your code during normal operation.

Instantly you will see the transaction flows in your dashboard and for each transaction you will be able to monitor what your application is performing in real time:

img

img

Enrich your timeline

Inspector monitors database queries, background tasks, craft commands by default, but there can be many custom statements in your code that need to be monitored for performance and errors:

  • HTTP calls to external services
  • Function that processes files (pdf, excel, images)
  • Data import / export process

Using the package, you can add custom segments to your timeline in addition to those detected by default, to measure the impact that a hidden block of code has on the performance of a transaction.

Let me show you a concrete example.

Suppose you have a queued job that is performing database queries and an http request to an external service in the background.

Job and database requests are automatically detected by Inspector, but it can be interesting to monitor and measure the execution of the http request to the external service.

Just use the inspector assistance function:

1class TagUserAsActive extends Job

2{

3 /**

4 * @var User $user

5 */

6 protected $user;

7 

8 /**

9 * The TagUserAsActive constructor.

10 */

11 public function __construct(User $user)

12 {

13 $this->user = $user;

14 }

15 

16 /**

17 * Handle the job execution.

18 */

19 public function handle()

20 {

21 // Monitring the HTTP request

22 inspector()->addSegment(function () {

23 $this->guzzle->post('/mail-marketing/add_tag', [

24 'email' => $this->user->email,

25 'tag' => 'active',

26 ]);

27 }, 'http');

28 }

29}

You will easily identify the impact of the new segment on the deal timeline:

img

Error and exception alerts

By default, every exception raised in your Laravel application will be reported automatically to be sure that you are alerted to unpredictable errors in real time.

I want every change I make to my code to be perfect. But the reality is, this is not always the case. Some errors appear immediately after an update, while others appear unpredictably. It is a sad fact of life for developers which often also depends on the problems caused by the connection between our application and other services.

However, Inspector makes the job easier. It automates the detection of unknown issues so that you no longer need to manually check the status of the application continuously or wait for reports directly from users. If something goes wrong, you will receive a real-time notification, and after each release you can stay informed about the impact of the latest code changes.

If your code throws an exception but you don’t want to block execution, you can report the error manually for personal monitoring.

1try {

2 

3 // Your dangerous code here...

4 

5} catch (GuzzleException $exception) {

6 inspector()->reportException($exception)

7}

If the code fails, you’ll be alerted in real time to your inbox to take a look at the error as it appears.

img

You even have access to the detailed information collected by Inspector in real time:

img

Conclusion

When a customer tells you that something isn’t working, it forces you to give up whatever you’re doing and start trying to reproduce the scenario, and then grab and reanalyze the logs in your own toolset.

Getting a true picture of what’s going on can take hours or, in my experience, even days. Inspector can make a huge difference in efficiency, productivity and customer satisfaction.

New to the inspector?

Get a monitoring environment designed especially for software developers by avoiding any server or infrastructure setup that many developers hate to manage.

With Inspector, you never need to install things at the server level or do complex configuration in your cloud infrastructure.

Inspector works with a lightweight software library that you can install into your application like any other dependency. Developers are not always comfortable installing and configuring software at the server level, as these installations are outside the software development lifecycle, or are even managed by external teams.

Try the inspector.

Visit our website for details: https://inspector.dev/laravel/


Source link

Share.

Comments are closed.