Spring Boot Performance Monitoring
Tags: springboot, performancmonitoring
Published: 2020-05-21 08:56:36

<p><strong>Spring Boot Performance Monitoring.</strong></p>



<p>Modern applications are highly distributed in nature, and thus they are often bundled with various dependencies like Database, caching and much more. Monitoring becomes essential due to the complexity of the implementation scheme that comes amid such advanced software. The Spring Boot Performance Monitor is a library kit that comes preinstalled with any Java Agent. It utilizes HTTP endpoints or JMX beans – because you can also configure the proxy profile to display Spring Boot JMX metrics, to enable us to interact with it. By way of this, we get firsthand reading of operational information about the running application<strong>&nbsp;</strong>– health, metrics, info, dump, env, etc.&nbsp;&nbsp;</p>



<p>The Spring Boot Monitoring toolset provides you with an immediate and extensive insight into your Java applications running on the Spring MVC Framework. In its essence, the Spring Boot Performance Actuator is a unifier that introduces production-ready features into our application. With this dependency manager, such functionalities as gathering metrics, monitoring our apps, understanding traffic in addition to the state of our Database becomes inconsequential. The Spring Actuator discovers the application name through of the spring.application.name,&nbsp;property located in the&nbsp;application.properties file or the SpringBoot Application&nbsp;startup class, if the spring.application.name property is not defined.</p>



<h2><strong>The Spring Boot Actuator</strong></h2>



<p>Notably, Spring Boot supports many additional features that help you manage and monitor your application, from the development stage, extending to production. Such auditing exercise as metrics and health gathering are seamlessly implemented through the Spring Boot Performance Monitor. Once installed onto the classpath, this dependency supplies us with several endpoints right out of the box.&nbsp;</p>



<p>As with several other Spring modules, we can extend and configure the Spring Boot Actuator in a variety of ways, of which we would be looking into shortly.</p>



<h2><strong>Getting Started</strong></h2>



<p>Typically, a monitoring system would have three core components which include;</p>



<ol><li>Dashboard for the visualization of data stored in the Database.</li><li>A Metric Store &#8211; Time-series Database. For example, <a href="https://prometheus.io/docs/introduction/overview/">Prometheus</a>, <a href="https://docs.influxdata.com/influxdb/v1.7/introduction/getting-started/">InfluxDB</a>, <a href="https://docs.timescale.com/latest/introduction">TimescaleDB</a> etc.</li><li>Applications that periodically supplies the Metric store with metrics data from the application&#8217;s local state.</li></ol>



<p>We can go on ahead to have other components installed as well like alerting – where the alert channel could be via Email, Slack or other conventional media. Alerting components would be sending alerts to the application owners or subscribers of events. For this guide, we would be using <a href="https://grafana.com/oss/grafana/">Grafana</a>&nbsp;as a dashboard and alerting system,&nbsp;<a href="https://prometheus.io/">Prometheus</a>&nbsp;as a metrics store system.</p>



<h2><strong>The Requirements</strong></h2>



<ol><li>An IDE</li><li>Java Development Environment</li><li>Gradle</li></ol>



<p>Now we create a project from the <a href="https://start.spring.io/">Spring Boot initializer</a>, after which we add dependencies – as much as we want to. Next, we use the <a href="https://micrometer.io/">Micrometer</a> library, which is an instrumentation façade that provides bindings for many metric stores like Prometheus, New Relic, Datadog, to mention a few.</p>



<p>Micrometre provides the following functionalities ready for use;</p>



<p>1. JVM.</p>



<p>2. Database.</p>



<p>3. HTTP request.</p>



<p>3. Cache system and related metrics.</p>



<p>While some of these metrics are enabled by default, others are subject to customized settings. For this feature; we would employ the application.properties file to handle enabling, disabling and customization. We would also need to use the Spring Boot Actuator to expose the endpoint of the Metric Store &#8211; Prometheus.</p>



<h2><strong>Steps: &#8211;</strong></h2>



<ol><li>Add these following dependencies in build gradle file just as done in <em>line 7</em> of figure 1.:</li></ol>



<ul><li>io.micrometer:micrometer-registry-Prometheus.</li></ul>



