Code Execution Monitoring for Symfony applications using Inspector – Tutorial

Valerio Barbera
graphic for real-time code execution monitoring with Symfony and Inspector logos

Hi, I’m Valerio software engineer, founder and CTO at Inspector.

As a product owner, you may have experienced how an application issue can be hard to identify and fix. It can negatively impact the users’ experience or block new potential customers in their onboarding path.

Users don’t spend their time reporting bugs, they just stop using our application if it doesn’t work as expected, looking for another one that fits their needs better.

The idea behind Inspector is to create a monitoring environment specifically designed for software developers. Therefore, it avoids any server or infrastructure configuration that developers hate dealing with.

Thanks to Inspector, you will never have to install things on the server or make complex configurations at the infrastructure level.

It works with a lightweight software library that you can install in your application like any other dependencies. In case of Symfony you can use our official Symfony Bundle.

Developers are not always comfortable installing and configuring software at the server level. These installations are out of their software development lifecycle or are even managed by external teams.

Forcing teams with different goals (Developers, IT infrastructure, Cyber Security, etc.) to work with the same tools can cause confusion. And it can compromise developers’ ability to quickly identify and resolve app problems.

Inspector allows developers to have a code-driven monitoring environment entirely under their control. So they will be the first to know if the application is in trouble. Moreover, it does so “before” users stumble onto the problem.

This functionality drastically reduces negative impacts on the user’s experience. It gives you the proper foundation to run a successful user acquisition process and continually increase engagement with your product. And all with the fewest interruptions possible.

Symfony Code Execution Monitoring: how it works

Inspector is a Symfony bundle to add real-time code execution monitoring to your application, allowing you to work on continuous code changes while catching bugs and bottlenecks in real-time before users do.

It takes less than one minute to get started. Let’s see how it works.

Install the Symfony bundle

Run the composer command below in your terminal to get the latest version:

composer require inspector-apm/inspector-symfony

Configure the Ingestion Key

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

You’ll see installation instructions directly in the app screen:

Create the inspector.yaml configuration file in your config/packages directory, and put the ingestion_key field inside:

inspector:
    ingestion_key: [your-ingestion-key]

Test everything is working

Execute the inspector:test command to check if your app sends data to Inspector correctly:

php bin/console inspector:test

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

By default, Inspector traces:

  • Database interactions
  • Console commands
  • Twig view redering
  • Controllers operations
  • Unhandled Exceptions

Explore your application’s performance metrics

When your application receives a request, Inspector automatically detects the most important events executed to fulfill the request. Then, it creates a visual representation of what happens inside your code during its regular operation.

Transactions stream in your dashboard in real-time, and for each transaction, you can monitor what your application is doing:

Add custom segments to the timeline

Inspector monitors database queries, HTTP requests, and console commands by default. Still, there might be critical statements in your code that you need to monitor for performance and errors. Such as:

  • Http calls to external services
  • Function that deals with files (pdf, excel, images)
  • Data Import/Export processes

Thanks to Inspector, you can add custom segments in your timeline besides those detected by default. This functionality allows you to monitor a custom code block’s impact on application performance.

Let’s look at a real-life example.

Suppose you have an HTTP route that executes some database operations and performs an HTTP call to an external service.

Inspector automatically detects database queries. But, it would be interesting to see the external HTTP request in the timeline to monitor its execution over time and activate alerts if something goes wrong.

class BlogController extends AbstractController
{
    /**
     * @Route("/posts/{slug}", methods="GET", name="blog_post")
     */
    public function postShow(Post $post): Response
    {
        $tags = this->externalService->get('tags');

        return $this->render('blog/post_show.html.twig', compact('post', 'tags'));
    }
}

You can wrap the external service call with a segment to add it to the transaction’s timeline. Type hint the Inspector service into the controller method to access the Inspector instance:

use Inspector\Inspector;

class BlogController extends AbstractController
{
    /**
     * @Route("/posts/{slug}", methods="GET", name="blog_post")
     */
    public function postShow(Post $post, Inspector $inspector): Response
    {
        $tags = $inspector->addSegment(function () {
            return this->externalService->get('tags'); // return value will be transparently returned back.
        }, 'http');

        return $this->render('blog/post_show.html.twig', compact('post', 'tags'));
    }
}

Errors & Exceptions Alerting

Every unhandled exception fired in your Symfony app is automatically reported by default. These reports alert you to unpredictable errors in real-time.

We all wish that every change to our code could be perfect. But the reality is that this is not always the case. Some errors appear immediately after an update, while others pop up at random. It’s a fact of life for developers. Many errors often depend on connection issues between your application and other services.

But, Inspector makes the job easier. It automates the detection of unknown issues. You no longer need to manually check the status of your apps or await reports from users. If something does go wrong, you’ll receive a notification in real-time. And after each release, you stay informed about the impact of the latest code changes.

If your code fires an exception, but you don’t want to block the execution, you can report the error to Inspector for personal monitoring:

try {

    // Your dangerous external call here...

} catch (GuzzleException $exception) {
    $inspector->reportException($exception)
}

Conclusion

When a customer reports that something isn’t working, it forces you to drop whatever you are doing and try to reproduce the scenario. You then have to recapture and reanalyze the logs in your toolset.

Getting an accurate picture of what’s happening in your code can require hours or even days. Inspector makes a massive difference in your efficiency, productivity, and customer happiness because it allows you to avoid this scenario.

Try Inspector for free and as long as you want

To let everyone interested try this new solution, Inspector offers a free tier with up to 30,000 monthly transactions. And it’s not a limited trial. So, you and your team can get familiar with Inspector without the pressure of a deadline.

Create your account

I hope you will try and enjoy the Inspector experience.

Please share the article with others who could benefit from Inspector if you found the article helpful.

Join the “Scalable Applications” community

If you want to learn more about scaling up your application, join Scalable Applications. It’s an international community of professional developers sharing strategies and experiences for building scalable systems.

Join us now

Related Posts

How to configure HTTPS in Laravel Homestead

How to enable HTTPS in Laravel Homestead

Hi, I’m Valerio Barbera, software engineer, founder and CTO at Inspector. In this article I’ll show you how to enable HTTPS for your local applications served by Homestead. I met this need because I am working to implement browser notifications for Inspector using Pusher/Beams. But Beams requires that the application be necessarily served over HTTPS.

Laravel cron scheduling and its secrets

Hi, I’m Valerio Barbera, software engineer, founder and CTO at Inspector. Laravel tasks scheduling is one of the most useful features of the framework.The official documentation clearly explains what it is for: In the past, you may have written a cron configuration entry for each task you needed to schedule on your server. However, this

Laravel validation and custom rules in Inspector

Hi, I’m Valerio Barbera, software engineer, founder and CTO at Inspector. Data validation is one of the fundamental features in any application and it is something developers manipulate almost every day. The value a software provides to users is often a function of the quality of data it is able to manage. Laravel ships with

How to build scalable applications

Read the best news, tips and other direct in your inbox