December 202321:4211.9k views

Customizing Laravel Pulse

About

A tutorial on creating a custom Laravel Pulse card.

Transcript

Everything I said

00:00

The team at Laravel has just hit us with a new first-party package. We love it when this happens. Maybe a week ago, Laravel Pulse was released. It's a kind of a lightweight application performance monitoring tool to keep track of a bunch of stuff going on in your app. This is not the Pulse overview video. I have one of those. This is a video on how to create a custom card for Laravel Pulse.

00:22

We're gonna create this Composer outdated card, which will show you outdated dependencies in your application. Um, it's a, it's a pretty basic card, but it's a good introduction to how, how to create your own Laravel Pulse card. So let's get started by installing Laravel Pulse. The first thing we need to do, as always, is require Laravel Pulse, and we'll pull that down.

00:43

You do need to set your minimum stability to beta, uh, depending on when you're watching this video, because it is in beta still. So we've installed that, and let's do PHP Artisan vendor publish and publish, publish, and let's see what we have for Pulse. I'm gonna go ahead and publish the configuration, and I'm going to publish the dashboard as well.

01:09

So if we hop over to PHP Storm and look in Pulse, you'll see this is the Pulse configuration. So this is where they've given you in a lot of places that you can hook in, and this is where we're gonna start to hook in with our own custom card. One of the things that we really need to understand, um, when looking at this configuration file is the concept of recorders. So Pulse ships with a lot of first-party recorders, and we're gonna actually write our own recorder, but what is a recorder?

01:33

A recorder is the thing that responds to a Laravel application event and records data. That's why they named it recorders. Pretty good name. They're very good at that over there. So all of the recorders listen to application events, and then they write data probably into these Pulse tables, which I'll show you. You can do whatever you want, but it's probably best to write it into the Pulse tables.

01:54

The other side of that is displaying the data, pulling it back out of the Pulse tables, and that stuff is just, is, is pretty vanilla LiveWire, honestly. Y- you have access to the p- the Pulse facade, and you can do a lot of l- nice, like, pulling the data out in, in such a way that it is ready to be graphed. But at the end of the day, it's just kind of a LiveWire dashboard.

02:15

I thought there was a lot more magic over here when really all of the magic is in the recording side and the data aggregation side. So looking at this config file, you see you can set it up on its own domain, on its own path. That's all pretty normal. That's a lot like, uh, Nova or maybe Telescope or Horizon, something like that. You can turn it on and off. The storage driver, right now, um, this is the only driver that is available to my knowledge.

02:37

There might be more drivers in the future by the time you're watching this. So the only place you can finally store all of this data is in a database, and you can configure which database you want to store it in, which is kinda cool. There is another thing called an ingest driver, and the ingest driver sits between your application and the storage driver. So that would sit right in the middle, and if you want, you could put Redis right there in the middle.

03:02

So the, the, the reason that the ingest driver exists is because you have your application, it's doing all of these events. Pulse is listening for those events, right? And writing to the database. If you find that that's not performant enough, you can shove it all into r- into Redis just, like, as fast as humanly possible, and then pull it out of Redis and write it to your database maybe at a little bit of slower rate or at a later time.

03:27

I haven't used this yet. I just write directly to the database. If you find that you're needing more performance, you can try that. All right, moving on down, we want recorders. Here are the recorders that ship out of the box. You can see these are all marked internal. That's why there's a line through it. The cache interactions, the exceptions, the queues, the servers.

03:48

We are gonna look at the servers. This is the one that shows you the CPU, the memory usage, all of that kind of stuff. So let's d- let's drill into this server's recorder. The first thing to notice here, which I think is interesting, is that this class is listening for an event called the shared beat. If we go up and we look at... let's look at the slow jobs. You'll see it's listening for a lot of job-related, uh, events, right?

04:11

These are events that Laravel is firing just throughout the course of the application life cycle, but this servers class is listening for one called shared beat. Looking in this shared beat class, you'll see it's just, it's, it's very simple. It's just a time and an interval, and it is a Pulse-specific event. This is the event that Pulse fires on a regular interval.

04:34

I think it's every five seconds, I think, and I'll show you, I'll show you how that works, but this is the event that Pulse fires on an interval. So if your recorder needs to wake up every five seconds or every minute or every day, once a day, you would listen to this event, and that's exactly what we're gonna do. Okay, continuing to look at this class, if we look down here, we have one method, and it's called record, and the record method receives one of the events that you're listening to.

05:01

