Playing
01. Profiling JSON responses in Laravel

Episodes

0%
Your progress
  • Total: 9m
  • Played: 0m
  • Remaining: 9m
Join or sign in to track your progress

Transcript

00:00
You might already be using Laravel DebugBar to check things like how many queries you're running per page, check on things like your session data, request data, how many views you're rendering,
00:10
and other bits and bobs as well, including your timeline, so how fast your application is actually running in terms of the page that you're currently on. But what happens when it comes to an API endpoint? Well, we can't really use DebugBar to append on HTML in this case.
00:26
It just wouldn't work because, of course, it would break the JSON structure that you're trying to render out. So what we're going to do is in this snippet, just take a look at how we can append onto our API endpoints when we choose the profile information about this, just so we can keep
00:42
an eye on the amount of queries we're running, the detail of the queries we're running, and all of that kind of stuff. So the way that this works is we're going to be building out some middleware, and we have a choice.
00:52
You can disable this, but I prefer to have an endpoint that I can just see as normal. And then when I want to go ahead and profile it, I just append on profile equals one, profile equals true, whatever you want to do. And what that's going to do is at the end, give us a huge amount of information here,
01:08
which we can limit. So I'll show you how to do that in a minute. Of all of the timings of this, we have memory usage, the root information, query information, which is super important, because obviously we want to keep an eye on how many queries
01:22
we're running, and all that kind of stuff. So it's really, really useful. Now, if you did want to go ahead and limit this, if we just come over to HTTP and middleware, this is the profiling information.
01:35
Inside of this profiling data property, we can just choose to maybe only show queries. You might just not be interested in seeing anything else. So that's what we're going to be doing in this snippet. So let's go ahead and start fresh and get this middleware built out.
01:49
All right. So of course, I'm starting out with a fresh Laravel project here. The only thing I've done is just switched over my database credentials purely for the fact that we need to test this out.
01:57
So I just have a user controller here, which is grabbing all the users ordered by latest, and we're just responding with some JSON. So what we're going to do is for those of you that are new to Laravel Debug Bar, we're going to go ahead and install this.
02:10
We've pretty much already seen how this works. So you should get a general idea of how this works. But let's go ahead and pull this in, register the service provider, and then we'll look at pulling this into our API responses.
02:21
Okay. So just while that's finishing off, let's come over to the service provider here. Let's grab this and come over to config and app. And let's come down and register this.
02:33
And what's going to happen is this is going to automatically be enabled because if we come over to EMV, we're on our local environment. As soon as we switch over to production, this is no longer going to be enabled. So I guess the first thing to mention is when you push this to production, make sure
02:50
that you have this as production when you deploy in your environment variables, or everyone's going to see your profiling information, including queries, your database structure. So it's not good.
03:01
So we have Laravel Debug Bar installed. As I mentioned, this will automatically be enabled. We just see it at the bottom of the page. But we're interested in this API endpoint just here.
03:13
So how do we do this? Well, the first thing to do is go ahead and create some middleware. So let's generate some middleware here. And we're going to call this profile JSON response.
03:24
So let's go over to our middleware section just here. And let's open this up and see what we can do. So the first thing that we need to think about is, well, what happens when there is no debug bar installed, first of all, or if debug bar is not enabled?
03:41
Well, let's go ahead and assign this to response just here. And we'll do a quick check to see if debug bar is bound to our container or not. If it's not, then there's no point even continuing. So the first check is if we can use our app helper in here.
03:56
And we use bound to check if it's bound. And we just use debug bar like so. Now, we also have the ability to, as we know, disable debug bar. So we can do that using our environment variable.
04:07
We can do that manually. But most of the time, you'll use your environment variable. So in this case, what we want to do is access debug bar. And then we want to use is enabled.
04:17
And if you need to find anything else about this, you just head over to the debug bar class. And I think this is called Laravel debug bar just there. And you'll see methods like is enabled.
04:28
So you can go ahead and take a good look at that if you need to do anything else. So in this case, we just want to return the response like so. What we also want to do, of course, with middleware is go ahead and return that response at the bottom anyway.
04:41
But in between this, what we want to do is check if we are looking at a JSON response. Then we want to check if we have that little profile thing in the query string just there. If we do, then we want to go ahead and not overwrite but append onto our JSON response. So the first thing to do is check if response is an instance of JSON response.
05:04
And let's just go ahead and pull the namespace in for that. Now JSON response is what we saw earlier. So when we go ahead and respond like this, the response will be an instance of JSON response. So we know that we're responding with JSON.
05:15
There are other ways to check this. But this is pretty reliable. And the other thing we want to do is check if the request has that profile signifier in there.
05:24
And of course, you can omit that if you want to. So let's go ahead and just die dump and just do anything. Let's register this middleware really importantly. So let's come over to kernel.
05:33
And we're going to grab this trim string one. And we're just going to split this over to profile JSON response. Let's come over and give that a refresh. And it looks like we missed a comma just there.
05:44
There we go. So let's imagine that we were on production. And earlier I said this determines the status of debug bar. Whether it's enabled or not, that isn't the case.
05:53
It's whether app debug is set to true or false. So if I turn that off, we see everything in here even with profile set to one. And if we, of course, set that to true, it means that we are debugging. And Laravel debug bar will be in there.
06:07
So yeah, it's not based on the environment. It's based on the debug config just there. OK, so now what we want to do is using our response, use the set data method to basically merge in the current data that we have and the debug bar data.
06:25
So there's a couple of ways that we can do this. But the easiest way is just to do an array merge. Go ahead and grab the data. So we're going to say response get data, like so.
06:35
And then we're going to, as the second argument, provide in the debug bar information. So in this case, we can just say debug bar. And we can go ahead. And I like to extract this out to a separate method because there's a couple of checks
06:50
that we're going to add in here. So let's extract this out to a method first of all. So let's say get profiling data, like so. And let's implement this method just down here.
07:00
So get profiling data. We want this to be as flexible as possible so we can really quickly change it around. So let's just kick things off by returning all of the debug bar data that we get. So we're going to use debug bar again from app.
07:14
So we pull that out. And we can use get data now. Now if we go ahead and return this, place this into here, merge this in with the actual request or response that we should normally see, then of course what we're going to end
07:26
up seeing is all of debug bar's data. So there's very little work we need to do here to actually get this working. So now what we want to do is potentially limit this using some kind of property. I found that when I was using this, there was just too much information here.
07:40
And I had to scroll down and have a look. And really, most of the time, all I really need is queries. So we are going to just at the top here create a property which can control this. So we're going to say something like profiling data.
07:56
And in here, let's just say we wanted to see queries. That's usually the most important thing just so we don't run into any N plus 1 problems or running too many queries. So in this case, this is really, really straightforward.
08:08
We just want to say array only. And that's a Laravel helper. And we just want to say this profiling data. And we should be good to go.
08:18
So let's just add a quick dot block to here. So get profiling data. And that is going to return an array. Perfect.
08:27
So let's come over, give that a refresh. And we should now just see our queries there. Now, the other thing I then went to do was say, well, actually, I need to see some other information.
08:35
But I can't remember what the property names are. So in this case, what I did is using an empty array, what we can do in here is just do a little check to say, well, if the profiling data is empty, so let's say this profiling data, then we just want to return app debug bar.
08:52
And we want to go ahead and grab all of the data just so we can very quickly disable the filtering. So now we have all of our data in there. But of course, we can come down here now, pick out what we want to see.
09:04
So in this case, maybe we want to see queries. And maybe you want to see the time information as well. Well, we can go ahead and see that now. Perfect.
09:15
So there we go. We have a little toggle here, which is really nice. We can just see our normal endpoint here. We can see our endpoint with profiling information.
09:24
And as I mentioned before, when you switch over to production and you set app debug to false, this, of course, isn't going to be enabled, which is very important. So there we go. That is how we add profiling data to our API endpoints in Laravel.
1 episode 9 mins

Overview

You might have used Laravel DebugBar to profile your query count and application speed, but how do we profile JSON responses? Let's find out!

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Episode discussion

No comments, yet. Be the first!