Start your free 14-day ContainIQ trial

Laravel Application Monitoring: Tutorial and Best Practices

June 24, 2022

This article explores the methods for monitoring Laravel applications, including best practices, tooling, and framework-specific knowledge necessary for successful monitoring.

Cameron Pavey
Full Stack Developer

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 Do You 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.

K8s Metrics, Logging, and Tracing
Monitor the health of your cluster and troubleshoot issues faster with pre-built dashboards that just work.
Start Free Trial Book a Demo

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 ContainIQ, are designed specifically for these applications.

Using ContainIQ to Monitoring Lavavel Apps on K8s

ContainIQ is a practical monitoring solution for Lavavel applications on Kubernetes. ContainIQ’s philosophy focuses on doing one thing and doing it well. To this end, the service focuses on Kubernetes specifically and does an excellent job monitoring all Kubernetes-related data. This doesn’t prevent ContainIQ from logging all events from your application, however. ContainIQ accomplishes this by collecting anything that is logged inside your cluster, including both internal applications and Kubernetes system components. You only need to configure your Laravel application’s logging appropriately to output logs into the container. ContainIQ will collect these logs—including exceptions, if that’s what you want—and display them in the visual dashboard alongside your cluster metrics.

An example of ContainIQ’s log dashboard

If you want to do this in your Laravel application, you can configure the logging channel in <terminal inline>config/logging.php<terminal inline> like so:


'default' => env('LOG_CHANNEL', 'errorlog'),

Besides monitoring, ContainIQ can configure real-time alerts based on any data it collects, including server resources and important events. These alerts can then be delivered via Slack so that you won’t miss any important events.

Thanks to clever instrumentation via eBPF, ContainIQ can display detailed latency metrics relating to your application and other Kubernetes services without requiring the installation of any code packages. ContainIQ can use this data to display latency data per microservice and URL path in your dashboard. ContainIQ provides better functionality than a Laravel-specific package, as you get the same insights and more without needing to change your code to suit your monitoring tool and without the extra overhead incurred by adding and loading more packages.

ContainIQ’s Tracing feature set and dashboard gives you an easy way to see trace data by URL path, including latency as mentioned above, but also error rates over time, request details, and associated logs for each path. All of this data is easily searchable and filterable, making it simple to find data relevant to whatever you are investigating at the time.

When running applications in Kubernetes, monitoring server resources becomes more complex, as you now need to potentially keep a tab on resources for multiple clusters. ContainIQ makes this easy by giving you a comprehensive overview of cluster health, showing real-time and historic metrics, including CPU and memory usage.

An example of ContainIQ’s dashboard feature

If you are running Laravel applications in Kubernetes and want to take advantage of ContainIQ’s powerful features, you can get started for as little as $20 per node, per month, which gives you access to the entire feature set, including metrics, logs, latency, events, and alerts.

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.

A service like ContainIQ might be your best option if your Laravel application uses Kubernetes. ContainIQ offers robust insights with minimal configuration and without the need for additional code packages. Logs are automatically collected and displayed in your dashboard, alongside cluster metrics and other valuable data to help you make informed decisions about your application.

Start your free 14-day ContainIQ trial
Start Free TrialBook a Demo
No card required
Cameron Pavey
Full Stack Developer

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. Cameron received a Bachelor of Information Technology from Queensland University of Technology. He is currently living and working in Melbourne, Australia.

READ MORE