<ul><li>org.springframework.boot:spring-boot-starter-actuator.</li></ul>



<figure class="wp-block-image"><img src="https://lh3.googleusercontent.com/iPeJ5ufvZXxxfuORyHhTvLOxBVVG-RJV1a4DtSq3HC14e5-jTYyjyT3mo5Nb2dLGS2z-3FN250BFJkfTCICQBBjfFbUNBDB4TS7wG051oHSY42KIKKUxxSnq0ja5U0HGV7fGxUROg4JSXvGoSw" alt=""/></figure>



<p><em>Figure 1:</em></p>



<ol start="2"><li>We would enable the Prometheus export functionality by adding the following line to the properties file.</li></ol>



<figure class="wp-block-image"><img src="https://lh3.googleusercontent.com/pMJlK2dCzZr54Llcq1d7GSf059TunqUOMME5lwah_3el067COrmP3yy0lGGYITPFU8XFfft3H_GBf6LVWtUtHKP4N5DyaAiY9MnXhwDwMRyK_qtjMBvKWIoERFAvpHkR28WxcDrlwGMIVz30eA" alt=""/></figure>



<p><em>Figure 2:</em></p>



<ol start="3"><li>As soon as the above command is enabled Micrometer automatically accumulates data about the application. This data can be viewed by visiting the <em>actuator / Prometheus </em>endpoint &#8211; which is used in the Prometheus script to fetch data from our application servers.</li></ol>



<p>Although we have included the line mentioned above of code into the properties file, we still won&#8217;t be able to browse the Prometheus endpoint since it is disabled by default. For that, we head on to the management endpoint and add Prometheus in the list.</p>



<figure class="wp-block-image"><img src="https://lh6.googleusercontent.com/kDD6UESDEwHilGVPdp6G5oVMy9vNa-DJsi_5yUhXu9bqJ2XKI4Zu6cxts46XWk9vM-TxoSxaFs98qNuHzskcFiEaimJUEl_X4rcWgH360GZRv9pbnLyN3apir53FR3GieU4xZXWROkoYErfU1w" alt=""/></figure>



<p><em>Figure 3:</em></p>



<p><strong>Note:</strong></p>



<p>Not all endpoints are enabled from actuator<strong> </strong>as this could lead to a security loophole. Hence, we choose endpoints selectively, especially in a production environment. Even though we need to use these endpoints, it is not advisable to expose certain endpoints to the whole world as we place a whole lot of sensitive application data at risk. Therefore, it is best to use some proxy to hide these endpoints from the real world.</p>



<p>Also, various components of the HTTP requests are customizable much like the <a href="https://en.wikipedia.org/wiki/Service-level_agreement">SLA</a>&nbsp;(Service<strong>&nbsp;</strong>Level&nbsp;Agreement), percentile histogram should be computed or not. This can be done through the metrics—distribution properties.</p>



<p>A sample application.properties can contain the following lines;</p>



<figure class="wp-block-image"><img src="https://lh4.googleusercontent.com/1UmRZ69kQTOjS1YxA9TOJBSZdErPAqE3c0uB6bCsYjAiHSBf90MZ_eavJhgnybCByoYbyoYCOVu9RUMzM7Kt0ZJHe8gZzjYq9Mf4PRR7hg7EnzGkzG5iqIZc8t4aca0Po4ozYiuYFXeLnmn8WQ" alt=""/></figure>



<p><em>Figure 4:</em></p>



<ol start="4"><li>We can now run the application and navigate to the page <a href="http://locahost:8080/actuator/prometheus">http<em>://locahost:8080/actuator/prometheus</em></a><em> </em>on a browser<em>,</em> to view our data.</li></ol>



<figure class="wp-block-image"><img src="https://lh3.googleusercontent.com/WhLFS2ApGgJ-CqYSDsQ4rjjtjEvk62ZHYTUeFXImF_TVgGfZiM8QdDAai3ULR8eO6ChP-I_LmVvgF51BnUWD9lMdnlcI-nuRWoQhWuA8l8Cw1lOdLS0NjR0GKWzwssuo1SOv1N0mY-EEc0NdFA" alt=""/></figure>



<p><em>Figure 5:</em></p>



