Laravelvideo· Jul 3, 2025· 9:19· 2,602 views on YouTube

How to use Event Sampling in Laravel Nightwatch

Watch on YouTube ↗
0:00

React at a moment you like. The best moments light up here.

transcript

21 moments
  1. Here's the thing. If you're someone who's already gotten the chance to use Laravel Nightwatch, especially if you've installed it into one of your productionready applications, then you've probably used up your events that you were allotted a lot faster than you expected. I mean, yeah, Laravel Nightw watch is great, but you have a lot of requests, you have a lot of cues, you have a lot of jobs, you have a lot of things going on in your Laravel application, and so that quota was hit a

  2. lot faster than you wanted. Now, here's the good news. Today, July 3rd, we're increasing your quota by 50% across all plans. So, in this video, I want to talk about how you can make those 50% more events count when it comes to Laravel Nightw Watch monitoring your Laraveville application. It all comes down to this one word, sampling. We mention it in the docs that nightwatch captures everything by default. So without sampling or

  3. filtering it will collect 100% of your requests, your jobs, your queries, your exceptions and more. It gives you complete visibility so that out of the box you have everything that you might need in order to track the things that happen within your Laraveville application. But each Laravel application is different and so what your Laravel application is doing might not necessarily fit with the quotas allowed for events within your particular nightw watch plan. So, while

  4. sampling is a great option, you can also add additional events on top of your given plan. So, if you go into your billing and then add event limits from there. So, you can add events per month where you're saying, I'm paying for my given plan, but I'm also adding events on top of that. Setting additional monthly event limits is great, especially if you have a month or maybe a given couple of months that you know

  5. you're going to have more requests, more jobs, more fillin the blank happening within your Laravel application. But then sampling comes in when you know that everything that you send to Laravel Nightwatch uh doesn't necessarily need to be captured in order to get the value of Laravel Nightwatch. So you might just want every exception but not necessarily every request and that's where those sampling parameters come in. So here's three ways you can use the different

  6. kinds of sampling that Laravel Nightwatch allows to get the most out of your events. The first way and the easiest way is changing the environment variables that are recommended as a starting point for Laravel Nightwatch. Of course exceptions are always going to be sampled. So regardless of what we change this command or request sample rate within Laravel Nightwatch, all exceptions will come through. Now you can change this exception sample rate to

  7. anything lower if you only wanted to sample that many number of exceptions. If that can get confusing when you have maybe an exception sample rate of 50% but a request sample rate of 10%, let me walk through what that means. If you have request sampling set to 10% and exception sampling set to 50%, the nightw watch is going to capture all exceptions from the sampled requests and

  8. capture 50% of all uh nonsampled requests as well. So let's say you wanted to only capture exceptions from all 10% of your sampling. So you only want 10% of anything that happens within your Laravel application, including exceptions to be pushed into Nightw Watch. Well, then you can set the Nightwatch exception sample rate to zero. And as long as you have request

  9. sample rate or command sample rate to a specific percentage higher than zero, then exceptions are going to come through those samples. So those environment variables work great if you want to generally set the sample rate of what flows into Laravel Nightbot. But what if you want to be more granular, more specific? Well, these next two sampling techniques that I want to show you fall into that category of diving into granular specificity of how you

  10. sample in your Larvo application. Starting off with routebased sampling. Let me paint a picture for you. You probably have critical routes that you would like all requests to get pushed into Laravel Nightwatch. Something like payments or maybe related to your users, but there's other routes that you just don't necessarily need anything or maybe just a small select few requests. Something like health checks, for example. With route-based sampling, we

  11. can make that happen relatively easily. For example, using this code, 50% of my slash users get requests are going to be sampled into Laravel Nightwatch. Something to note is this resets any overall general environment variable that you might have set for requests. So let's say you have requests set to 10% within Laraveville Nightwatch, but then within this particular route you have it

  12. set to 50%. That's not 50% of 10%. That is 50%. it completely negates the environment variable at that point. You might have already noticed this, but it's not just a function callback that allows you to set that sample rate. In fact, it's its own middleware. So, you can imagine the use cases for setting that particular sample rate using middleware. You can use things like uh route fallbacks for unmatched routes or

  13. even within a package to say that anytime this package or its routes or its requests I want to sample at a particular rate because nightw watch's sample middleware gives you that option. But it's not just middleware that you can use throughout your Laravel application. It's also this nightw watch sample method. Here's where you can get even more granular detail on what you are setting to sample within your

  14. Laravel application because there might be some uh jobs there might be some particular functions or methods that happen within a given request in your application that you want to make sure they are sampled 100%. While other things or even other routes might be less than that. So yeah, this is where you can get that fine grain control to say I only want to sample this particular method or function

  15. within your application 50% of the times that that method or function actually runs. Or you can have this handy don't sample method to make sure that hey I don't necessarily need this being sent to nightw watch. These nightw watch sample methods are great for the things that Larbell Nightwatch might not have that fine grained control just yet. Well, you can make it happen on your own. And then lastly, while this isn't necessarily sampling per se, an event in Nightw Watch can span across your whole

  16. application. It can be a request, outgoing requests, notifications, jobs, queries, mail, commands, cache. It can be scheduled tasks, events exist within your layer application, whatever it is doing or sending. So, what if you don't necessarily want to capture all of those or maybe just some of those requests within your application, but not everything else without having to dive into that granularity within your

  17. application to say, "Okay, I'm using that nightw watch sample method." Well, what can we do? We can use event filtering. This is similar to sampling, but you can filter out specific events entirely. You can filter out specific events and have granularity control over which events you're filtering out based off of these callback functions. Or maybe you only want to reject queries that match particular set of parameters. For example, this is helpful within

  18. Postgres or MySQL. They have this on the docs that are linked below that you can reject queries that are specifically for jobs or for cache because Laravel Nightwatch unless you've said otherwise is already accepting jobs and cash as part of those events. But you might see that this is where you can get even more granular within your application. There might be a specific notification, an outgoing request, or even a mail event that you always want to reject without

  19. rejecting everything else. And then of course you can also reject all of those events as a whole. So this would live in your environment variable within your production setting to say you want to ignore things like mail notifications, outgoing requests, cache events, and queries. So overall, you can see that there really isn't any limit to what you can modify in order to send the things that you actually want to send to Laravel Nightwatch or limit the things

  20. that you want to limit that is being sent to Laravel Nightwatch. Whether it's something within your environment variable to say, hey, I only want to sample this many across the board or it's within your code to say I only want to sample this many of this particular function or method or this many of this particular route requests. It comes down to what do you want to monitor within your application to make your application better. If you liked this video and you haven't gotten the chance

  21. to sign up for Laravel Nightwatch, you can do that for free today. Check out the documentation that is linked in the description below. And if you want to dive into what is happening within Laravel Nightw Watch at an even more granular state, there was a whole AMA done with Jess Archer, the lead developer for Laravel Nightw Watch, and you can find that up here.

just you here