Real-time monitoring for Laravel applications
Laravel top is a real-time monitoring tool that you install in your Laravel application to monitor requests.
Everything I said
Today, I wanna highlight a third-party package that helps you monitor your Laravel applications. I know I talk a lot about first-party packages, but there are even more good third-party packages. So I'm gonna show you Laravel Top as a way to keep track of what's going on in your application real time. Let's get started. I'm gonna start by saying Laravel new top, and we'll do no starter kit.
No, we don't need any of that. This is gonna download and install Laravel for me and create a new project, and we'll choose SQLite, and we'll change into Top, and I think it migrates for you. Yeah, there's nothing to migrate. After that, we're gonna composer require, uh, leventcz, don't really know how to pronounce that, slash laraveltop.
So we're gonna pull that in, and then we can open this in PhpStorm and start poking around. Actually, let's go ahead and publish. I think it's vendor publish, and is there a... Yeah, let's publish whatever they have in there. So there is a configuration file, which means we can go take a look at some stuff. So let's open this in PhpStorm, and go to the config file and see if there's anything good there.
Um, looks like you can change your Redis connection or change recording mode. Uh, I don't think we're gonna do either of those things right now. Let's go to our ENV file. My Redis connection is on 6380 because it's powered by Laravel Herd. And so if we open up Herd and look, we see 6380.
So I need to change that to 6380, and I have. Laravel Top is a way to monitor some high-level stuff about your application, including request response time, database query time, cache hits and misses, et cetera. Um, the way that it works is it listens to Laravel's native events, of which there are many, and Laravel fires these events for a query completed or a cache hit or miss or response, uh, sent, that sort of thing.
So it listens to all of those and then writes it into Redis, and then gives you this command where you can watch it. So if you run PHP Artisan Top, you see that you're sent into this little dashboard. So it's continually running, but we're not sending any requests yet. But if we were to send some requests just to top.test, you'll see stuff starting to fly in here.
This is super cool. So you see it's live updating. We get requests per second over here, um, queries, cache. We're not running any cache yet, uh, so let's go ahead and add a few things. Let's come over into web.php, and we'll duplicate this guy and call that cache and return, uh, how do we wanna do this?
Let's go ahead and make a key of key RANDBETWEEN one and 100, and we'll return cache-remember. So we'll populate it if it's not there for now, plus how many seconds? AddSeconds. Let's do RANDBETWEEN, I don't know, two and 10 seconds, and then the callback is gonna just be cached.
So this is going to pull it out of the cache, or if it's not there, populate it in the cache, um, and store it for between two and 10 seconds. So now we should see some cache hits and misses. Um, let's go ahead and, not that one, this one. Let's fire that up there again, and we can fire it up on cache as well.
So we'll see two routes are now being hit, and we see the hits and misses are starting to populate over there, which, this is pretty cool. I can see this being extremely useful on a real application. Let's do a database one as well. So here, we'll just call this DB, and this will be easy. We can just say, uh, DB select, select star from users.
Uh, semicolon outside simplify, uh, import, sure. And we'll just do that. So now we'll come back over here, and while those are running, we will also fire up some hits against the DB route, and we should see there's the DB route and the queries. There you go. They start to fly up.
This is a very cool way to monitor the web side of your Laravel application. It doesn't monitor queued jobs or commands, um, but it does write all the data to Redis. So if you do have multiple servers, multiple web servers, this will aggregate all of that data together because it's writing it to a central repository, which is Redis. Um, another thing you could use to monitor your application is Laravel Pulse, which is a first-party package from the Laravel team.
It is similar in that it aggregates statistics, um, but puts it in a web dashboard, and also it makes it very easy to write your own, uh, aggregators. I have a video on creating a custom pulse card that you can watch here. Until the next time, see ya.