In this case, we're only listening to one event, so we know that it's a shared beat event. And what the Pulse team is doing is they're saying, "If this is not, um, every fifteen seconds," so they're running a mod 15 on it and seeing if it returns zero. So they're, they're basically running this record event every fifteen seconds, and it's a pretty clever way to do it, and so otherwise they just bail out.

05:24

And then what they do down here is they just, you know, run a few commands. They see what's the memory total, what's the memory used, what's the CPU, and then they record a bunch of stuff. That's exactly what we're gonna do. We're gonna write a recorder that wakes up every hour, let's say, and writes some data to the Pulse tables. I could buy an argument that you don't need the recorder at all in this case, and you could just run it as a part of the LiveWire request response and cache it maybe.

05:51

Maybe. That's just less interesting, so let's create a recorder. Recorders are pretty simple. You just create... What do you wanna call it? You wanna call it Composer outdated recorder? Might as well be wordy about it. There we go. That's it. That's all you have to do. There's nothing even to extend here. So what we are going to do though is we're going to say, um- We want a public function record, and then we need to look at the servers.

06:16

This is how I... There's no documentation on this, so this is how I figure all this out. Let's look at the servers and just kinda yoink. That's mine now, and we'll come back over here, and we'll just put that up there. Yes, I want to import all of that, so I don't need these nice things, as lovely as they are. So we are listening to the shared beat event, and we do wanna accept Pulse so we have access to it.

06:41

This is the configuration from the pulse.php, which is really nice that they give that to you. And then we're gonna listen to the shared beat, and we'll call it event, and we can just do it the old-school way and just dump it out and see what happens. Now, we need to go register this, so we'll pop this open, go to config, Pulse, come over here, and this is where they're all registered.

07:05

And we're just gonna put ours at the top 'cause ours is the best, and we'll say outdated, composer outdatedRecorder class. And we don't have any configuration right now, but I think if you leave that out, it all dies very ungracefully. So we're just gonna make it an array. And now what? Right? We're listening for an event, but who's firing the event? Pulse is, but on a schedule, how does that work?

07:27

Well, we're all covered here. So if we run PHP Artisan, and we look for Pulse, you'll see we've got Pulse check, purge, restart, and work, and this is the one that we're looking for. This is the one that runs in a loop and fires out those shared beat events. So if we run AR Pulse check, and we wait for a second, you see there's our event.

07:51

There's our event that we dumped and died, and you see we have a carbon immutable time, and we have a carbon interval of five seconds. So it does run every five seconds. So this is the command. If you're running on Forge, there's one of those little application toggles where you can just turn on Pulse the same way you can turn on Octane and Horizon and one other one, the Laravel scheduler.

08:14

So now that we, now that we kinda know how these things are wired together, let's start writing out our actual recorder. I think this is the fun part. I like working on the backend of applications. This is, this is what I love the most, so let's have some fun here. So we'll say if event time does not equal event time start of day, let's do that, then we'll just return.

08:36

Since this is carbon immutable, we don't have to worry about mucking it up, um, and so what we wanna do is we only wanna run this at the beginning of the day because, I mean, our composer dependencies don't change that often. Right now I'm just gonna comment that out because that would make for a really boring video if we could only test it once a day, right? So what are we gonna do? Let's hop over to this command line here, and let's say composer outdated and see what we get.

09:02

The way that we're gonna get all this information is from Composer itself. If it would ever... There we go, if it would ever finish. So we have one outdated direct dependency, and that's the Laravel framework, and I did that on purpose. So this is a good start, but there's no way I'm parsing this. Fortunately, I don't have to. Let's go ahead and just say we only want direct dependencies.

09:24

Is it a capital D? There we go. So I only want direct dependencies. I don't want transitive or secondary dependencies. That's a better start. And actually, the format that I want is JSON. There we go. That's what I'm looking for. So I'm gonna take this back to PHP Storm, and we're gonna hop over here, and we're gonna say, uh, what do you wanna call it? Outdated equals process, Laravel's process, run this guy, and I think that is all we have to do.

09:54

So we'll DD outdated output and see what we get there. So if we clear this out and run Pulse check to start our loop, and we're gonna dump it out, there you go. There's our JSON. So that's pretty good. We'll say that outdated equals that, and then I'm just going to, uh, JSON decode it without actually setting the variable, and I'll show you why in a second.

10:19

So outdated, and then the flags are, I wanna say JSON throw on error. So all I'm doing here is I'm just making sure that it's valid JSON. So that should be good, and if we run Pulse check again, it's gonna have to run Composer outdated again, and there you go. So it got past this throw on error, and that's pretty good.

10:40

