DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Setting Up Local Kafka Container for Spring Boot Application
  • Spring Boot Application With Spring REST and Spring Data MongoDB
  • Developing Saga Participant Code for Compensating Transactions

Trending

  • Mastering Fluent Bit: Beginners' Guide for Contributing to Our CNCF Project Website
  • Give Your AI Assistant Long-Term Memory With perag
  • DZone's Article Submission Guidelines
  • Persistent Memory for AI Agents Using LangChain's Deep Agents
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Monitor a Spring Boot App With Ostara

How to Monitor a Spring Boot App With Ostara

In this blog, you will learn how to monitor a Spring Boot application using Ostara. Ostara is a desktop application that monitors and manages your application. Enjoy!

By 
Gunter Rotsaert user avatar
Gunter Rotsaert
DZone Core CORE ·
Dec. 06, 23 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
4.1K Views

Join the DZone community and get the full member experience.

Join For Free

In this blog, you will learn how to monitor a Spring Boot application using Ostara. Ostara is a desktop application that monitors and manages your application. Enjoy!

Introduction

When an application runs in production (but also your other environments), it is wise to monitor its health. You want to make sure that everything is running without any problems, and the only way to know this is to measure the health of your application. When something goes wrong, you hopefully will be notified before your customer notices the problem, and maybe you can solve the problem before your customer notices anything. In a previous post, it was explained how to monitor your application using Spring Actuator, Prometheus, and Grafana. In this post, you will take a look at an alternative approach using Spring Actuator in combination with Ostara. The setup with Ostara is a bit easier; therefore, it looks like a valid alternative. The proof of the pudding is in the eating, so let’s try Ostara!

The sources used in this blog are available on GitHub.

Prerequisites

The prerequisites needed for this blog are:

  • Basic Spring Boot 3 knowledge;
  • Basic Linux knowledge;
  • Java 17 is used.

Create an Application Under Test

First, you need to create an application that you can monitor. Navigate to Spring Initializr and add the Spring Web and Spring Boot Actuator dependencies. Spring Web will be used to create two dummy Rest endpoints, and Spring Boot Actuator will be used to enable the monitor endpoints. See a previous post in order to get more acquainted with Spring Boot Actuator. The post is written for Spring Boot 2, but the contents are still applicable for Spring Boot 3.

Add the git-commit-id-plugin to the pom file in order to be able to generate build information. Also, add the build-info goal to the executions of the spring-boot-maven-plugin in order to generate the information automatically during a build. See a previous post if you want to know more about the git-commit-id-plugin.

XML
 
<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>build-info</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>pl.project13.maven</groupId>
      <artifactId>git-commit-id-plugin</artifactId>
      <version>4.9.10</version>
      <executions>
        <execution>
          <id>get-the-git-infos</id>
          <goals>
            <goal>revision</goal>
          </goals>
        </execution>
      </executions>
 
      <configuration>
        <dotGitDirectory>${project.basedir}/.git</dotGitDirectory>
        <prefix>git</prefix>
        <verbose>false</verbose>
        <generateGitPropertiesFile>true</generateGitPropertiesFile>
        <generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties</generateGitPropertiesFilename>
        <format>properties</format>
        <gitDescribe>
          <skip>false</skip>
          <always>false</always>
          <dirty>-dirty</dirty>
        </gitDescribe>
      </configuration>
 
    </plugin>
  </plugins>
</build>


Enable the full git information to the actuator endpoint in the application.properties.

Properties files
 
management.info.git.mode=full


Add a Rest controller with two dummy endpoints.

Java
 
@RestController
public class MetricsController {
  
    @GetMapping("/endPoint1")
    public String endPoint1() {
        return "Metrics for endPoint1";
    }
  
    @GetMapping("/endPoint2")
    public String endPoint2() {
        return "Metrics for endPoint2";
    }
      
}


Build the application.

Shell
 
$ mvn clean verify


Run the application.

Shell
 
$ java -jar target/myostaraplanet-0.0.1-SNAPSHOT.jar


Verify the endpoints.

Shell
 
$ curl http://localhost:8080/endPoint1
Metrics for endPoint1
$ curl http://localhost:8080/endPoint2
Metrics for endPoint2


Verify the actuator endpoint.

Shell
 
$ curl http://localhost:8080/actuator | python3 -mjson.tool
 ...
{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-path": {
            "href": "http://localhost:8080/actuator/health/{*path}",
            "templated": true
        }
    }
}


Add Security

The basics are in place now. However, it is not very secure. Let’s add authorization to the actuator endpoint. Beware that the setup in this paragraph is not intended for production usage.

Add the Spring Security dependency to the pom.

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


Add the credentials and role to the application.properties file. Again, do not use this for production purposes.

Properties files
 
spring.security.user.name=admin
spring.security.user.password=admin123
spring.security.user.roles=ADMIN


Add a WebSecurity class, which adds the security layer to the actuator endpoint.

Java
 
@Configuration
@EnableWebSecurity
public class WebSecurity {
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(authz -> authz
                .requestMatchers("/actuator/**").hasRole("ADMIN")
                .anyRequest().permitAll())
                .httpBasic(Customizer.withDefaults());
        return http.build();
 
    }
}


Build and start the application. Verify whether the actuator endpoint can be accessed using the credentials as specified.

Shell
 