<ol start="5"><li>The above data displays HTTP request detail, such as</li></ol>



<ul><li><strong>exception=None,</strong> which means there is no exception just as the name implies. If any, we can use that to filter how many requests have failed due to that exception handler.</li></ul>



<ul><li><strong>method=GET&nbsp;</strong>HTTP method name.&nbsp;</li></ul>



<ul><li><strong>status=200&nbsp;</strong>HTTP status code is 200.</li></ul>



<ul><li><strong>uri=/</strong><a href="http://localhost:8080/actuator/prometheus"><strong>actuator/prometheus</strong></a>&nbsp;displays the URL path.</li></ul>



<ul><li><strong>le=xyz&nbsp;</strong>displays the processing time.</li></ul>



<ul><li><strong>N.0&nbsp;</strong>displays the number of times that endpoint was called.</li></ul>



<p>This data can be represented as a pie-chart or histogram that can be plotted in Grafana, for example, to <em>plot p95 over 5 minutes</em> we can use the following query.</p>



<figure class="wp-block-image"><img src="https://lh3.googleusercontent.com/3MP_qT_18aQWyr6-lCmh9ubYaw20umm4ehq7HlzZEUAU3eBNZFK0pxjaNdrAKKbo_RPO7c_2GVkapBGWkaptuGu0WIGxUF8OhEvW_OyVzEFU0maxj6fWZLhcOz2M5mBzCG8nfATPKdfDveah5A" alt=""/></figure>



<p><em>Figure 6:</em></p>



<h2>Custom Metrics</h2>



<p>Most times, we might need to customize our metrics where some of such use-cases include the number of logged-in users, number of orders in the order queue, currently available stock details etc. Certain business use cases can be solved using custom metrics. Thus, Micrometer supports multiple kinds of metrics like Gauge, Counter, Timer, Long task timers, Distribution summaries etc. But for the scope of this walkthrough, we would focus mainly on Gauge and Counter. Gauge gives us instantaneous data like the length of a queue, whereas the counter is much like a uniformly increasing number, starting from one.</p>



<p>For this, we&#8217;re going to create a demo stock manager that will store details in memory and would provide two functionalities:</p>



<p>1. Add items.</p>



<p>2. Get items.</p>



<p>By that, we&#8217;ve created a single counter and an available gauge in<strong>&nbsp;the </strong>init<strong>&nbsp;</strong>method. Therefore, whenever&nbsp;getItems&nbsp;is called, we increase the counter as well as to measure the stock size. While a call to&nbsp;addItems&nbsp;updates the gauge.</p>



<figure class="wp-block-image"><img src="https://lh3.googleusercontent.com/J7sn6r4rYkCuzyZfGlvmm-8d4FoRcKbqWcFo59CRTZxmQTnHciXMMiepHMgFcslfRxtF0IcD9etNIYvrQ60jBOOWAtGcJO2VFOYHfj-XUYcOacwKChv9SbiVY4gGtR3Tpa1wo7XTylJ3FxOiyg" alt=""/></figure>



<figure class="wp-block-image"><img src="https://lh6.googleusercontent.com/uAYK9n4YGsPYDsKnl3ucCJpZzg5-0AHLTU__xaWn7shs-S4Pgu9_o0Hw4LlBppet08k_jD-u8T5_4tYqOVNfJ_GWrUU6aXYJZVlvlIv8W6-VPGrwAJKGMOa6OMiA2f_r7F-qztHNBei6Fg_wRg" alt=""/></figure>



<p><em>Figure 7:</em></p>



<p>For demonstration purposes, we&#8217;ll add two endpoints to add items and get items.</p>



<figure class="wp-block-image"><img src="https://lh6.googleusercontent.com/UcGM4z5qyEu9tYxKwcnP5GFut_oFPJaJp1M78UDXQ8eaBVv7nzuEVYIce5hyWOQweoQLGOVqcB-WY7Ch117q8GWD2t07tMiIcsTJaMdwvAAOlMNiCAH6BHUNAg4dWS6uBOXpq7HkfGmmpTyhwQ" alt=""/></figure>



<p><em>Figure 8:</em></p>



