Introducing Autopilot, an AI coding assistant
gradient
How to use Laravel application monitoring

How to use Laravel application monitoring

Mar 27, 2022
6 min read

Application monitoring is necessary for teams that build and run applications. Properly implemented monitoring can give you insights into how your application performs and identify emerging issues before they are out of control. Such monitoring also empowers developers to solve problems faster by providing access to a trove of historical data about inputs, outputs, and performance at various points in time.

This article explains what you should consider when selecting an appropriate monitoring solution for Laravel applications, as well as some best practices and cautionary advice.

How to get started with monitoring Laravel applications

If you want to implement monitoring for your Laravel application, you need to consider what exactly you want to monitor. Capturing all the necessary data for practical monitoring is crucial when designing your monitoring plan, which often entails information like the following:

  • inbound requests
  • exceptions
  • server resource utilization

Standard tools

Thanks to Laravel’s open and flexible nature, there are many ways you can monitor your application. The official Laravel ecosystem is an excellent place to start. Laravel Forge is a service for deploying and managing Laravel applications. Forge has built-in server monitoring capabilities in its “Business plan” offering. This built-in monitoring allows you to keep an eye on CPU load average, used disk space, and memory, which are all essential resources to keep an eye on. If you already have an established application deployed somewhere besides Forge, fear not; there are many other ways to gain the necessary insights into your application.

Aside from server resources, out of the box, Laravel can also help with general application logging. Laravel includes a useful Log facade that allows you to capture various events, from errors and warnings to general points of interest. Laravel also allows you to configure various channels and drivers, meaning you can save your logs straight to the disk or upload them directly to your preferred cloud-based log manager like Papertrail.

Familiarizing yourself with and configuring Laravel’s exception handling is also worthwhile. Here, you can configure your application to send all errors directly to an external service like Bugsnag or Sentry. Indeed, you can configure these exceptions to handle errors however you’d like and with any service that supports external events via API call.

Popular ways to monitor Laravel applications

One great thing about popular frameworks like Laravel is that if you have a problem, someone has probably already solved it. Smaller applications with less traffic will often be able to satisfy their monitoring requirements simply by configuring their logging to use an appropriate driver and perhaps adding a middleware or two to gather extra data about which routes have been visited and when.

Popular tools for monitoring Laravel applications

For more complex applications, it is often prudent to implement some kind of monitoring solution. There are many monitoring solutions that might be suitable for you. In this context, an application performance monitor (APM) is a (usually managed) service that connects to your application and collects important events, errors, performance metrics, and other valuable data. APMs provide a single place to monitor how your application is performing. The following are a few popular APMs (and their corresponding Laravel packages):

Scout

Scout can offer various insights into your application’s performance metrics, showing you which requests were slow, which common traits are shared between slow requests, and even where in the source code these bottlenecks might originate from. There is a first-party Larvel package for Scout.

Raygun

Raygun offers detailed insights into code execution, including flame graphs and call trees, making it easy to see exactly how the code ran, thus making things easier to debug and fix. There is a third-party Laravel package for Raygun.

Datadog

Datadog is a popular solution for application monitoring, offering code-level visibility into where problems originated, tools for diagnosing slow requests, and post-deployment regression detection. Datadog supports many tech stacks, including Laravel, for which there is a package that you can install.

Sentry

Sentry offers performance monitoring but is particularly well-known for its excellent error-tracking. Sentry provides you with a detailed trail of events, showing you what happened shortly leading up to the offending event. Sentry also supports an assortment of tech stacks, including Laravel, via their package

Notable security concerns for Laravel applications

When using tools like these, you should be aware of how much data you send. For example, if your application deals with private medical data, it is usually subject to many regulations and compliances. If an exception occurs, your logs can leak some of this sensitive information to the APM if you have it configured to send as much data as possible relating to the crash. A more tempered approach might involve anonymizing data before sending it or, better yet, redacting the sensitive data and not sending it at all. Data sensitivity doesn’t just apply to medical data and must be considered when working with any personal data because the APM is unlikely to have the same access rules as your application.

Best practices for monitoring Laravel applications

Laravel gives you a lot of functionality out-of-the-box, and while the tools that it gives you - such as the Eloquent ORM - are quite well-refined at this stage, it is still quite easy to inadvertently write sub-optimal code that can lead to performance issues and bottlenecks further down the line. As such, no matter which tools you decide to go with when it comes to monitoring your Laravel application, there are a few best practices that you can follow:

Monitor all network requests

You should make sure that you are keeping track of not only all incoming network requests from users but also all outgoing network requests to third-party APIs. This data can help with identifying patterns, isolation performance issues, and solving bugs.

Monitor server resource usage

Each request that you receive will use a measure of server resources. Some requests will be more expensive than others, will take longer to run, and use more resources. By keeping a record of resource usage over time and ideally relating it to requests and events, you can get a pretty good idea of which requests are putting strain on the system and can plan to optimize them accordingly.

Centralized log collection

Most of the data traditionally contained in log files, such as HTTP requests and errors, can be extracted and cataloged via different means, but logs remain a valuable source of information when it comes to diagnosing and solving application problems and bottlenecks. Even if you think you can get all of the data you need elsewhere, it is generally a good idea to configure a centralized log aggregation service so that the raw data is there if you do need it.

Collect error details

When an error occurs in your application, it is crucial that you get as much detail about it as possible to give the developers the best chance of replicating and solving the problems. The tools mentioned above are all capable of recording errors to varying degrees, including details such as stack traces, event timelines, and session information. Make sure you check your monitoring tool’s documentation to ensure that error detail collection is properly configured, as misconfigurations can lead to lots of missed detail.

Configure alerting

Many monitoring tools also include alerting functionality where you can configure the tool to send you a message in response to critical events, whether it be via Slack, email, SMS, or some other mechanism. It is a good idea to configure these alerts for events that are important to you, such as unhandled errors or sustained resource usage above a critical threshold. Being alerted to these issues early puts you in a better position to evaluate the situation and determine if urgent action is required.

Use something targeted at Laravel

Choosing the most appropriate services and tools within your budget and constraints is crucial. Purpose-built tools will generally save time and effort compared to more generic offerings. For example, the tools mentioned above have open-source adapters, making them suitable for Laravel applications. However, they may not be the best choice for containerized applications. Containerization tools like Kubernetes introduce new complexities that can require a different approach to monitoring. Some monitoring services, such as Datadog, are designed specifically for these applications.

Final thoughts

As you have seen, monitoring is a crucial component when it comes to developing Laravel applications, so it is important to make sure that you do it right. In this article, you’ve seen how to get started with Laravel application monitoring, some tools that you can use to help get you there, and some best practices that you can follow to help you get the results you want. There are many services that can provide a monitoring solution for your Laravel application. You simply need to think about what you want from your monitoring service now and in the future and narrow down your options appropriately.

If you're looking for an even easier way to monitor your Laravel applications, then Airplane is a good solution for you. With Airplane, you can build powerful internal workflows and UIs for your engineering workflows. Use Airplane Views to build a custom dashboard that makes tracking your Laravel applications easy to do.

To try it out, sign up for a free account or book a demo.

Share this article:
Cameron Pavey
Cameron is a Full Stack Developer at Rexlabs, and before that was a Software Engineer at Catapult. He specializes in Laravel, React, and is also knowledgeable in DevOps.

Subscribe to new blog posts from Airplane.