1 00:00:00,300 --> 00:00:01,286 ‫Okay. So now let's talk 2 00:00:01,286 --> 00:00:03,417 ‫about some advanced concepts for X-Ray, 3 00:00:03,417 --> 00:00:04,930 ‫and the first thing I want to show you 4 00:00:04,930 --> 00:00:07,210 ‫is how to instrument your code. 5 00:00:07,210 --> 00:00:09,310 ‫So this word may be new, it was new for me. 6 00:00:09,310 --> 00:00:12,220 ‫So instrumentation means the measure of a product's 7 00:00:12,220 --> 00:00:14,070 ‫performance, diagnose errors, 8 00:00:14,070 --> 00:00:15,960 ‫and to write trace information. 9 00:00:15,960 --> 00:00:18,100 ‫So it is a field in Software Engineering 10 00:00:18,100 --> 00:00:19,400 ‫to do all these things. 11 00:00:19,400 --> 00:00:21,030 ‫So now it makes a lot more sense. 12 00:00:21,030 --> 00:00:24,670 ‫When we want to instrument our our application with X-Ray, 13 00:00:24,670 --> 00:00:28,730 ‫we need to change our code and to use the X-Ray SDK. 14 00:00:28,730 --> 00:00:31,310 ‫So here is an example of how we can instrument 15 00:00:31,310 --> 00:00:34,430 ‫our node js code with the X-Ray SDK. 16 00:00:34,430 --> 00:00:37,730 ‫And so once we add some code, for example here, 17 00:00:37,730 --> 00:00:41,600 ‫requiring the X-Ray SDK and using it in our express app, 18 00:00:41,600 --> 00:00:43,510 ‫then our code will be instrumented. 19 00:00:43,510 --> 00:00:46,240 ‫That means that we will get trace information from our code 20 00:00:46,240 --> 00:00:48,110 ‫into the X-Ray service. 21 00:00:48,110 --> 00:00:51,290 ‫So the use of the X-Ray SDK is very minor. 22 00:00:51,290 --> 00:00:55,380 ‫Sometimes it only requires a configuration changes only 23 00:00:55,380 --> 00:00:57,630 ‫and you can modify as well your application code. 24 00:00:57,630 --> 00:00:59,510 ‫If you want to customize your traces 25 00:00:59,510 --> 00:01:03,260 ‫and annotate the data, or change the way X-Rays send it out 26 00:01:03,260 --> 00:01:05,210 ‫to the express service. 27 00:01:05,210 --> 00:01:08,196 ‫And so for this we can create interceptors, filters, 28 00:01:08,196 --> 00:01:10,580 ‫handlers and middleware. 29 00:01:10,580 --> 00:01:11,413 ‫This is pretty advanced, 30 00:01:11,413 --> 00:01:14,113 ‫but what I mean is that you can customize how X-Ray works 31 00:01:14,113 --> 00:01:15,770 ‫in your code. 32 00:01:15,770 --> 00:01:18,190 ‫So, now some advanced X-Ray concepts. 33 00:01:18,190 --> 00:01:20,270 ‫So the definition is that a segment 34 00:01:20,270 --> 00:01:22,240 ‫is how we can see things in the URL 35 00:01:22,240 --> 00:01:23,766 ‫so we've been looking at segments so far, 36 00:01:23,766 --> 00:01:27,350 ‫and so each application and service will send them. 37 00:01:27,350 --> 00:01:29,000 ‫But if you want to be more granular, 38 00:01:29,000 --> 00:01:30,840 ‫you can define subsegments. 39 00:01:30,840 --> 00:01:33,130 ‫This is when you leave more details in your segments. 40 00:01:33,130 --> 00:01:34,380 ‫Then the trace is when you get 41 00:01:34,380 --> 00:01:36,360 ‫all the segments collected together 42 00:01:36,360 --> 00:01:39,070 ‫and this will form an end-to-end view 43 00:01:39,070 --> 00:01:41,970 ‫of your API call, or your call. 44 00:01:41,970 --> 00:01:44,750 ‫And so this will be an end-to-end trace 45 00:01:44,750 --> 00:01:45,583 ‫sampling. 46 00:01:45,583 --> 00:01:46,416 ‫We'll see this in a second. 47 00:01:46,416 --> 00:01:48,870 ‫This is to decrease the amount of requests sent 48 00:01:48,870 --> 00:01:51,030 ‫to X-Ray, in order to reduce costs 49 00:01:51,030 --> 00:01:53,580 ‫because maybe we don't need all the requests. 50 00:01:53,580 --> 00:01:54,880 ‫Now, very important. 51 00:01:54,880 --> 00:01:59,290 ‫Annotations is when we add some key value pair data 52 00:01:59,290 --> 00:02:02,240 ‫to index our traces and use with filters. 53 00:02:02,240 --> 00:02:04,980 ‫So annotations is are extremely important in X-ray. 54 00:02:04,980 --> 00:02:07,780 ‫If you want to be able to search your traces 55 00:02:07,780 --> 00:02:10,970 ‫with new indexes versus metadata. 56 00:02:10,970 --> 00:02:13,380 ‫Metadata is key value pairs, as well. 57 00:02:13,380 --> 00:02:15,296 ‫But these this time they're not index. 58 00:02:15,296 --> 00:02:17,944 ‫Okay, so your annotations are indexed 59 00:02:17,944 --> 00:02:20,970 ‫and you can use them to search with filters, 60 00:02:20,970 --> 00:02:23,190 ‫whereas metadata are not indexed 61 00:02:23,190 --> 00:02:25,840 ‫and you cannot use them for searching. 62 00:02:25,840 --> 00:02:28,890 ‫Now the X-ray daemon agent also has a config 63 00:02:28,890 --> 00:02:30,750 ‫to send traces across accounts. 64 00:02:30,750 --> 00:02:32,500 ‫And for this we need to make sure the IAM permissions 65 00:02:32,500 --> 00:02:33,333 ‫are correct. 66 00:02:33,333 --> 00:02:35,700 ‫The agent will automatically assume the correct role 67 00:02:35,700 --> 00:02:38,096 ‫and this will allow us to have a central accounts 68 00:02:38,096 --> 00:02:41,291 ‫for all logging and application tracing. 69 00:02:41,291 --> 00:02:44,380 ‫Now let's talk about sampling in details. 70 00:02:44,380 --> 00:02:45,570 ‫So with the sampling rules, 71 00:02:45,570 --> 00:02:48,740 ‫we are able to control the amount of data that you send 72 00:02:48,740 --> 00:02:50,740 ‫to the X-Ray service and record. 73 00:02:50,740 --> 00:02:52,720 ‫And the more data you send you X-Ray, 74 00:02:52,720 --> 00:02:54,460 ‫the more you're going to pay. 75 00:02:54,460 --> 00:02:56,150 ‫So you can modify your sampling rules 76 00:02:56,150 --> 00:02:57,680 ‫without changing your code. 77 00:02:57,680 --> 00:02:59,810 ‫And by default there is a sampling rule, 78 00:02:59,810 --> 00:03:04,095 ‫which says that X-Ray SDK will record every first request, 79 00:03:04,095 --> 00:03:06,990 ‫each second, and then five percent 80 00:03:06,990 --> 00:03:09,020 ‫of any additional requests. 81 00:03:09,020 --> 00:03:11,740 ‫And so the blue part, the first request, 82 00:03:11,740 --> 00:03:13,620 ‫each second is called the reservoir, 83 00:03:13,620 --> 00:03:15,770 ‫which ensures that at least one trace 84 00:03:15,770 --> 00:03:18,090 ‫is recorded each second as long as a service 85 00:03:18,090 --> 00:03:19,360 ‫is serving requests. 86 00:03:19,360 --> 00:03:22,280 ‫And then the five percent is called the rate 87 00:03:22,280 --> 00:03:25,130 ‫at which additional requests beyond the reservoir size 88 00:03:25,130 --> 00:03:26,910 ‫are sampled. 89 00:03:26,910 --> 00:03:28,889 ‫So let's talk about the custom sampling rules. 90 00:03:28,889 --> 00:03:30,450 ‫You can create your own rules 91 00:03:30,450 --> 00:03:31,450 ‫and you can define what 92 00:03:31,450 --> 00:03:33,500 ‫is a reservoir and what is the rates. 93 00:03:33,500 --> 00:03:35,300 ‫So here is an example. 94 00:03:35,300 --> 00:03:38,740 ‫We have in this example a higher minimum rate for posts. 95 00:03:38,740 --> 00:03:41,150 ‫So we're saying the reservoir is 10. 96 00:03:41,150 --> 00:03:43,330 ‫That means that 10 requests per second 97 00:03:43,330 --> 00:03:46,508 ‫will be sent into X-Ray and then 10% 98 00:03:46,508 --> 00:03:48,270 ‫of the other ones will be sent. 99 00:03:48,270 --> 00:03:49,940 ‫So here we have a higher minimum rate 100 00:03:49,940 --> 00:03:53,240 ‫and we send more requests into X-Ray. 101 00:03:53,240 --> 00:03:55,160 ‫Whereas here we want you to have debugging. 102 00:03:55,160 --> 00:03:58,000 ‫And so we say we want to have all requests. 103 00:03:58,000 --> 00:04:00,210 ‫So one a reservoir and one of rate. 104 00:04:00,210 --> 00:04:03,220 ‫That means that all requests will be sent into X-Ray. 105 00:04:03,220 --> 00:04:05,630 ‫So we don't want to lose any traces. 106 00:04:05,630 --> 00:04:07,530 ‫And this is very helpful when we want you to debug 107 00:04:07,530 --> 00:04:09,850 ‫to find what's going on for every single trace. 108 00:04:09,850 --> 00:04:12,610 ‫Obviously, in production, this will be very, very expensive 109 00:04:12,610 --> 00:04:14,695 ‫because now we are sending a lot of data into X-Ray, 110 00:04:14,695 --> 00:04:17,080 ‫but it's very helpful temporarily 111 00:04:17,080 --> 00:04:18,930 ‫to change these custom sampling rules 112 00:04:18,930 --> 00:04:20,060 ‫to see what's going on. 113 00:04:20,060 --> 00:04:21,530 ‫And the cool thing about it is that 114 00:04:21,530 --> 00:04:24,920 ‫if you change your sampling rules in the X-Ray console, 115 00:04:24,920 --> 00:04:27,020 ‫you don't have to restart your applications. 116 00:04:27,020 --> 00:04:30,757 ‫You don't have to do anything with your X-Ray in SDK. 117 00:04:30,757 --> 00:04:33,690 ‫Automatically the daemon, the extra daemon knows how 118 00:04:33,690 --> 00:04:35,640 ‫to get visa something rules 119 00:04:35,640 --> 00:04:37,300 ‫and correctly send the right amount 120 00:04:37,300 --> 00:04:39,910 ‫of data into the X-Ray service. 121 00:04:39,910 --> 00:04:41,660 ‫So let's go into the next lecture 122 00:04:41,660 --> 00:04:43,860 ‫to see how we can define the sampling rules.