$ curl http://localhost:8080/actuator -u "admin:admin123" | python3 -mjson.tool
 ...
{
    "_links": {
        "self": {
            "href": "http://localhost:8080/actuator",
            "templated": false
        },
        "health": {
            "href": "http://localhost:8080/actuator/health",
            "templated": false
        },
        "health-path": {
            "href": "http://localhost:8080/actuator/health/{*path}",
            "templated": true
        }
    }
}


Install Ostara

Navigate to the Ostara website and click the Download Ostara button. Choose the platform you are using (Linux 64bit, in my case), and the file Ostara-0.12.0.AppImage is downloaded. Double-click the file, and Ostara is started. That’s all!

install Ostara


Monitor Application

By default, only a limited set of actuator endpoints are enabled. Ostara will function with this limited set, but less information will be visible as a consequence. In order to see the full set of capabilities of Ostara, you enable all actuator endpoints. Again, beware of how much you expose in production.

Properties files
 
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always


Before you continue using Ostara, you are advised to disable sending usage statistics and error information. Navigate to the settings (right top corner), choose Privacy, and disable the tracking options.

Privacy

In the left menu, choose Create Instance and fill in the fields as follows:

  • Actuator URL
  • Alias: MyFirstInstance
  • Application Name: MyFirstApp
  • Disable SSL Verification: Yes (for this demo, no SSL connection is used)
  • Authentication Type: Basic
  • Username and Password: the admin credentials

create instance

Click the Test Connection button. This returns an unauthorized error, which appears to be a bug in Ostara because the credential information is correct.

connection failed

Ignore the error and click the Save button.

Ostara can connect to the application, and the dashboard shows some basic status information.

my first instance

You can explore all the available information for yourself. Some of them are highlighted below.

Info

The Info page shows you the information which you made available with the help of the git-commit-id-plugin.

App Properties

The App Properties page shows you the application properties. However, as you can see in the below screenshot, all values are masked. This is the default Spring Boot 3 behavior.

app properties

This behavior can be changed in  application.properties of the Spring Boot Application. You can choose between always (not recommended), when-authorized or never.

Properties files
 
management.endpoint.configprops.show-values=when-authorized
management.endpoint.env.show-values=when-authorized


Build and start the application again. The values are visible.

application

Metrics

The Metrics page allows you to enable notifications for predefined or custom metrics.

Open the http.server.requests metric and click the Add Metric Notification.

metrics

Fill in the following in order to create a notification when EndPoint1 is invoked more than ten times, and click the Save button:

  • Name: EndPoint 1 invoked > 10 times
  • Type: Simple
  • Tags: /endPoint1
  • Operation: Greater Than
  • Value: 10

add metric notification

Invoke EndPoint1 more than ten times in a row. Wait for a minute, and the notification appears at the top of your main screen.

Loggers

The Loggers page shows you the available loggers, and you are able to change the desired log level. This is an interesting feature when you need to analyze a bug.

loggers

Click the DEBUG button for the com.mydeveloperplanet.myostaraplanet.MetricsController. A message is shown that this operation is forbidden.

The solution is to disable the csrf protection for the actuator endpoints. For more information about csrf attacks, see this blog.

Add the following line to the WebSecurity class.

Java
 
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http.authorizeHttpRequests(authz -> authz
            .requestMatchers("/actuator/**").hasRole("ADMIN")
            .anyRequest().permitAll())
            .csrf(csrf -> csrf
                    .ignoringRequestMatchers("/actuator/**")
            )
            .httpBasic(Customizer.withDefaults());
    return http.build();
}


Also, add some logging statements to the EndPoint1 code in order to verify the result.

Java
 
@RequestMapping("/endPoint1")
public String endPoint1() {
    logger.debug("This is DEBUG message");
    logger.trace("This is a TRACE message");
    return "Metrics for endPoint1";
}


Build and restart the application.

Enable the DEBUG logging again for the MetricsController and invoke EndPoint1. The DEBUG statement is shown in the logs.

Shell
 
2023-09-10T15:06:04.511+02:00 DEBUG 30167 --- [nio-8080-exec-8] c.m.myostaraplanet.MetricsController     : This is DEBUG message


Multiple Instances

When you have multiple instances of your application, you can create another instance to monitor.

Start another instance of the application on port 8081.

Shell
 
$ java -jar -Dserver.port=8081 target/myostaraplanet-0.0.1-SNAPSHOT.jar

Hover over MyFirstApp and click the three dots menu. Choose Add Instance and fill in the following:

  • Actuator URL
  • Alias: MySecondInstance

add instance

Clicking the Test Connection button is successful this time. Click the Save button.

The second instance is added, and the application dashboard view shows the summary information.

summary

Conclusion

Ostara is a good alternative for monitoring Spring Boot applications. The installation only requires you to download a file and start it. Ostara gives you a clear visual view, and the notifications notify you when something is wrong. It is also capable of starting thread profiling on your instance and downloading heap dumps. Compared to Grafana, Grafana has some more fancy graphs than Ostara. However, Ostara is not only a visualization tool; but you can also interact with your application and receive notifications when something goes wrong.

application Spring Boot

Published at DZone with permission of Gunter Rotsaert. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Setting Up Local Kafka Container for Spring Boot Application
  • Spring Boot Application With Spring REST and Spring Data MongoDB
  • Developing Saga Participant Code for Compensating Transactions

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook