7. Prometheus Counter metric type practical example with handson | Custom metrics with prometheus

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today's video i am going to show you how to expose custom matrix so today's video's objective is how to expose counter matrix type we will expose a counter metric type on this prometheus endpoint so our simple objective is whenever somebody will hit our any of the rest point in our application we will log it here and then prompt a server will slap on it also this in the second part of the video i will show you how to log a counter matrix type for all the log levels this will be very important use case in practical scenario let's say you need to check if there are many errors in our application let's see if you have a thousand errors in last five minutes then that means something is really wrong in our application and then with the help of alert manager we can generate alert via sms or incident and then you can act on it then i will show you how to query on the prometheus server whatever metrics you expose like we will expose total request and how to execute a query to get the data so let's get started so before starting just i want to show you 10 seconds of my previous video where i showed when to use which matrix so best way to watch my video is go to the chapters and here on the counter so counter if if you want how many times x happened but that value can only increase in your application meaning counter is useful when something increased and not decrease we will use a counter matrix type only when then we need to check how many times x happened and that value can only increase and not decrease in our application so our simple objective is whenever somebody will hit this end point or let's say any of the end point in your application or all the endpoint in your application then you have to increase the count and log it into the prometheus matrix so we know that we need to use a counter for this purpose okay and counter is from i o promises client so let's check the method definition here so here you can see counter constructor here it is having a builder design pattern means we can use a build method and in the build method it's accepting a name and help so the first rule is whenever you define a matrix type any of the matrix type always use a static final like you do in the logger the reason behind this is so that nobody can use the same variable name same class variable name in the application elsewhere so like the static final counter counter equal to counter dot build dot name so we will define a name let's say we are doing a total request okay and help so anybody can understand what we are doing in this matrix type let's say total number of requests okay and then register the matrix that's it you are done and now we will use this variable so our objective is whenever somebody will call this we will increment so before here we you can see increment and increment with the amount so you can give the increment then it's increment by one or you can pass the value also and that's also correct okay but in this case our aim is it should be incremented by one right so whenever somebody will hit this we will increment by one and that's it your purpose is done so let's run the project and check so as you can see application is started let's run this you can see help whatever we give there in the help is here coming here total number of request okay total number of requests and this is the actual name we gave here total request okay so currently it's a 0.0 so let's go here and run one time hello hello world and now refresh this matrix you can see value is incremented by one let's do it again here value is 2 okay so this was our first object so now i will show you how to query in the prometheus server so our prompted server is pointing to this endpoint this matrix type here so whenever you search like this so you have to take the name whatever name and then go here you can see total request is there execute you can see value is 2. so here you got the actual value now you want to check in last five minutes how many time we received or somebody hit our resting endpoint so so you can do it with the rate function so red and here you give your type and the time so let's say 5 minute you can see value here so total time series meaning what it is doing it's a per second request in last five minutes so it's like a rate it's not the exact value it's a rate if you need exact value then directly type like this and it will give you the exact value so you can see two times but the one more extra thing you need to understand rate will only work with counters not with the causes okay so now let's jump to the next problem statement this will be very important use case in practical scenario because what will be the use case let's say you need to check if there are many errors in our application let's see if you have a thousand errors in last five minutes then that means something is really wrong in our application and then with the help of alert manager we can generate alert via sms or incident and then you can act on it so let's go to our code this is how you enable logger right so now my objective is in your case it will be like you will have console.log or sorry logger dot info and logger dot error somewhere in our application but for demo purpose what i am going to do what i will do i will expose one get endpoint like uh log and whenever somebody will call this endpoint i will expose when somebody i will i will write this much amount of logs and then i will return check out the logs okay and that's it save so you would start stop let's check if it's working at least you can see and i don't have any uh logback.xml so by default it will print it into the console it started from this endpoint now we have one endpoint called log check out the logs you can see three lines it's printed you know by uh why only three lines otherwise you have to go to here and then you need to change the logging level to let's say one or a trace and then it will enable all but i'm not going to do it this i i will i'm okay with this three so now the next thing is whenever we are printing something into the log file or in the console or from any of our application or from anywhere in our application it is printing the logs how we can expose it in the promoters endpoint for that we don't need to write lot of code just go to pom.xml and we have one dependency so there are many libraries which are written to solve these things to give you ready to use code so from official io dot prometheus prometheus and here if i go and check for log you can see there is one for log back and lock for jam using log back so i will use this and version let's check the latest version 0.9.0 okay and once dependencies are resolved you can see this is this is resolving the dependency so first thing is it will enable now our next thing is we need to tell our application that before appending meaning this is a console appender right before appending anything let's have a appender or let's say let's have a filter of a prometheus in between so for that let's go to our logback spring.xml here you can see i don't have anything just i'm extending a page.xml and here you need to add the appender so appender of of prometheus and which is of class io dot prometheus dot client dot you can see here you have a lot of option from a log back and go to you can see instrumented appender and once this is added and now we need to refer this appender so let's say you have root level defined so of info and sorry appender ref you need to give the reference of this of appender name matrix and that's it you are done so let's run the project so what it is doing if you go here you can check it's just appending so it is like a event listener whenever there is an event to trace to debug to info it is just appending to the prometheus end point so whatever this is started let's go to browser and let's try to hit log and now check from it yes you can see here it's added so the default name of this matrix type is log back appender total which is of a tau type counter and what it is giving log back log statement at a various log level so you can see here one just we added one line error one line and info 50 why it is giving 50 let's go to here and you can see you have a lot of info here so it added all the info from here okay so this is our next objective and now how to query in prometheus just take this name go here and you can see here you have values with the rate because it's a counter it's of type counter so let's take off maybe two minutes you can see for two minutes this is the rate per second okay and like this in next video we will see how to enable or how to expose a gauge matrix type in next video i will tell you a one interesting uh example for the gauge we will monitor the disk uses with the watch and check if the log file where we are printing this locks this locks if that lock size is having a less size or let's say you already use a 60 70 percent of the size then generate a alert via alert manager from a prompt yes server if you like the video then please hit that like button and please comment and tell me how it was thank you very much for the time being and see you in the next video for the coach thanks a lot bye
Info
Channel: Namaste Java
Views: 10,989
Rating: undefined out of 5
Keywords: namaste java, custom metrics prometheus, custom metrics, custom metric instrumentation, Prometheus Counter metric type, Custom metrics with prometheus, prometheus counter metrics, prometheus metrics tutorial, prometheus metrics explained, prometheus metrics types, prometheus monitoring, prometheus monitoring tutorial, custom metrics prometheus spring boot, custom metrics prometheus kubernetes
Id: fJziSyJJgtU
Channel Id: undefined
Length: 13min 5sec (785 seconds)
Published: Sun Dec 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.