Spring Boot Admin Prometheus Service Discovery
Tags: java, spring, prometheus, servicediscovery
Published: 2023-03-26 16:47:04

[SBAPSD](https://github.com/alexey-lapin/spring-boot-admin-prometheus-service-discovery) - expose your Spring Boot Admin registered applications for Prometheus monitoring.

## Introduction
Monitoring your applications is crucial for maintaining a healthy system. **Spring Boot Admin** is a popular tool for managing and monitoring Spring Boot applications, providing a central dashboard to visualize application status and metrics. On the other hand, **Prometheus** is a powerful open-source monitoring and alerting toolkit that has become the go-to solution for many developers. When you're using Spring Boot Admin to manage your applications and Prometheus as your monitoring solution, it's essential to find an efficient way to expose application instances to Prometheus. That's where **SBAPSD** comes in. This new Java library allows you to expose application instances registered in Spring Boot Admin in Prometheus HTTP service discovery format, making it easier to monitor your applications without any additional manual configuration or setup.

✔ SBAPSD is the perfect solution if you already have a setup with Spring Boot-based applications registered in Spring Boot Admin and want to introduce a monitoring stack based on Prometheus without manually adding each instance to the scrape configuration or setting up any other service discovery tool.

## Getting Started with SBAPSD

SBAPSD can be used in two ways: as a **standalone application** or as a **library** integrated into your existing Spring Boot Admin server application or any other app. Here's how to get started with both use cases.

### Using SBAPSD as a Library
1️⃣ Add the `sbapsd-server` dependency to your project:

For Gradle:
```kotlin
implementation("com.github.alexey-lapin.sbapsd:sbapsd-server:latest")
```

For Maven:
```xml
<dependency>
    <groupId>com.github.alexey-lapin.sbapsd</groupId>
    <artifactId>sbapsd-server</artifactId>
    <version>latest</version>
</dependency>
```

Make sure you have the Spring Web stack on your classpath, such as **spring-boot-starter-webflux** or **spring-boot-starter-web**. It is not necessary when **spring-boot-admin-starter-server** already added.

2️⃣ Enable the service discovery feature by applying the `@EnableAdminServerServiceDiscovery` annotation to your main class:

```java
@SpringBootApplication
@EnableAdminServer
@EnableAdminServerServiceDiscovery
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

}
```

### Using SBAPSD as a Standalone Application
1️⃣ Download the latest standalone JAR from the [releases page](https://github.com/alexey-lapin/spring-boot-admin-prometheus-service-discovery/releases/latest):

- v2 is based on Spring Boot 2 and requires Java 8
- v3 is based on Spring Boot 3 and requires Java 17

2️⃣ Run the JAR file:
```sh
java -jar sbapsd-standalone-v2-latest.jar
```

or

```sh
java -jar sbapsd-standalone-v3-latest.jar
```

Standalone app is also available as **GraalVM** native binaries for Linux and Windows.

### Configuring SBAPSD
Whether you're using SBAPSD as a library or a standalone application, you'll need to configure it by adding properties to your `application.yml` file:

```yml
sbapsd:
  providers:
    server-1:
      type: web
      params:
        url: http://localhost:8092/instances # SBA v2/v3
      filters: # optional
        - type: app-name
          params:
            value: app-.*
        - type: status
          params:
            value: UP,DOWN
      labels: # optional
        static-label-1: value-1
```

Finally, configure **Prometheus** to use the provided endpoints for service discovery:

```yml
scrape_configs:
  - job_name: "spring"
    http_sd_configs:
      - url: http://localhost:8080/service-discovery/prometheus/server-1
    relabel_configs:
      - source_labels: [ __meta_discovery_actuator_path ]
        target_label: __metrics_path__
        replacement: $1/prometheus
      - source_labels: [ __meta_discovery_app_name ]
        target_label: app
```

## Conclusion
With [SBAPSD](https://github.com/alexey-lapin/spring-boot-admin-prometheus-service-discovery), integrating your Spring Boot Admin registered applications with Prometheus monitoring is super easy. The library simplifies the process, allowing you to efficiently monitor your applications without the need for manual configuration or additional service discovery tools. Give **SBAPSD** a try and streamline your application monitoring setup!