Welcome back to Backspace Academy. In this lecture we're going to be creating 2
a nodejs application and we're going to use the X ray software development 3
kit to send performance data about that application over to AWS X ray and we're 4
going to do it all using elastic Beanstalk and take advantage of the 5
integration between elastic Beanstalk and a AWS x-ray. 6
In order for us to 7
use the AWS x-ray service to monitor our application we need some permissions set 8
up for our application that is using the AWS x-ray software development kit so 9
what it will need to have permission to send segment document information to the 10
daemon and also the x-ray daemon that is running will need to have permission 11
after it has collected that data to forward that on to the AWS x-ray service 12
now if we're using Elastic Beanstalk we can automatically in our configuration 13
file enable x-ray integration with elastic Beanstalk and what elastic 14
Beanstalk will do is it will not only run our application with its AWS x-ray 15
software development kit it will also run as a separate process on the same 16
ec2 instances it will run the AWS x-ray daemon and so that makes life a lot 17
easier for us and what will happen is that our our application will pass its 18
segment documents over to the x-ray daemon and it all be happening on the 19
same ec2 instances 20
now for that to happen we need some permissions to be set up 21
so first of all we need to have permission for the software development 22
kit that is running on our application to pass a segment documents over to the 23
daemon so that the daemon knows what information to collect and then the 24
daemon needs to have permission to forward what it has collected over to 25
the AWS x-ray 26
now fortunately for us the elastic 27
beanstalk already has those permissions set up but we can check that by going to 28
services and then into our IAM management console we go to roles and 29
we're going to search for an ec2 role 30
and there we have the elastic 31
Beanstalk ec2 roll that elastic Beanstalk uses for its ec2 32
instances that it launches 33
we'll click on that it has four policies attached to 34
it one of those policies is the elastic Beanstalk web tier so let's have a look 35
at that one 36
okay so in that policy we've got some permissions for Amazon s3 to 37
list and put objects in there we've also got x-ray access so what we've got there 38
are two permission so we've got put trace segments that will be used by the AWS 39
x-ray software development kit and it will be used to send segment documents 40
over to the x-ray daemon that will also be running on that same ec2 instance so 41
that ec2 instance will also need permission to put telemetry records for 42
the daemon to pass those telemetry records over to the x-ray server so 43
it'll pass that trace data that it is collected about our application over to 44
the x-ray service so that is setup fine but of course make sure that you go in 45
and make sure that it is already set up for you but it should by default be 46
there now before we actually start our elastic Beanstalk application we're 47
going to go to the repository or our starting branch of the repository for 48
our application code and that will be in the lab document so make sure that you 49
download those lab notes as always and get the link to that and let's start 50
having a look at this code so we've got our EB extensions folder and that will 51
contain all of the configuration information that we're going to use to 52
set up Elastic Beanstalk so let's have a look at that one 53
and we've got to there we've got an options config and an xray config now 54
all of these configurations could be in the one config file it doesn't have 55
to be in separate ones but it is a obviously a good practice to separate 56
them into the service and the actual elastic Beanstalk core options that you 57
are configuring 58
so we're just going to the options there for elastic Beanstalk the 59
first one there is we're creating an application environment so this is an 60
an environment variable that our NodeJS application can reference 61
and that is called AWS underscore region and it's going to reference the AWS 62
region that our ec2 instance is running on so that will appear to our 63
application as an environment variable called region and we've also got there 64
that we're going to be running nginx for nodejs 65
and we're also going to be putting all of our static files into a static folder 66
here as well so let's go back to the application and we can see there we've 67
got that static that static directory it's going to have our bootstrap and 68
jQuery libraries in there not going to be changing their static and it's going 69
to have our view which is going to have a pug document that's going to have the 70
details of our HTML web application 71
and finally there we've got our app,js 72
which is going to have our nodejs or javascript code 73
now the first thing we 74
see there is we've got X ray require the AWS x-ray software development kit now 75
that is different to the AWS software development kit also if we want to 76
capture any calls to the or through the AWS software development kit for example 77
if further on in this application we're going to meet making some calls to 78
Amazon s3 if we want to capture those as well we we need to require the AWS 79
software development kit inside of this capture AWS 80
and finally there we also 81
need to have capture HTTP and we're going to require HTTP and that 82
will monitor all of our HTTP requests to and from our application and further 83
down here we can see here that we've got AWS config dot region and as we said 84
before with we set that up in our configuration file and we can reference 85
it and that's going to get the current region for for this application that it 86
is running on and then we also need to set our sampling rules so there's going 87
to be a sampling rules.JSON file we'll have a look at that soon and 88
there's going to be here we've got our start x-ray segment so this is where we 89
define our segment that we're going to be collecting data with so we've got 90
here we're going to call our segment myfrontend and we're going to finish that 91
at the end of our application here we're going to close that segment there as 92
well so within here and here we're going to have a segment called my front end 93
and that will appear on the AWS x-ray when we look at the dashboard we've got 94
a sub sub segment here that we're going to be calling page render and so what we 95
do is we do x-ray capture async function and we call it page render and then 96
we're going to put that segments or that segment that we're capturing inside here 97
so there we can do we're just going to be rendering that application and giving 98
it a title and then we're just going to be closing that sub segment there's a 99
segment.close and that's what's going to happen so we've this will have 100
a very simple application it's just going to display a HTML web page and 101
we're going to have that as a segment and inside that segment we're just going 102
to have one single sub segment and just go back to the main root directory here 103
and as we said before we have sampling rules JSON so there we've got our 104
default sampling rules we're going to have a target of one sample per second 105
and the rate is going to be one so what that means is that when we've 106
when we've got over or any-any samples that go over one sample per second we're 107
going to get all of those additional samples as well so we're 108
going to get 100% of those 109
so if we had a target of one sample per 110
second and we owned it only once it gets a 10% of those additional all those 111
additional traces that were coming in all those additional requests we can 112
have the rate of 0.1 and it'lll only collect the extra 10% that for that period of 113
one second 114
so that's how we set up those sampling rules but there's a lot more to 115
sampling rules but goes beyond what you need to know for an associate level 116
certification 117
okay so that's enough on the application let's get in and run it 118
so what we do is we first need to download it now this zip file that I'm 119
downloading here we can't cannot use it directly with with the elastic Beanstalk 120
and don't try and do it because you'll get an error you'll get a 502 error when 121
you go to do it so what we need to do is that we just need to show in folder okay 122
so the reason we can't can't use this directly this zip file is if we double 123
click on have a look at it we can see that all of our code is inside this 124
folder so if we upload this directly unless your beam starts I'm not going to 125
be able to find that code is gonna be expecting it here not hidden inside this 126
folder so we need to get it outside of this folder so first of all it will 127
extract it 128
okay so that's extracted that what we do to do is just open this up and what 129
we're going to do now is just create another zip file in here so sent to 130
compressed folder okay that's good alright so now we can use that to upload 131
to elastic Beanstalk and we're ready to go okay so back into elastic Beanstalk 132
and management console and if we haven't got any applications up and running this 133
is what we're going to get just a welcome screen so we click on create new 134
application in the top right or right hand corner here and we'll give this a 135
name and create that 136
okay so let's create an environment first it's a web server environment and 137
we'll just leave that name and we'll leave it for auto generating a domain 138
for us that's fine and we select nodejs and we're going to upload our 139
code and we'll upload that now so app.zip and we upload that and upload 140
okay and create our environment 141
okay so our environment will start chugging away there and soon it will be 142
up and running okay so after quite some time we've got our environment up and 143
running let's click on the end point for that environment and we can see there 144
we've got our simple HTML page nothing too spectacular there so let's have a 145
look and see where the x-ray actually collected any of that data that we that 146
we requested so we're going to services we're going to x-ray just going to open 147
that into a new tab and it'll be computing that map it might take some 148
time depending on how much data you've got if you've got less than a minute it 149
won't come up we've got a minute or so you should come up with a bit of data 150
and then we can see we've got our our server set and node and it's myfrontend 151
so remember we created a segment in our application called my front end and 152
that's what's appeared here so we've we've got a problem here so we've got 153
most of it's okay but one of those requests came of an error and that's 154
going to be an orange so it's an client-side error so it must be 155
something like a 404 error we've made a request for something that doesn't exist 156
so we can click on this node here and we can see information about the response 157
distribution of the requests I've got some requests there and we can see that 158
they vary from up to one second down to you know 100 or 200 milliseconds here so 159
what we can do now is we can filter our traces based on their error so we're 160
just going to click on here on their arrow because we just want to see the 161
problem traces and we're going to click on View traces here and here we can see 162
our trace list so we click on the actual trace ID and we can see what the problem 163
is so it was my front end and it gave us a 404 error what we can do now is that 164
we can click on that again and that will give us more information about that 165
segment which was my front end and what caused it to give an error so if we look 166
down here for it further we've got the request URL and it was for our favicon.icon 167
now we didn't actually have one in our application so it made a request 168
to that and it didn't actually exist so that was where the problem was 169
and not a major deal we can just not worry about but later on we can if we're 170
going to create a real application we're going to put a favicon icon there for 171
our application we can do that so we just close that out now what we want to 172
do now is that we want to actually start looking at how we can trace cause to the 173
software development kits the actual AWS software development kit so what we're 174
going to do is that we're going to call the Amazon s3 service we're going to 175
list the buckets and we're going to see how long that's going to take so we need 176
to make some changes to our application code to make that happen so again we'll 177
just jump back into our repository and we'll now go to the master node which 178
will have the completed application then okay so let's have a look at some of the 179
changes that I've made to this so that we can actually monitor those calls to 180
AWS or using the AWS software developer kit 181
so again we've already set 182
this up previously but we have a require AWS software development kit inside of 183
the x-ray doc capture AWS and that will capture all calls that are going to the 184
software development kit or from the software development kit and what we do 185
now is we just scroll down here and in our capture async function we're going 186
to have our an s3 list buckets here and then at the same time we're also going 187
to have our rendering out there as well and then we're going to close off that 188
segment so what we'll do is we'll we'll see exactly the same as what we had 189
before we'll see that that that segment my front end will come up as a no but we 190
should have another node there as well for this call to Amazon s3 and it's just 191
going to output that when it renders this it's going to do it to a variable 192
there called bucket list and we're going to put that in to the view so I just go 193
back into the view here and index if we see down there 194
that we're just going to output that buck at least it's going to be a string 195
of all of the buckets that I've got on this account so again we go back up here 196
and we'll save that zip file or download that zip file and again we need to 197
extract it and get it all into the right format show in folder let's have a look 198
and I've just extracted that that that file that zip file just go into it and 199
again we need to select everything and put it into a higher-level 200
zip for them and we just open it up make sure that is fine there is not a problem 201
so I'll just go back in there we can now update our elastic beanstalk environment 202
to have our new code so go back into their elastic Beanstalk environment 203
click on upload and deploy choose our file 204
and we just give a version label say version 2 will be fine and deploy that 205
ok again so after some time we've got our new version 2 up and running we'll 206
click on that end point there so it's the same web page as before but we've 207
got this list of our buckets here that I've got on this account so everything 208
seems to be working so we should be able to see as a trace this call to Amazon 3 209
s3 to list those buckets so we go back into AWS x-ray we'll click on the 210
service map and we'll refresh it so we just click on the right hand side here 211
and refresh and map is updating ok so there we can see that we've got again as 212
before we've got our my front-end and we've also got this other service note 213
here for Amazon s3 so the my front-end will be exactly same as before but we've 214
we click on s3 we can see there that we've got a single call and it took 215
about 150 or so milliseconds there was no errors on that 216
so we've reviewed those traces and we click on the trace ID down here we can 217
see some more information about it so there we can see we've got our my 218
front-end segments inside our my front in my front end segment we have a page 219
render sub segment and that took up 1.2 seconds 220
and we've also got a call to Amazon s3 which was a list buckets call here and 221
we can see there that the big thing that took the time was a page render but the 222
cord Amazon s3 was quite quick no not a problem there and we can some some more 223
information about the s3 them and we can see there that he took a hundred 224
sixty-three milliseconds there he was two hundred he was a successful call if 225
we click on that we can get some more information about it and there we can 226
see no errors the resources and we can see there as it's a list buckets 227
in the region the request ID all fine there so that's pretty good you pretty 228
well have a good understanding now of how x-ray works how it can monitor calls 229
HTTP calls to your request and how it can or how you can set up segments of 230
your code and monitor those segments and sub segments if you want to look at the 231
performance of different parts of your application and you can also monitor 232
calls to AWS services as well so that brings us to an end what we need to do 233
now is clean it all up so we just closed x-ray there we just need to go into our 234
applications there in an elastic Beanstalk and click on the x-ray lab and 235
actions and delete that application don't delete the environment delete the 236
application and it would delete everything that is associated with that 237
application so just delete that and we should be up and running so just click 238
on that in from and again and we can see their elastic Beanstalk is terminating 239
your environment so I hope you enjoyed it hope you lot got a lot out of this 240
one it's a very good hands-on lab and I look forward to seeing you in the next 241
lecture