Conserving Sentry Transactions by Ignoring Laravel Routes
Yet another case of trying to find an answer to a very particular problem, coming up empty, solving it myself, and posting about it for future generations.
Update 2023-04-01anchor
Thanks to Michi Hoffmann, this is now even easier to manage with a built in ignore_transactions
configuration option now available as of release 3.17.0 of sentry-php!
Now, instead of using the before_send_transaction
config I swapped to using ignore_transactions
and my code changed to the following:
'ignore_transactions' => ['/_debugbar', '/monitoring', '/pleaseignoreme'],
Below is the original post for posterity, but with the addition above that solution is no longer necessary.
The Problemanchor
I have a couple Laravel PHP routes that are triggered quite often, both are very specific to other site monitoring (is the site down?) and session management (am I still logged in?). These being utility routes that don’t serve a user-facing purpose, I don’t need any additional logging details about them from Sentry.
Can I prevent the transaction from being sent to Sentry before it even leaves my server? I tried searching terms like “sentry laravel ignore route” and “sentry control transactions from PHP” to no avail so I got to work.
The Solutionanchor
In my searches, I did find this helpful documentation for before_send_transaction specifically for Laravel. It’s an optional addition to the /config/sentry.php
configuration. In it, you can define any logic you want to return null
in preferred situations. This null return will lead to the event being discarded. Bingo!
Here’s the code I worked up.
'before_send_transaction' => function (
\Sentry\Event $transaction
): ?\Sentry\Event {
$ignore = ['_debugbar', 'monitoring', 'pleaseignoreme'];
$request = $transaction->getRequest();
$check = array_filter($ignore, function ($url) use ($request) {
if (stripos($request['url'], $url) !== false) {
return true;
}
});
if (count($check) > 0) {
return null;
}
return $transaction;
},
Here’s what it’s doing:
$ignore
is the array of URL key terms I want to ignore, this is all you need to edit. Add any URL segments that you’d prefer not to be logged by Sentry and you good to go$request
is the request details coming from the packaged$transaction
generated by Sentry. In it is the full URL that we use to test against$check
was my method of iterating through the ignore list and checking for a match. This filters the$ignore
array down to just matches.- We then see if the count of
$check
is greater than zero, if so then one of the routes matched and we want to returnnull
to ignore this transaction. Otherwise, return as normal.
Closure Alternativeanchor
Shout out to Alex Bouma for also noting in this Github Gist how to avoid using a closure (as I had in the solution above) because it makes php artisan config:cache
no longer work. Something I hadn’t noticed until after I had the above solution in place.
If you choose to continue using before_send_transaction
and need config caching, give a class a try!
That’s it!anchor
Pretty simple, but it took a bit of digging through documentation to find before_send_transaction
and through the Sentry package to see what $transaction
contained so I’m hoping to spare the next dev that bit of trouble.
Onward and upward! 🚀
Have some thoughts or feedback about this blog post?
Get a conversation started on LinkedIn or Twitter, or send me an email.
Webmentions
What's this?2 Replies
-
This adds two new options, namely `ignore_exceptions` and `ignore_transactions`. ### ignore_exceptions Make it dead simple to ignore an Exception, without the need to deal with any integrations. I actually marked `IgnoreErrorsIntegration` as deprecated. Additionally, we take `$exception->getPrevious()` into account, when applying the option. ```php \Sentry\init([ 'ignore_exceptions' => [BadThingsHappenedException::class], ]); ``` closes https://github.com/getsentry/sentry-php/issues/1426 ### ignore_transactions Same story, make it dead simple to ignore a transaction, without the need to fiddle around with the `traces_sampler` or any `before_send_transaction` stuff. ```php \Sentry\init([ 'ignore_transactions' => ['GET /health'], ]); ``` Got inspired after reading https://stevenwoodson.com/blog/conserving-sentry-transactions-by-ignoring-laravel-routes/.
-
I've been running out of Transactions in Sentry and realized it's because of some utility routes that are called quite a bit. I don't need these to be logged so I figured out how to ignore them.If you're using Sentry to track your Laravel app, maybe this will help you too! 🚀