<p>Firstly, we add ten items using two API calls.</p>



<ol><li>Curl -X POST http://localhost:8080/stocks?items=1,2,3,4</li><li>Curl -X POST http://localhost:8080/stocks?items=5,6,7,8,9,10</li></ol>



<p>Now if we browse to Prometheus endpoints, then we can see the following data indicating that we currently have 10 items in the stock.</p>



<figure class="wp-block-image"><img src="https://lh3.googleusercontent.com/bYwedouuKPF15dulbyg4J87e6ybtsSIv-dHHOQ2r4_s87OjGQ7aGB7P1C3rQ3_En3Mr6YMbgwyWuw6afmSwB8SMPko8rgd-1ZLWJKyjp7jpcDXP-Bcp80EcUcy18Xppkl1f4vvy9R9K0u8lSyw" alt=""/></figure>



<p><em>Figure 9:</em></p>



<p>Now, we&#8217;re going to place an order of size three;</p>



<figure class="wp-block-embed"><div class="wp-block-embed__wrapper">
http://localhost:8080/stocks?size=3
</div></figure>



<p>Then again, if we browse Prometheus endpoint, then we get the following data that indicates stock size has been changed to seven.</p>



<figure class="wp-block-image"><img src="https://lh6.googleusercontent.com/0PzUf170Fa0Ze_FT5H2ldAuR9v7xsudyUDsnPgQEiNJV0EKZcLXxY_-1OWN-1KKctt-mtKLt0_HQPu1s9ofSl_do-BpbrxNN3sCQSQSt4iNwcyGL1km95WDwY0nR_03ADlS2GEt2BzEDrZsKTw" alt=""/></figure>



<p><em>Figure 10:</em></p>



<p>We could also see that the counter has been updated with a value of 1. This indicates that just a single order has been placed.</p>



<figure class="wp-block-image"><img src="https://lh4.googleusercontent.com/fjE1vtZw68n9lH-IyQmdFoRUqgHPwrd34PPshk-Pjp49MDnln9RbdYkcU6cLAwkWJzE90klhPiYOmcONLwjY-DzTjx2SdNNbmW8qWUms6c6Y9lEEIYdtXjK8AAYhFT5mf3lB06Elk9nCYgU4hw" alt=""/></figure>



<p><em>Figure 11:</em></p>



<p><strong>Troubleshooting with Spring boot performance monitoring</strong></p>



<p>Other tools like FusionReactor permits you to troubleshoot the most complex of application performance issues. They were giving you instant insight into where problems are occurring or where your application is performing poorly.</p>



<p><strong>Critical Features for Spring Boot Performance Monitoring Tools</strong></p>



<p>Below are some of the critical features that FusionReactor provisions, along with the Spring Boot Performance Monitoring Tools.</p>



<ul><li><a href="https://www.fusion-reactor.com/production-debugger">Production Debugger</a> – Production, Safe / User, Controlled enclosed debug environment.</li><li><a href="https://www.fusion-reactor.com/production-java-profiler/">Code Profiler</a> – Production Safe / instantly see performance bottlenecks.</li><li><a href="https://www.fusion-reactor.com/memory-profiler/">Memory Profiler</a> – Production Safe / Real-time heap analysis to locate memory leaks.</li><li>Instant Code decompilation.</li><li><a href="https://www.fusion-reactor.com/fusionreactor-metrics">JMX MBeans</a>&nbsp;Support.</li><li><a href="https://www.fusion-reactor.com/crash-protection/">Crash Protection</a>&nbsp;– Constant application availability and performance checking.</li><li>Generate alerting for poor performance or when resource thresholds (memory/CPU) are reached.</li><li>Isolate Long Running threads.</li></ul>



<p>One essential Actuator software for monitoring and interacting with your application is <a href="https://www.fusion-reactor.com/spring/spring-boot-performance-monitoring/">FusionReactor</a>, as it goes beyond &#8220;just monitoring&#8221;. FusionReactor instruments web-based transactions within the Spring 2.x and 3.x MVC Framework, hence provisioning a seamless transaction identification process. Also, proactively mitigates server downtime while expediting time to a resolution just like no other Spring Boot Performance Actuator on the market.</p>