Now, what we need to do is we need to store this information in one of the Pulse tables. The Pulse tables, extremely interesting, extremely well done, no documentation yet. So I'm gonna tell you what I have seen so far. There are three Pulse tables. There's an entries, an aggregates, and a values. The values table is where you can just put stuff and get it back later.

11:02

It's just like put a key in and get the key back out later. The aggregates table is the one that keeps track of, okay, over the past sixty minutes, past six hours, seven days, twenty-four hours, whatever. That's the one that aggregates a ton of data and puts it into something that you can graph. I'll do a separate video on that. The final one is the entries table, and that's where you, um, you maybe wanna store the aggregate data, but you also need the discrete things that made up that aggregate data, like the exceptions or the users making requests, that, that

11:32

sort of thing. And so that's a huge long table full of information. The aggregates is kind of that information rolled up, and the values is just, you just put a value in and get a value back out. That's what we're gonna be using for Composer outdated, is the values table. We can validate some of what I just told you if we take a look at the actual Pulse tables and see what the server recorder is actually doing.

11:52

So if we look at the Pulse aggregates table, you'll see CPU, memory, CPU, memory, and then here's the period, one hour, six hours, twenty-four hours, seven days, and the different buckets correspond to different time periods. So it's keeping track of an average of the CPU, and for this period, it took two readings, and the average was, uh, twenty-five percent.

12:14

That's the aggregates table. So as more readings come in, these rows either get updated or created depending on the timing of the next recording. The values table, on the other hand, just has that one entry, and if you look here, it will... We'll see, it should change to eight five here in a second, or I guess ninety-five, 'cause it runs every fifteen seconds.

12:36

Boy, I hope that changes soon. Otherwise, I'm going to have to restart- This per... Phew, that was close. Um, so it, it, this one updates every 15 seconds, and it's just a point in time. And so we can see CPU 33, and if we hop over to the browser, that is where they're pulling this information from. This is the point in time reading, and then you can see that the, um, aggregates are starting to flow in on this chart over here.

13:03

Now, what we're gonna do is we're just gonna put data into that values table. We're just gonna put point-in-time data in, and we're gonna get it back out later. To do this, we're just gonna use the Pulse class that was injected into the constructor for us, and we'll say this pulse set, and then we have to pass through, we have to pass through a type, a key, and a value, and an optional timestamp.

13:23

Otherwise, it'll just be now. Now is fine for us. We're not doing time series data, and so it doesn't really matter. So if we pass through a type, we can say Composer outdated. We're just kinda making this up. Now, for the key, this is where, um, this is where the Pulse team was putting, you know, the name of my computer. We're just gonna put, uh, we can just put, uh, outdated.

13:46

We'll just put packages. Doesn't really matter 'cause we're not keeping track of different, um, different computers or different servers in that case. And the value has to be a string, and so we're just gonna put outdated. And that's why I didn't actually set this up here, 'cause then I would just have to re-encode it. All I wanna know is that it's valid JSON, so that's fine. So what I'm doing is I am setting this, um, with Composer outdated as the type, packages as the key, and the string.

14:12

So now if we hop over here and we stop this and run this again, it should run... This is gonna run every five seconds. It's gonna take a minute. And we should see that pop in. There we go. So now we have stored all of the data in the Pulse tables. So one of the great things about this is that we didn't have to write our own data store.

14:35

We don't have to write our own models. We don't have to write our own migrations. We don't have to write our own anything. We can just take advantage of these generic data stores that Pulse gives us, and that's great for values, right? So we're not worrying about, we're not worrying about cache and expiring, and we're not cr- we're not doing anything. This is great. It gets even better for aggregates, and I'll have to do an entire another video on this for the time series data because they keep track of the aggregates in a really compressed way.

15:02

But for now, the values are great. Let's take a look at the card and see how we can pull it back out. First thing that we're gonna do is we're gonna create a Livewire component. So we're gonna say Livewire make Composer outdated. Hang loose. We have created the component. So we're gonna go check this guy here, Composer outdated. So if we pop over to Composer outdated.

15:24

Looks like it hasn't loaded yet. So we're gonna go to app, Livewire, Composer outdated. The first thing we're gonna do is we're gonna change that from, from component to card, and we're going to extend the Livewire, the Pulse Livewire card. So if we go look at that guy, you'll see there's some rows, columns. There's some just, just a little bit of nice stuff going on there.

15:47

And so popping back over here, what we need to do is we need to find our data now, right? We put it in that Pulse table. So how do we actually get it out? Well, we're gonna use the Pulse facade, and we're gonna say values. And then we just have to remember, we just have to remember the type and the key that we used in our recorder.

16:07

