Monitoring Spring applications

Spring Framework has some built-in features for monitoring and providing metrics to know the health of applications. We have multiple ways to do this, so let's review some of them:

  • We can use an old-fashioned approach that implies creating interceptors around methods to log everything we want around them.
  • Spring Actuator can be used along side Spring Boot applications. Using this library, we can review the health of an application; it provides an easy way to monitor applications via HTTP requests or JMX. Additionally, we can use tools to index the data produced and to create graphs that are helpful to understand the metrics. There are plenty of options to create graphs, including:
    • ELK Stack (ElasticSearch, Logstash, and Kibana)
    • Spring-boot-admin
    • Prometheus
    • Telegraph
    • Influx, and
    • Graphana, among others

Spring Actuator can be integrated as part of an existing Spring Boot application adding the following dependency as part of the build.gradle file:

compile('org.springframework.boot:spring-boot-starter-actuator')

If we are using Maven, we would add the following dependency as part of the pom.xml file:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Actuator supports many configurations that must be provided in the application.properties file. We are going to add some properties to this file to provide metadata, such as the name, description, and version of our application. Also, we are going to run the Actuator endpoints in another port with the security model disabled:

info.app.name=Banking Application Packt
info.app.description=Spring boot banking application
info.app.version=1.0.0
management.port=8091 management.address=127.0.0.1 management.security.enabled=false

Then, after running the application, some endpoints provided by Actuator will be available. Let's review some of them:

  • Health: This endpoint provides some information in general about the application health in the http://localhost:8091/health URL:

Health endpoint result
  • Info: This endpoint provides information about the metadata of the application, which was previously configured in the application.properties file. The information is available at http://localhost:8080/info:

Info endpoint result
  • Metrics: This provides information about the OS, JVM, threads, classes loaded, and memory. We can view this information at http://localhost:8080/metrics:

Metrics endpoint result
  • Trace: This provides information about the most recent requests made to our application. We can view this information at http://localhost:8080/trace:

Trace endpoint result
If we want to review all endpoints, we can find these in the official documentation of spring: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-endpoints.

As we have seen in the Actuator library, we are getting snapshots of our application at a certain time, knowing the status and health of the application, or even tracing the most commonly used endpoints.

Sometimes, the information provided is enough. If you are looking to have graphics and inspect historical data, you should integrate the tools we mentioned earlier.

Spring Actuator also offers the ability to collect custom metrics about the application; this is helpful for gathering business metrics. For example, if we are working with an application to create savings accounts, we can collect metrics to know how many accounts are being created. Then, after opening more branch offices, we can see how many more accounts are created and figure out the impact it has on the business itself. 

The key factor when we are collecting business metrics is to understand what is important for the business. To achieve this task, it is important to work together with business people. 

Business metrics are also helpful for understanding the impact we generate after releasing new features. It also facilitates an understanding of unexpected behaviors or bugs. Imagine that you roll out a new application version using a different email provider; you should compare the number of emails that are being delivered after the change with the number of emails delivered before changing the email provider. If you find a big difference in these numbers, you will need to check what is happening because the difference should not be too much. If you want to learn how to create custom metrics, I encourage you to visit this link: https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-metrics.html.  

There are many tools available on the market that allow us to monitor applications without changing the code, and these tools are referred to as Application Performance Management tools (APM). We are going to review how these work in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.86.211