Grafana : Setup Grafana for Spring Boot app | Actuator, Prometheus & Grafana | Monitoring & Alerting

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody welcome to a brand new video of channel code board club in today's video we will discuss how to set up rafana to monitor a spring boot application in the video i'm going to cover what is grafana what is the benefit of using grafana and finally we will set up rafana from scratch so without any more delay let's begin after deployment of an application and production environment our main concern becomes to provide the best experience to the end user and for that monitoring the application health is an important aspect we always want to ensure that if something goes wrong with the server or application health we should get an alert before it affects the end user while we say server monitoring and alerting grafana comes into the picture grafana is a tool which helps us to monitor application and server health if something goes wrong with the application health grafana and trigger customized alerts as well grafana supports customizable and beautiful looking dashboard with colorful graphs today we are going to set up grafana to monitor a spring boot application so without any more delay let's jump into the setup first let me show you that i have created a simple spring boot web project with name monitoring we have spring boot starter and spring boot starter web dependency in palm.xml file i have mentioned the server port as 8888 and this is the main method of our application which is the starting point now we will create a simple rest controller with namedtestcontroller i will add rest controller annotation in the class also let me configure an endpoint slash monitor and the method will return the string hello world let's start the application it started in the port 8888 let me open the endpoint localhost colon8888 then slash monitor and we are getting the output as hello world so our application is working as expected now before going into the grafana setup implementation let's clarify what exactly do we mean by application health so there can be different metrics to monitor application health like how much memory is the application consuming or what is the cpu consumption by the application if we say specifically about java based application health measurement can be done based on heap memory usage garbage collection timing etc so the next question should be how do we get these data from a spring application the answer is actuator using actuator we can get different application related information quite easily let's configure actuator and our spring boot application to configure actuator in our spring boot application we first have to include spring boot starter actuator dependency in palm.xml file after that in application properties file we have to configure two management and point properties management.endpoints.web.exposure.include equals to star and management dot endpoint dot health dot show details equals to always now let's restart the application and if i hit the end point slash actuator we will be able to see all of the available actuator endpoints these actuator endpoints will provide us different application related information for example there is an endpoint slash actuator health if we hit this end point we will be able to see memory consumption related information but to make these information visible in a separate dashboard we need the data in a specific customized format for that we will use prometheus to use prometheus with our spring boot application we will have to add prometheus dependency in palm.xml file now let's restart the application again if we refresh the actuator endpoint we will be able to see a new endpoint related to prometheus and that endpoint will provide us different health measurement metrics our next step is to download prometheus server you can download prometheus server from the site prometheus dot io download here you can get options to download prometheus server for different operating systems i will download for windows it will take few seconds to complete the download let me extract the zip file inside the extracted folder there will be a configuration file prometheus.yml we have to add few configurations in the file let's open the file towards the end of the file there will be a section scrape underscore configs we need to add a new job under this section let's add job underscore name spring actuator metrics path will be the actuator prometheus endpoint that is slash actuator prometheus right next is the scrape interval let's keep it as five seconds which means in each five seconds application health data will be fetched from the actuator endpoint and finally there will be a section as static underscore configs under this section we have to mention our application host and port which will be local host colony 888 correct i will mention these configurations in video description that's it let's save and close the file now we have to start the prometheus server for that we need to run the prometheus.exe file let's open a terminal here and execute dot slash prometheus.exe as you can see the server is started at 9090 port let's open localhost colon9090 in the browser and that's our prometheus dashboard here we can select different health metrics let's select the garbage collection duration and click on execute the data will be displayed below let's go to the graph tab and this is the graph of garbage collection duration against time let's try another metric so this is the metric for memory consumption by the application and this is the graph graph style can also be changed our next step is to set up grafana you may ask if prometheus is providing us the data and graphs then why should we go for grafana that is because grafana ui is much more customizable and provides us a better insight at a glance on the application health we can simply search in google to download grafana and you'll be able to see the link to download grafana for windows in the page options are available to download for other operating systems as well let's download the zip file it will take few seconds to download let's extract the zip file go inside bin folder to start the grafana server we have to run perfanaserver.exe let me open a command prompt here now run dot slash profana server.exe notice that the server is started in the port 3000 so let's open in the browser localhost colon 3000 there you go grafana login pages appear default username and password for grafana is admin once you will log in it will ask to change the password let's skip it for now we are finally at rafana welcome page now our first step will be to set up a data source so let's go the configuration icon at left side menu go to data source click on add data source you will be able to see different data source options for us it will be prometheus right in prometheus setup page we only have to mention the prometheus host and port which will be localhost colon9090 correct let's save and test it if everything goes well you will be able to see a success message let's go back you can notice the prometheus data source is added under data source section now we will proceed to create a dashboard click on add an empty panel you will be redirected to a panel configuration page in this text box we will have to mention the prometheus query let's copy the prometheus metric name from the prometheus dashboard and paste it here well that's our graph let's reduce the time range for a better view let's save the panel so the first graph panel is added in our dashboard let's add another panel this time we will add a different metric and here it is the garbage collection timing graph so we have added two panels now we can rearrange the panel positions as well now if we go to settings there is an option to get a json model of the dashboard this json holds all the configurations of our dashboard just importing this json anyone can also able to create the same dashboard like us let's save the dashboard now let me put dashboard name as spring dashboard there are different grafana dashboards available in json format in the internet we will now import one such json instead of creating each panel this is one such grafana dashboard for spring application i will mention the url in the video description some user has already created this spring boot apm dashboard and published it in the internet let's download the json one json file will be downloaded now go back to grafana and from the left side menu click on import select the downloaded json file now select the data source as default prometheus which we have set up earlier click on import bingo a fully functional dashboard is ready for us we can see different health metrics of our application from the dashboard our next step is setting up alerts before going into that part let me request you all please subscribe to channel code board club your subscription likes and comments motivates me to create such interesting videos for all of you now we are going to set up email alerts for that let's go inside the configuration folder we are going to edit the defaults.ini file find out the smtp section in the file we have to set up email configuration here first set enabled as true we are going to use gmail so the host will be smtp.gmail.com and port will be 465. now put the email id from which the alerts need to be sent also mention the password for the mail id save the file now we have to restart the grafana server just stop the server using control c and start it again grafana server is restarted now let's go back to grafana ui from the left side menu let's go to alerting and then notification channel click on add channel first we have to provide a channel name let's keep it as alert admin the notification type should be email right now we need to add email id where the notifications will be sent let me provide it as channel codeboard gmail.com now at the bottom click on test if everything is good you will be able to see a success message test notification set if i go to my email account inbox i will be able to see a new email from grafana the email is for test notification right let's go back to rafana ui save the channel the channel is now added into the channel list let's go to manage dashboard we have two dashboards one is created by us manually and the second one is created by importing the json right let's select spring apm dashboard we will add alert for cpu usage now we need to edit the cpu usage panel to add an alert adding alert is not supported in grafana when template variables are being used in prometheus query which means from the query section we need to remove all the variables which are mentioned with dollar sign let's remove those first now go to the alert tab and click on create alert now we need to define the rule when the alert should be triggered let's keep the rule name as high cpu usage alert this rule should be checked in each five seconds and the alert should be triggered only if the rule condition is violating for more than 10 seconds now we need to define the conditions so the alert should be triggered if the maximum system cpu usage in last 10 seconds is above 0.3 that is more than 30 now we need to add notification channel which means if the cpu usage is more than 30 where do we need to send the notification let's select alert admin channel we can put some customized message as well let's write high cpu usage warning please check your app asap now the expectation is if cpu usage is more than 30 i should get an alert in channel codeboard gmail.com right let's save the panel now i need to write a code which will consume icpu to test our alerting functionality so let's go to test controller class i will write code inside monitor function let me write the code then i will explain it okay i am creating multiple threads here inside an infinite loop right and each thread will invoke another infinite loop which means threads will never die so numerous threads will be alive and will increase cpu usage let's restart the app now from this newly developed code the expectation is whenever we will hit the monitor endpoint we should be able to see cpu usage spike let me hit the monitor endpoint now and you can see the cpu usage is increasing rapidly now if we go to grafana you can notice the little heart is broken which means the health rule is violating now in my email inbox i have received multiple notification email as well so grafana set up for application health monitoring and alerting is working as expected so that's it for today we will meet again in a new video please subscribe to channel code board club and don't forget to like and share your feedback in comment section bye for now
Info
Channel: Codeboard Club
Views: 4,078
Rating: 5 out of 5
Keywords: grafana, grafana tutorial, grafana tutorial for beginners, grafana dashboard, setup your own grafana dashboard, continuous monitoring with grafana, grafana dashboard creation, learn grafana, learn grafana dashboard, grafana alerting tutorial, continuous monitoring grafana tutorial, devops, getting started with grafana, grafana tool devops, grafana devops tutorial, devops training, java, spring boot, spring, alerting, email alert, prometheus, actuator
Id: gJZhdEJvZmc
Channel Id: undefined
Length: 17min 2sec (1022 seconds)
Published: Wed Jun 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.