So if we look at our recorder, we used Composer outdated, and we used packages. So the value is Composer outdated, and the keys we wanna pull out is just packages. So we can say that the values... Actually, let's just call it packages. We'll say that the packages is that. And this is gonna be a little bit wrong. I'll show you that in a second. But first we have to dump it out.

16:29

And now we need to add this to our dashboard, right? So we've got this Livewire component, but we don't have, uh, we haven't hooked it into the actual Pulse view yet. Remember at the beginning, we published both the configuration and the dashboard. So if we come out here and we look in resources, views, vendor, Pulse, dashboard, this is the entire view. So if we were to remove...

16:51

Let's just remove m- most of it and hop out to the browser and take a look. Great. It works. We, we are in full control of the Pulse dashboard, but we're not removing stuff. Let's add some stuff. Let's add, pr- specifically, let's add our card, Composer outdated. And because we extended that card, we can say columns equal two, and that should do something interesting.

17:13

But first we're gonna get a DD, and this is close. The data's a little bit wrong. It's pulling all of the keys that we gave it, of which there was just one named packages. So it, it's, it's kinda close. So let's go back and fix that. If we hop into Composer outdated and say, "I actually just want the only one that I care about." If you, if you were storing multiple keys, this would be more helpful.

17:37

Um, but for us, it's actually not that helpful. Great. This is what we're looking for. So let's say, uh, we can just tag that. Let's just tag that on the end, get packages, 'cause that's the only one that we care about. Now, what we need to do is that value may not be present. So we'll say packages equal packages, and then say, if it is present, then JSON decode the packages value, and we want associative to

18:07

be true and JSON throw on error. We're pretty confident that that JSON is gonna be good at this point 'cause we verified it going in. But I don't know. A little overkill never killed anybody. I don't know if that makes sense. So we'll keep moving here and say that this should be packages. I think we're gonna wanna pull the installed key off of there. That's the only thing we really care about.

18:29

So we can pull that off of there. And now we have our array of outdated- packages that we can pass to the front end. So we are good there. We can pull this out and return this and say packages, packages, and that should be it. I don't think it's gonna do anything because we haven't actually put anything in our view yet, so we'll do that next.

18:55

I'll show you the view for this card, and we'll walk through it together, but I'm not gonna do it all live. And honestly, I just kinda stole it from other first party Pulse cards. So I went source diving to figure out what are the components that are available to me, and then I just kinda plopped them into my own view, and that way everything matches. And it's probably pretty good, 'cause that's basically what they wrote. So everything is wrapped in a Pulse card, and that adds the borders and the padding and the background and all that kind of stuff.

19:21

And this is where the columns and rows come in from the, um, extending of the Pulse card. And then there are just a lot of great components that I kinda looked at and thought, "Yeah, I want that one." Card header, icon, icon. Um, it, it has this nice scroll thing so that it doesn't take over the whole page. It kinda shrinks it down and you can scroll through it. But really more importantly is, um, once we get down here, we're just gonna loop through all of the packages that we passed in from the component and then just kinda put them out onto the

19:51

screen. And there's a little bit better-- Th- there's some UI stuff that I could change, but if we just take this exactly as it is, it's, it's pretty basic, and we hop into the browser, this is what we have. We have our outdated Composer dependency card. And if you decide, "Oh, that's really too narrow, um, let's change that to six, and in fact we should drop that down f- underneath the server card," and we refresh that, hey, that looks, that looks pretty good.

20:20

Now, would it be nicer if, um, you know, would it be nicer if it showed colors for how far behind the dependencies are? Maybe. You have, you do have a link here, so you can go directly to that package. But more importantly, n- now we've written our own Pulse card. Now you know how to write Pulse cards. And I think this is interesting, this, this outdated Composer thing. It is interesting.

20:40

It'd be nice to add NPM dependencies as well. That would, that would be cool. It's available as, um, a open source and installable Composer package. So if you have ideas, I'll leave a link down below. But I think the more interesting thing is figuring out how to write your own recorder, how to write your own view, and then figuring out, "What does my business need? What does my application need?" And this is just storing pretty dumb data.

21:05

Once we start talking about time series and aggregation data, that's where it gets really, really interesting. And so I'll do another video on that in the very near future. But until then, please let me know what you think about Pulse. Let me know what you think about creating your own cards. Um, I am working with Matt Stauffer on builtforpulse.com, which is just like a community site to aggregate all of the cards that people are building.

21:29

So go to builtforpulse.com, um, and hopefully by the time you're watching this, you should be able to submit your own package. Um, let me know what you think. I think Pulse is amazing. See ya.

Laravel & PHP

More videos on Laravel & PHP

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues