Intro to Health Checks in .NET Core

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
how do you know if your web application is healthy sure you can check to be sure your site is running but is that enough for instance maybe your database is responding slowly or your logging has crashed or maybe your server is about to run out of hard drive space how would you know that well the answer it turns out is with health checks health checks are built into net core web applications you can use them to verify and report on the health and stability of your application they can even be used to tell your load balancer to restart your web server in this video we're looking at ways to easily implement health checks in our asp.net core projects now if this is the first video you've watched of mine my name is tim corey and it's my goal to make learning c-sharp easier one of the ways i do that is by teaching context there are a lot of tutorials out there that will teach you what to do i go a few steps beyond that to show you when do it why you should or should not do it what pitfalls to avoid and what the best practices are basically i get you ready for the real world if that's the type of training you're interested in subscribe to my channel and hit the little bell icon to be notified when i release new videos finally in the description there are links to get the source code for this video to sign up for my mailing list to join patreon and to get access to my courses if you're looking to make a career out of c sharp or want a better in your c-sharp career my content can launch you forward faster check out all i have to offer i am timcory.com or sign up for my mailing list and hear from me directly all right let's go over to visual studio and get started we'll scroll down and say new project and we could choose pretty much any type of web project any of the five types for net core so there's three types in asp.net core web application that would be your razer pages mvc and api and there's two more under blazer app blazer server and blazer web assembly let's start by choosing blazer server because typically if i don't know what type of project to create for web i just don't need a web project i'm going to default to blazer server that i think is a sweet spot for most projects so let's call this blazer demo and we'll call this health checks demo for a solution name now i said blazer server we're also going to show this in an mvc probably we could do it razor page we could do an api i just want to show off the fact that it's not specific to a a specific web project type like blazer server and we also want to have more than one project anyways so we'll do a different type now we'll leave it blazer server no authentication yes https and that's it here we go now we do need a couple of nougat packages but before we do that i want to talk about the health of our application now if we just start this application let's just run this just to make sure it works and once it starts up we have to ask a question is this a fully working application is it is it healthy and that really depends on your definition of healthy you see we can go to the pages we can click around we can click the button see that it works we can fetch data and see that that works so we can say yeah it's healthy but is it though for example what if our customer reports that the site is slow well slow compared to what how do we know because if we go in later and we go click click click seems to work just fine maybe we're not seeing the same thing they are maybe when they fetch data it's slow but we're not testing that part and then how do we determine slow is slow visible slow like one one thousand two one thousand oh the data popped in that's slow but what if it is 200 milliseconds versus 500 milliseconds what if some of the underlying data this is pulling from isn't available those are the kind of things that are a lot more gray area than just is the site up we can use a service like um i'm not sure which service i haven't used one recently but a pinging service that kind of pings your site and says is it up well that's a type of health check where it just says is it running but there's a lot more to it than just is it running because maybe the site is running but the database isn't or like i said maybe we have logging going on behind the scenes to make sure we log in exceptions but the log system is down and if the log system is down it probably won't crash our application it shouldn't so therefore we're running without catching any of our exceptions that wouldn't be good but we can't see that by just going to the site that's where health checks comes in and that's the kind of things we're going to look at in today's video now let's start by adding the nuget packages we need let's start with asp.net core dot health checks dot ui all right there's the ui that we need and we also need the client so once the ui installs we'll grab the client as well now if you're looking for downloading nuget packages what happened with net core is microsoft said instead of having this really really big framework that's got everything inside of it let's make it really small and then you can just add the pieces back that you need so don't be afraid to add in nougat packages especially since they are you know a lot of them are supported by large companies and or microsoft themselves so just don't feel like adding nuget packages is bloating your application it's just the opposite we had blade applications with net framework and we still needed nuget packages instead they slimmed it down and said you bring in the things that you need instead of having them include automatically okay let's go to our startup.cs and inside of configure services right in here we're going to configure our health checks let's do this we're going to do it right here after the add server side blazer the spacing here doesn't really matter but we're going to put in here and say configure health checks just to demark the section so we're going to say services dot add health checks now we're going to add a couple of checks in line right now just to demonstrate how health checks work so let's start with an ad check we're going to add it's a couple of hard-coded checks to show you how they work how it it uh interacts with the system how we can view it and so on so add check where i say this is a healthy check meaning it's going to pass healthy checking have a healthy check um and then in here we're going to say our arrow we'll do this the next line down and we'll say health check result dot let's say healthy and in here we'll say the check of the foo service worked something like that okay so we have one check now let's actually indent that so you have one check so we said add health check that's the overall saying add health checks to our application and then we have one check that says this the name of it is just healthy check that's a generic name that we gave it to say um you know that this is gonna work let's actually change that name to um uh foo service okay so we're checking the food service which we don't have a food service as it's obviously just a demo i'm going to get into more realistic scenarios in just a minute but i want to start simple so foo service and this is an anonymous method that's going to return just healthy and the response message is the check of food service worked that's all it's going to do now that's it for health checks for right now let's come down here to end points so use endpoints and in here we're going to say endpoints dot map health checks and we'll say slash health which is a pretty standard name so it's going to say map health checks to slash health meaning we go to our url health we should get the health check telling us if we're healthy or not let's run this okay so i just add one test health check just to see health checks and action so slash health and it says hey you're healthy how does it know that well it knows it because this check right here is healthy what if we change it to a different state how about unhealthy let's change this to did not work not that we're showing this yet but the food service did not work it's unhealthy let's run this slash health and says hey you're unhealthy okay so it's kind of corresponding one to one right well there's a third state i just want to show you a state first and we'll talk about adding more than one check the third state is degraded the china food service did not work well so a degree of state is actually the i think one of the most fascinating or best states that they could have added instead of saying offer on yes or no worked or didn't work there's a a middle ground here saying hey there's a problem but it's still working for example maybe you're doing a database check here and you're saying hey can i access the sql database and it says yes you can but it took me three seconds to get data back that's not not working but it's slow which is a degraded state meaning there's a problem it's just not stopping work it's it's causing a delay in work and maybe three seconds is too long maybe you should still say it's unhealthy but maybe if you expect a 50-second mill or 50 millisecond response time and you're getting a 250 millisecond response time it's still acceptable but it's not optimal so you could say degraded meaning the problem is there's some kind of bottleneck or slowdown but the application is still working in that case between 50 milliseconds and 250 milliseconds you wouldn't necessarily notice that just by going to the page because it's still it's still a quarter of a second so if your app is otherwise responsive you won't see that but yet you can see in the background oh actually we're in a degraded state because we expect to be at this speed when we're actually at this lower speed that could indicate an issue so let's just see degraded state again really cool state definitely want to see us in use so slash health it says hey you're in a degraded state okay so so far that's just corresponded to whatever we said here with one service so let's take this semicolon off at the end i'm gonna copy this whole line these couple lines and paste in uh two more checks so now we have the foo service which did not work so well i have the bar service which that worked so let's do ctrl j and say that was healthy and this last one was um the database and that also is healthy so you have one degraded and one two healthy what do you think the response is going to be for our health say slash health we get degraded well why well because it's going to take the lowest value so if all three days are healthy cool that's a healthy cert it's a healthy system everything's working but if one piece of your application is degraded your application is degraded but maybe there's important pieces that are degraded or made as unimportant pieces that are degraded for example if we are um doing some work in the background but maybe we want it to you know work whenever just just get this work done but it's whenever and that's a little slower than normal we don't really necessarily care especially if you have high load on a server or whatever else so that degradation is is not a big deal whereas not be able to talk to a database fast enough can be a big deal so how do we split out these values and see these messages well that's where we change this down here to give us a little bit more information so we can say comma new health health check options notice it wants to extend this whole thing out here i don't want that so i'm going to come down here this next one which is going to add a using statement for me so notice the very top it's add a using statement right up here for that health checks notice i actually have two using statements now visual studio is getting very good about adding using statements for you automatically which is great when you're typing things out not as great when you're doing videos about the code we often miss those things so just note let's go back up there one more time make a note of these two using statements in case you have to type them by hand typically if you are typing something out and it's not in the using statement but get the reds quickly do control dot and we'll bring up an option and one of those options will be to add missing using statement okay so new health check options open close parenthesis or paren open and close curly brace like so and let's put those on new lines like so we're going to say response writer equals ui response writer this is going to add a third using statement so notice up here now we have using healthchecks.ui.client that's one of the nuget packages we added so responsewriter dot write health check ui response what this is going to do is format a a json response for our our health check so let's run this again to see this new type of response so slash health and now you notice we have status of degree this is a json response but then we have a total duration that's how long our health checks took and this is an important part we're going to talk about again but i want to point this out you don't want your health checks to take so long to run that they degrade your application so make sure your app your health checks are quick so that's just a note there but then entries so here are the entries we have in our health checks the food service well no data associated the description says the check of the food service did not work well the duration was very very short and stats was degraded no tags we'll talk about tags in a minute the bar service that worked and the status is healthy the database the status is healthy so that's those three pieces of information so instead of just saying degraded which doesn't give us a lot of information now we get this whole object of information this whole json object which we could bring into an application and and use in some way which we're going to do in just a little bit so let's close this out and i do want to show you how you can have more than one of these health checks because maybe you don't want to have all that data so you say endpoints dot map health checks and say you know what i just want a quick health just give me overall quick health of my applications you can say new options like so and in here you can say predicate equals oops underscore arrow function or arrow false so what this does let's press my colon okay so what this does is says the predicate is false now what this this underscore here is this is a variable we don't care about this is one of those features i believe it was c sharp eight that introduced these whenever you have a variable that you don't care about but you want a different variable or you don't want to even worry about it you just do an underscore so this is actually a calling a method and this method's gonna return back false just false so what this will do is it will say hey you know what if the predicate is false then it's not going to regist run all the checks it's just going to run just the um the overall check that's it there's gonna be just the overall check of our application so let's run this again we'll do health and quick health so slash health that's the overall degrade but now if we say quick health it says healthy cool so our application is healthy which it's not because if we went back to the quick health it says degraded so what is health really about well actually you can use this page to ping to see if our checks are available it's not running any of our checks it's just saying hey can you get to this spot it's essentially what you could put here is you could use this page to ping so an external system could look at this page which is technically a health check but what it's going to do is paint us and say yep your system is running that's all this does it says yes the system is running it's not saying that it ran any of the checks and these checks here didn't run any of them it just because we already said false and that's predicate that predicate false says don't run any of the checks so therefore the response is healthy now i mentioned tags let's talk about tags well before we do i do want to talk about this syntax right here because it may be a little confusing because this is really shorthand so this is saying is create an anonymous method and call it and get back a value but it's not it's everything shortcode so let's break this out and show you what it would be if we kind of made this into a full method it's still anonymous so let's cut this out and inside this curly braces let's like so let me say return and put a semicolon at the end that's the equivalent okay so we have to create braces we're saying return and this is the value we're returning so we could have more lines of code up here you know that actually do the real checks and then after that is when you actually return a value where you say it's degrade or it's um it's healthy or it's unhealthy so we're just shortcutting all of that and just returning back a hard code result it's like returning true or returning false or returning one or a string of some kind we're just getting back the return type it's expecting with one of the three states as expected so this right here is the equivalent of this right here this is just this right here is just a shortcut to that okay i just want to make sure that that was um you know pointed out i didn't want to get lost in that it this is what you're gonna need to do right here if you want more than one line in your code well now that we have this i do want before the close parenthesis or paren before this close paren i do want to put a couple of extra values in fact one extra value so let's say new array inside here we're going to say this is a service and i think that's it it's a service that's the one tag we're going to give it so this right here indicates our tags now you can have multiple tags if you want let's copy this and after the first paren we're going to put our tags here but this one this is a service as well the bar service but the database that's not a service that's a database and maybe it's also sql so now i have tagged these and we can actually filter based upon these tags so if we come down here let's create a new health check that just checks services let's start with right about right after quick health we're going to say endpoints dot map health checks we'll call this uh let's call this health slash services and we'll say new health option health check options inside of here let's put our semicolon at the end inside of here we're going to say predicate or the predicate but instead of just saying underscore we actually want to have a variable name here and then we have our our arrow and we're going to say reg dot dot contains we call it service or services wouldn't matter because if we said service it would get services but if the tags contain service all right actually it wouldn't matter sorry so if we find a tag in the list of tags that says service we want to include that in our value we also want to say that the response writer equals ui response writer dot write check ui response that gives us the full json uh path or the full json amount or text let's run this so it's health services so first slash health there we go we're degraded services but notice that we have only two here if you can tell this we still degraded we have a food service and we have the bar service no database now what if we change let's not change any text let's just change the this is healthy so let's change it to healthy and we'll change the database to grade it okay i'm not going to change any text so it's gonna the text me off but that's okay i just want to demonstrate what happened so we go to slash health it says you're degraded now the food service is healthy the bar service is healthy but the database is degraded well if we go to slash services it says we're healthy why is that well because the food service is healthy and the bar service is also healthy and notice it picked up because they were tagged as service so therefore it's picking up these two just these two and saying well the the health of this group is healthy but the health overall is degraded because that database isn't working as efficiently as you want to so that's how we can and let's undo those those two so we're back in order and we're calling this out in just a minute because i want to show next how we can actually create a service that looks at this and puts it into a better format because json quite frankly is not the best format to be reviewing your health checks in so let's create a new project in our solution so right click on your solution we're going to add a new project and the type we're going to create an asp.net core web application let's call this health checker and let's create a web application type which is a razer pages project now it doesn't matter which prototype i create but it seems the simplest to create this so now let's go to health checker and we're going to right click on dependencies and manage nuget packages now i could find those two packages but i want to show you a shortcut here and close it back out single click on blazer demo or right click and go properties notice this i am group here with these two references copy the whole thing these are our nuget packages single click on health checker paste in that item group now when we save this if we close it out we go right click on dependencies manage nuget packages go installed we have those two project or packages installed really cool stuff you can manually type out the packages you want but i still find that easier to find in nougat when i'm looking for a new package but if i'm going from project to project and they both need a certain package that's in both projects i just copy and paste that into that one line so you right click and go to properties oops right click and say open oh there we go edit project file sorry edit project file there we go you can copy just this one line if you want just this one package just make sure it's inside of an item group when you move over to the other project we have one more to add asp.net checks dot core.healthchecks.edu dot in memory dot storage there we go so we're gonna use in memory storage for this we're not gonna store it in sql or somewhere else for the long-term health check information this is kind of going beyond what we need to do in the scope of this demo i didn't want to go into storing this anywhere else but in-memory works just fine and what that will allow us to do and you can probably even use this in production it's up to you how much you want to go back in history and how much you want to use your memory up to store health check information but what this will allow us to do is use this project let's close this out use this health checker project to monitor other projects other web applications that have health checks and that may sound a little weird but it's okay i'm going to show you how it works and this is really helpful if you are going to be working with multiple projects inside of your overall structure now when would you ever use multiple projects well if you're a business you'll probably have multiple sites multiple web applications or you might have microservices you see you can do this beyond just web applications we're talking about web applications today but you could go and you implement these health checks in other net core projects including micro services well if you have micro services you probably have 10 20 30 50 or 100 plus services trying to monitor all those separately and individually would be crazy instead you want to centralize that into one location we're going to see that with our health checker project let's go to startup for health checker and inside of our configure services we're going to say services dot add health check ui dot add in memory storage now that just added a couple of i believe it added a couple using statements it appears not to have okay well look at that didn't even didn't even need using statements but we're going to add this in memory storage for our health checker ui or health checks ui then we'll come down to end points and we're going to say endpoints dot map health checks ui you know what that's it now in order to run both of these applications i'm going to need to right click on solution and say set startup projects i'm going to choose multiple let's say the blazer demo is starts and the blazer or health checker is start without debugging i'll hit okay oh i have one more thing i need to do i almost forgot and that is in health checker go to app settings.json and in here we're going to add after this comma here we're going to add a new section called health checks ui and make sure you put your comma after the the end of the curry brace so in here we're going to say health checks we're going to specify an array of objects that's why you have a square brackets and then we have curly brace for an object or say name and for this one we're going to say well it's our blazer server demo that's that's this blazer let's just call it blaze demo since we call it that for the project blazer demo and the we have the uri which is the place we locate it at where the um project will start well where how we know what that is well under properties launch settings we see it's localhost and the port for ssl is 44380. so i'm going to copy that and we're going to say https localhost colon and the port number and then slash health now in a real application when you are really doing this for real you would have the url for your application in production but you wouldn't have that for testing you would change this value throughout your build process so for example when you're building and pushing to development this would be the development url for your your site your development site and then for you know staging would be the staging url for your site and then this would be production when you push to production locally we're hosting it on localhost 44380. therefore that's the url including the slash health that's the the health check we created so notice in startup on our blazer app or blazer demo we have slash health which is that right health check ui response you gotta have that but it doesn't have any filtering for these tags it doesn't turn off the predicate it just says give me everything and by the way write that json that's friendly for the health check ui so this is a health check ui response which will allow us to consume it in our health checks ui client so with that let's we've got the health checks in place we've got this array of health checks which we have just one right now but there's a couple more says to set up evaluation time on seconds is ten let's make it five so five seconds and every five seconds gonna evaluate and then minimum seconds between failure notifications and we're going to say 60 but that's a default you can actually change that to be whatever you want for how many times between seeing a failure do you keep reporting that failure so if a database goes down how often do you want that report do you want to report it every single time well probably not you probably want to give it some time before it reports that failure again so if it it fails and then it comes back up but it fails again within a one minute period it won't report both failures just to one so that's up to you how you want to see that okay now we have both of these starting up let's run and see if these will both launch they should so there's our health checker this is our our razor pages application this is our blazer page we go here and say slash health we see it's degraded and why but if you go to health checker go slash health checks dash ui we get this and this is really cool this is a user interface for our whoops i now say the pulling interval 10 seconds i think that's because um in not on let's try this again so health checks dash ui there we go five seconds so this is the default user interface that it gives us you can actually you know make changes modifications to it but this is really cool stuff because with just adding those two lines let's let's see what we have we had this this section in our our app.config and then our startup we said map health checks ui and add health checks ui and add in memory storage those are the two entries besides those three nuget packages and the app settings.json the result is this even cooler hit the plus and we see all of our services how long they took to run the description of the message we get back the status of them and the tags we can even go in here and look at the details to see oh it's degraded which doesn't tell us a whole lot right now but we're going to see how those details update as our services become healthy or unhealthy this is really cool stuff it's really simple to do notice it says blazer demo that's because this is evaluating that blazer demo project but we don't have to have just one of these we can have a whole bunch of them we can see the overall health we can expand out and see all these services and databases and everything else all the checks or we can look at it on a per server per project basis per web application or per application in general project basis let's see how we can implement this first let's minimize health checker and what i'm going to do let's close out everything and i'm going to right click on the solution i'm going to add a new project remember i said i'm going to add an mvc project so let's go to asp.net core web application where i say this is the mvc demo and scroll down here scroll's too fast web application model view controller that's an mvc project no authentication yes to https let's run this and now we can add let's go to blade to demo and grab those two packages the mvc demo paste them in you could manually go to nuget and add this and then this but i find that a whole lot easier to add those nuget packages that way then let's go to startups.cs for a blazer demo and let's just copy the health checks we have so far what's going on a quick copy paste because i don't want to go over this all over again for the same exact things so let's just copy and paste this they'll be the same names ctrl dot to add the using statement there we go and then we'll also come down here we'll just add the the one endpoint the slash health now it's going to yell at us about this ctrl dot to add the using statement and control dot add the using statement so now we have two services that both have health checks now they're identical but they're both in place in fact i'm not going to worry about changing anything we're going to leave it as is so we have two demos of this let's go to our properties in nbc demo and go to launch settings notice it's a different ssl port let's grab that let's go back to health checker go to app settings and what we're going to do is we're going to add let's just paste it up here i want to copy this whole section i'll put a comma paste it in there this is mvc demo and we're going to grab this port number right here paste it there we go so now we're doing two checks we're gonna check two different systems the blazer demo and the mvc demo and again right now they're mirrors i'm going to change that in a minute but i want to show this does work right click on solution say set startup projects notice the mvc says none want to change it to start without debugging let's run all three of these so we have health check at the at the end we have mvc and we have blazer well that didn't load well seems to have crashed let's start again there's blazer but that's it let's close this out and start all over again see if we can't get a better result there's blazer and starting behind the other two that's weird but okay it'd be a little finicky trying to start all three health checks ui look at that which blazer server or blazer demo actually says unhealthy the target machine actively refused it whereas this one is degraded which is kind of cool we're actually seeing something different here um but that's an artifact of of this not working quite right so we can get restarted here nope okay let's try it again let's coax it back to life here and if we go to health checks oops this is the mvc there we go health check ui there we go it's seeing both of them we can see you know if we can expand out we can see all the different things are going on we can collapse them all to just their project we can see which ones are healthy or not now seeing checks that don't change isn't really exciting and it's not really doesn't seem like it's a valid use of checks and it's not because just having a solid check in here that that just hard could doesn't do anything so let's close out everything let's minimize the health checker and the mbc demo and let's dive back into the startup for blazer demo because this we're going to make some modifications to and i make these modifications the first thing i'm going to do is right click on blazer demo i'm going to add a new folder and let's call this health checks and we're going to create a class in here that's going to do a health check for us one health check so let's call this our response time health check we're gonna do is say it's gonna implement i health check notice it's going to add a using statement so i hit enter which adds that user statement at the top but then control dot i can implement the interface and let's just give us a little extra room here this is all you have to do to implement a health check of your own so this is a whole class but this health check async what it's going to do is we're going to call it's going to call this method whatever the return here is which of course right now is throw new not implemented exception but whatever return type here is that's what it's going to check that's what's going to do now i need one helper class i'm going to put the class actually right in here i don't know i put classes inside of other projects but this is a really really simple helper class that's hardcoded it's ugly but i don't want to waste time implementing a different way so let's say public static class we're going to call this um helper not heal per helper methods like so this is a public static class where i have one uh property in here actually let's just create a public variable or a public field so public static random rnd equals new random now what am i doing here well i wanted to have a random value that was accessible and you know okay so this be accessible throughout my entire application i could use it but to do so i have to make this ugly work around unless i want to do a lot of work let's just do this let's cut it out let's put it up here because we're going to actually make this static anyway this will make me feel better this is a more valid way of doing this the the key here for a random number is you don't want to recreate or do the new random over and over again you want to do new random once for the life of your application therefore we don't want this to be reused or recreated in other places but since we're doing just for this health check we're going to put it outside of the method and then in here we'll just do rnd.next and since this will be a singleton and i'll show you how to do that in a minute it will not do equals new random every time it will do it once for the life of our application so just want to point that out ram is a bit tricky to get right i do have a video on random and how to deal with it but just know that you don't want to say equals new random inside this method because then you're redoing that over and over all right so let's say int response time in milliseconds equals rnd dot next from one to three hundred so a number between one a whole number between one and three hundred actually is going to not include three not three hundred so he wanted to 299 but close enough for our our uses i'm going to say if response time milliseconds is less than 100 that sounds like a good response time return task dot from result since this is a returning a task of this type this task.from result means just return it right away we're not doing anything async here but we're returning it like it was async but it's already done so task dot from result we'll say health check result dot healthy because that sounds healthy to me and we'll say string interpolation so a dollar sign in front of our string the response time looks good and i'll put in parens response time in no seconds like so let's um unpin this you can see more of what i'm doing here i'll i'll kind of shrink it just a minute and that's it so this right here you can go like this there we go so this right here is going to return a healthy result if the response time in milliseconds is less than 100. but what if it's not okay so we'll say else oops new line this is not javascript else if it's less than 200 i'm going to say well that's actually degraded it will say the response time is a bit slow it still works but that's a little slow and then finally let's do one more actually just copied uh this right here we'll say else and ctrl j for unhealthy is unacceptable so if the response time is less than 100 milliseconds cool that's healthy if it's less than 200 milliseconds that's the else if that's why we're we're falling through and we're only doing this if it's not less than 100 so it's 100 to 199 then say it's a bit slow if it's 200 or above say no that's unacceptably slow that's how you build a health check where you're doing some type of just really quick calculation to say is this good is this not and then you're telling the system it's either healthy it's degraded or it's unhealthy notice the return statement's right here so we're returning that value right away once we evaluate where it is in that cycle now since this is a random number every time we call the health check async it's going to get the next random number which means we're gonna see this value change and that's why i did it because i wanted since i'm not actually checking a response time and since if i did it probably about the same all the time i am simulating getting a response time so this would this code right here instead of having this you would do an actual check where you'd actually you know ping the server or you would talk to the database or you would look for um you know the service broker or whatever you're doing in this check that's what you do in this line so that's our one demo piece here the rest of this right here would be how you'd actually do the evaluation let's implement this in our checks and in fact let's comment out all these um let's come out out all the ones except for the database check and the reason i want to comment out the database check is because i want to have some type of check besides just a healthy one besides just this new check so our new check is called response time health check in fact let's just copy that makes life a little easier so we're say add check and the type is going to be health time response time health check control dot to add the using for that and then inside here we're going to say that this is a network speed test we're going to say no for the next this is the health status so it says the health status that she reported when the health check reports a failure if the the provided value is null it reports unhealthy so what's going to happen is if we can't call the health check we can't find the health check whatever then what do you call that is it healthy is it degraded is it unhealthy well if you can't find a check that's a pretty significant issue therefore by default it is unhealthy you could say well it's degraded because we can't find the check that doesn't mean that service isn't available you can argue it however you want you identify what the status should be but if you put null there it's going to say unhealthy by default and then finally we have here our tags so we say a new array we can put our tags here this is a um let's just say service please spell it right like so and that's it notice that i'm not putting a semicolon at the end because we're saying dot we're continuing the line until the last one and that gets a semicolon so just add this new response time health check but there's one more step to do and that is to say services.add singleton for that response time health check and the reason why is because then you declare that response time health check is a singleton so when it does the add check it's going to say we get one instance of this and use it for the life of the application which allows us to keep state between our calls which is what's going to allow us to have that same random value that random instance and we can just say next on it now let's start all this up and see what it looks like let's hope that all three will actually launch like they're supposed to all right all three cd launched health checks ui blazer demo this says network speed test is degraded wait it's unhealthy see it has changed it is 203 but now it's healthy because it's 26. so we can watch this status change in real time we can even go over to details and notice it now says has this history where it says hey it's currently at 264. it was at 26 but then it was at 203. so this is where the the changes in our status come about it's a bit slow to 111 and now notice we have more of those checks popping in now this this list right here i don't believe it updates until after you close and open it back up so if you're pulling really really fast like we are every five seconds which is a bit aggressive i would tone that down to maybe every 60 seconds or even every five minutes but it depends on how quickly you want to know that your service is unhealthy or even just degraded if you want to know really quickly then check every 30 to 60 seconds just know that every time you check it's running those checks over again so if those checks are long you want to either make those checks shorter which you should or you want to check less often which if you can do so now we can see this change in real time we can watch our services change we and this is what we can do we can open this page up and leave it open and just make sure it's all green and when it's not you go into the service and figure out why oh the food service integrated why well it didn't work that well not very great message but you get the point where as you come up here you see everything's green cool but as soon as that changes we can see the reason why the response time is unacceptable at 211. i should put ms at the end of that i'll do that just just for a second completion here um makes me feel better so now i'll say milliseconds but that's how you use hell as i create a real health check this is a real health check i encourage you to create a class for each health check you can even call them from external dlls so you create a package or nougat package or something like that they would be your health checks for your organization and then you could just bring them in and add them to your dependency injection like so and then add them into your checks like so i would encourage you to have basically just all these this right here quick and dirty stuff cool that works but the reality is that that's not ideal to have all this in startup.cs notice how ugly this section already is when it really should look a lot more like that or even if you had multiple of these like that that is clean that is easy to read it's easy to understand what's going on oh it's response time health check that's the network speed test oh it's a database health check that's for sql database oh it's the you know service broker health check for our main service broker so having that very clean look is is important to readability important scalability versus what we have with this that's why i encourage you to use these checks among other reasons because also notice all this this would all have to go inside of here so now you can see how fast our startup.cs file goes from 100 yeah 95 lines of code to more like 5 000 lines of code not ideal so we've seen how to add health checks to our application really quick to do we have seen how to add simple health checks like this or like this you want to do something quick and dirty and you've seen how to add a what i'd call a real health check which is a full class it does some real work maybe it has other methods in here that that would do some work and do some evaluation again just make sure they're quick but we've seen all of that we've seen how to then come down here and say hey just give us a second ping to make sure the service is up and we can see how to add a filtered health service that just gives us certain tagged items we've seen how to get the full json endpoint for application and then we've also seen how to use our health checker project with the health checks ui to get that nice user interface that that shows us how all of our different projects are working how the certain items underneath are are functioning and see all that data in real time now that's how to use health checks and it's also about why to use health check because health checks are like i said really important at the beginning of this talk we talked about well it's more than just a matter of making sure a site is up it's making sure that you know it's a database fast enough is it we have a slowdown somewhere we have are you running out of memory somewhere so let's talk about where what you would do a health check for because there's not built in health checks there's not a health check out of the box thing now there are some on the web that people have written for things like sql server and sql lite and so on for various checks but really the checks may be based upon your environment what are the things you are doing and what are the things that your application depends on and that's really what we're talking about here is monitoring our external dependencies for our application in general mostly for example is the database available that's a big one and then is the database responding quickly maybe you have a problem with your database getting bogged down you would figure that out much more quickly if you had a health check in place that had that degraded state when the database was still available but slow but then you can check if you rely on api you can check to make sure the api is available and that it's responsive if you're using logging which you should be then you're going to check to see if you have room left in your log file location maybe you're logging to files and you don't have it overriding the oldest log file therefore you're just building up more and more log files well you can run a space on the disk pretty quickly so you want to check that disk space or maybe you just want to make sure that you're actually logging something because the logging system goes down you want to know about it well you can also check the logging system to see how many exceptions you've had in say the past 24 hours well if you expect to have no exceptions but you have you know also a blip of a hundred exceptions unless you're looking at the log file all the time how would you know that well you have a health check in place that looks at how many exceptions happened in the last 24 hours and anything under 10 is healthy anything a you know under 50 is degraded and anything you know beyond that is unhealthy you can figure out what those points are for your application maybe you have a system in place where you try to do an action let's just say resize an image and maybe you know that it fails every once in a while because there's not enough memory or whatever but you have a retry system in place where you retry it three times before you totally you know error out well you could put the system at a degraded state if you're retrying multiple files more than once that maybe it's you're saying hey there's a problem here we're getting it done but it's really slow because we're retrying over and over or maybe you can check the memory of your server and say hey i can see that you know we're adding a megabyte every hour and at this rate the application will have no memory in two days well you can alert early as a degraded state and say there's a problem we're running out of memory or maybe you rely on a queue to get new orders in and that queue is not available your application will still run but since the queue is not available it won't be doing anything therefore you can put it in a degraded state saying i'm available but the queue is not therefore i'm useless these are just some of the endless amount of health checks you could implement in your application implement what works for you just like with logging though you don't want to implement just to implement you want to implement uh health checks that actually you're going to look at you're going to evaluate you're going to listen to because if you have a health check that puts your application in degraded state but you don't care then why are you putting your application in the greatest state just don't because why check that and do the extra cycles to check it every 60 seconds or every five minutes or whatever why do all those checks if you're not going to care if the status changes so only check things where you care that the status changes if you're looking for what to check look for all your external dependencies whether it's the system that's running on whether it's the file system for getting files or writing files databases you know message brokers and queues and external websites and apis check all of your external dependencies if your application relies on them and finally actually look at your health checks because if you don't look at your health checks why have them leave them alone now the really cool thing is you can use automated systems to look at your health checks for example azure app services has a preview feature in where if you put your application in degraded state it can actually recycle your application meaning turn it off and turn it back on again if you have a a load balance situation where it says okay we've got four running web applications they're all load balanced we've got one of them that went into a degraded state let's just use the other three for now we'll restart that fourth one and bring it back up get it healthy again and then add it back to the load balancing situation so that's really cool you can use an automated system like that since it returns json you can do a whole bunch of things with that on your own you can create your own views or reporting systems or automations you can tie into things like you know it tweets at you or it puts a message in slack or whatever you want to do you have the ability to do it's just a matter of make sure you actually use your health checks one thing to note is that you want to be careful how you do these health checks as to what you expose externally you can say in asp.net you can map and say okay that is actually you have to have authentication in order to see that url now i didn't use authentication in the system this is just an intro too but i do want to point this out because even though it's an intro to and even though i'm just showing you what it can do i do want to make sure that you're ready for the real world in the real world you might not want to expose your application's health to the external world this may be an internal only thing so maybe you don't allow this to go outside the firewall or maybe you say hey you have to be authenticated or authenticated at a certain level in order to go to this this end point so just kind of note that there are systems out there or services out there that instead of publishing it on a url an endpoint instead it packages it up and then it will ship that off somewhere which means it never is exposed on your actual web application just note think that through when it comes to micro services you're probably okay because usually your micro services are not exposing things publicly or if they are they're only exposing little bits and pieces not the whole micro service so you're probably okay there but if you're a full web application think that one through if you want to be simple about it just make sure you don't put anything sensitive in that path and you can even change the url to be something a little less conspicuous than health but you can also put authentication against like i said there's a whole bunch of things you can do just think it through okay i think that covers the introduction to health checks this is a really important feature of asp.net core and net core in general and it's something i really would like to see you implement in your applications i'd like to see you put into practice let me know what you think let me know you know what do you use health checks for or what do you want to use health checks for and let me know if you have any questions down below i'll do my best to get to them i've not been able to get to every post or every question especially detailed questions just because of the time constraints in doing so for hundreds of thousands of people but i will do my best to read and respond where possible but even if not if you see somebody's question that maybe you could help out on i love it if you would jump in and give them a hand thanks for watching as always i am tim [Music] [Applause] corey [Applause] [Music] you
Info
Channel: IAmTimCorey
Views: 26,049
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# training, C# tutorial, timco retail manager, asp.net, .net core, health checks, .net core health check, asp.net core healthchecks ui, asp.net core health checks, asp.net core health checks example, asp.net core
Id: Kbfto6Y2xdw
Channel Id: undefined
Length: 73min 52sec (4432 seconds)
Published: Mon Oct 12 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.