How Sentry Can Improve Your Laravel Application

Published on by

How Sentry Can Improve Your Laravel Application image

Sentry is a popular error and application performance monitoring platform that can be easily integrated into a Laravel application to help you identify and fix performance issues quickly. With Sentry, you can capture errors, crashes, and performance problems that occur within your application and receive alerts to take immediate action.

In this post, we will cover how to install and set up Sentry in a Laravel application, as well as how to explore some of Sentry's features that are valuable for improving application performance.

How to install & set up Sentry in a Laravel Application

Getting started with Sentry takes just 5 lines of code - no agents required. Sentry's automatic instrumentation captures transactions and spans, making it easier to trace the root cause of any issues that occur within your application.

To get started with monitoring your Laravel application, install the sentry/sentry-laravel package with Composer:

composer `require sentry/sentry-laravel

Then, add Sentry reporting to App/Exceptions/Handler.php

public function register(): void
{
$this->reportable(function (Throwable $e) {
\Sentry\Laravel\Integration::captureUnhandledException($e);
});
}

Lastly, run this Artisan command to configure the Sentry DSN:

php artisan sentry:publish --dsn=<paste-your-DSN-here>

Check out the documentation for the latest instructions. Now, in order to activate Sentry's performance monitoring, you'll want to configure your sample rate for transactions by either:

  1. Setting a uniform sample rate for all transactions using the traces_sample_rate option in your SDK config to a number between 0 and 1. (For example, to send 20% of transactions, set traces_sample_rate to 0.2.)
  2. Controlling the sample rate based on the transaction itself and the context in which it's captured, by providing a function to the traces_sampler config option.

These two options are meant to be mutually exclusive. If you set both, traces_sampler will take precedence.

// Specify a fixed sample rate:
'traces_sample_rate' => 0.2,
// Or provide a custom sampler:
'traces_sampler' => function (\Sentry\Tracing\SamplingContext $context): float {
// return a number between 0 and 1
},

While you're testing, set traces_sample_rate to 1.0, as that ensures that every transaction will be sent directly to Sentry.

Once testing is complete, we recommend lowering this value in production by either lowering your traces_sample_rate value, or switching to using traces_sampler to dynamically sample and filter your transactions.

And that's it! See this documentation for more tips on how to improve the response time of your PHP application.

Use Sentry to detect performance issues

If left untreated, performance issues can fester and grow into full-blown errors. Taking action (before it's too late) is easy in Sentry — you can triage, assign, and resolve performance issues directly from your issues feed.

Sentry alerts you on latency issues in your application that you can actually fix - no deciphering dashboards or researching span trees required.

Sentry detects a number of backend performance issues like N+1 database queries, consecutive database queries, and slow database queries. With Sentry, you can:

  • Get notified about your performance issues via email or one of your favorite workflow tools like Slack, Pagerduty, or Jira.
  • See the most relevant spans in a condensed span tree to find the root cause faster in your code (similar to a stack trace for error issues. You can also expand the span waterfall to see more of your issue evidence.
  • Triage, assign, and resolve issues directly in your issues feed.

Instead of manually monitoring transactions over time, you can let Sentry tell you exactly what's wrong in your code in order to quickly take action.

Understand your overall application health

In addition to alerting users on specific performance issues, Sentry provides detailed information about slow transactions, database queries, and HTTP requests in a transaction summary. This provides a high-level overview of the performance metrics you care about — whether it's a p95 duration or failure rate of your transactions.

You can also decrease troubleshooting and manual searching through logs with suspect spans that make it easier to find slow requests that are impacting your users the most.

To get a holistic view of your application health, you can also create custom dashboards to align on performance KPIs that your team deems business-critical, plus see any trends that indicate significant changes in performance over time in your application.

The benefits of using Sentry for Laravel application monitoring

Connect Backend and Frontend Issues

Sentry's distributed tracing allows for seamless connection between backend and frontend issues, enabling developers to effectively monitor and diagnose problems in their applications.

You can trace slow-loading pages all the way back to poor-performing API calls — and surface any related errors — to get to the root cause of a performance issue faster.

If you use Sentry Performance on your Javascript projects, you can also connect traces for complete visibility. You can do this

  1. For requests that start in your backend, by adding a meta tag in your HTML template that contains tracing information.
  2. For requests that start in JavaScript, by the SDK setting a header on requests to your backend.

Profiling for Code-Level Insights

Sentry's latest feature, Profiling, gives code-level insight into performance bottlenecks in production – allowing you to speed up troubleshooting and optimize resource consumption. It is now available for PHP, Python, Node, Android, and iOS users.

Profiling allows you to:

  • Drill into the exact functions and lines of code, causing a latency issue without writing performance tests or searching through spans.
  • Optimize resource consumption by using flamecharts to identify hot code paths before users feel the impact of a slow request.
  • Test your application performance in any environment, including in production, with minimal overhead.
  • Fill missing instrumentation gaps with insights into your code execution - no need to manually add in spans.

Improve your Laravel application with performance monitoring

Sentry is a powerful tool that can help you improve your Laravel application by identifying issues quickly. By following the steps outlined in this post and exploring some of Sentry's features, you can ensure that your Laravel application is performing at its best and delivering a great experience from beginning to end for your users.

Eric L. Barnes photo

Eric is the creator of Laravel News and has been covering Laravel since 2012.

Filed in:
Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes.

Visit Larafast: Laravel SaaS Starter Kit
Laravel Forge logo

Laravel Forge

Easily create and manage your servers and deploy your Laravel applications in seconds.

Laravel Forge
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
No Compromises logo

No Compromises

Joel and Aaron, the two seasoned devs from the No Compromises podcast, are now available to hire for your Laravel project. ⬧ Flat rate of $7500/mo. ⬧ No lengthy sales process. ⬧ No contracts. ⬧ 100% money back guarantee.

No Compromises
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Lucky Media logo

Lucky Media

Bespoke software solutions built for your business. We ♥ Laravel

Lucky Media
Lunar: Laravel E-Commerce logo

Lunar: Laravel E-Commerce

E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.

Lunar: Laravel E-Commerce
LaraJobs logo

LaraJobs

The official Laravel job board

LaraJobs
Larafast: Laravel SaaS Starter Kit logo

Larafast: Laravel SaaS Starter Kit

Larafast is a Laravel SaaS Starter Kit with ready-to-go features for Payments, Auth, Admin, Blog, SEO, and beautiful themes. Available with Vue and Livewire stacks.

Larafast: Laravel SaaS Starter Kit
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Rector logo

Rector

Your partner for seamless Laravel upgrades, cutting costs, and accelerating innovation for successful companies

Rector

The latest

View all →
Configuring Laravel With Additional Environment Files image

Configuring Laravel With Additional Environment Files

Read article
Real Time Command Line Monitoring With Laravel Top image

Real Time Command Line Monitoring With Laravel Top

Read article
A New Validation Rule and the Ability to Manually Fail a Command in Laravel 11.8 image

A New Validation Rule and the Ability to Manually Fail a Command in Laravel 11.8

Read article
New Proposed Array Find Functions in PHP 8.4 image

New Proposed Array Find Functions in PHP 8.4

Read article
Laracon AU 2024 tickets are now on sale image

Laracon AU 2024 tickets are now on sale

Read article
Dash UI is a Laravel Blade Component Library Inspired by Shopify Polaris image

Dash UI is a Laravel Blade Component Library Inspired by Shopify Polaris

Read article