Spring Boot Metrics on OpenShift

Learn how Spring Boot metrics work on OpenShift using Actuator and Micrometer. Understand JVM metrics, HTTP metrics, custom business metrics, Prometheus integration, ServiceMonitor configuration, and enterprise monitoring best practices.


Introduction

Modern enterprise applications require more than logs to operate reliably.

While logs help answer what happened, metrics help answer:

  • Is the application healthy?
  • How many requests are processed every second?
  • Is memory usage increasing?
  • How many active users are connected?
  • Which API is slow?
  • Is the JVM approaching OutOfMemoryError?
  • Are Kafka consumers keeping up?
  • Is the database connection pool exhausted?

Metrics provide continuous visibility into application health and performance.

Spring Boot integrates seamlessly with Micrometer, exposing metrics through Spring Boot Actuator. OpenShift collects these metrics using Prometheus, enabling real-time dashboards, alerts, and capacity planning.


Learning Objectives

By the end of this article, you will understand:

  • Why application metrics are important
  • Spring Boot Actuator
  • Micrometer Architecture
  • Prometheus Integration
  • JVM Metrics
  • HTTP Metrics
  • Database Metrics
  • Custom Business Metrics
  • ServiceMonitor
  • Enterprise Monitoring Best Practices

Why Metrics?

Imagine your payment application receives 50,000 requests per minute.

Users complain that payments are slow.

Logs show no errors.

Without metrics, you cannot determine whether the problem is:

  • High CPU usage
  • Memory pressure
  • Long Garbage Collection pauses
  • Database connection pool exhaustion
  • Slow external APIs
  • Thread starvation
  • Kafka consumer lag

Metrics provide quantitative insights that complement logs.


Spring Boot Metrics Architecture

flowchart LR
    APP["Spring Boot Application"]
    ACT["Spring Boot Actuator"]
    MICRO["Micrometer"]
    ENDPOINT["/actuator/prometheus"]
    PROM["Prometheus"]
    GRAF["Grafana"]

    APP --> ACT
    ACT --> MICRO
    MICRO --> ENDPOINT
    ENDPOINT --> PROM
    PROM --> GRAF

Metrics Collection Flow

sequenceDiagram
    participant Client
    participant SpringBoot
    participant Micrometer
    participant Prometheus
    participant Grafana

    Client->>SpringBoot: REST Request
    SpringBoot->>Micrometer: Record Metrics
    Prometheus->>SpringBoot: Scrape /actuator/prometheus
    SpringBoot-->>Prometheus: Metrics
    Prometheus->>Grafana: Store & Visualize

What is Micrometer?

Micrometer is the metrics facade used by Spring Boot.

It works similarly to how SLF4J works for logging.

Instead of binding directly to a monitoring system, Micrometer provides a common API that can export metrics to:

  • Prometheus
  • Datadog
  • Dynatrace
  • New Relic
  • CloudWatch
  • Azure Monitor
  • Wavefront

This allows the same application to support multiple monitoring platforms without code changes.


Monitoring Stack

Component Responsibility
Spring Boot Business Logic
Actuator Exposes Metrics
Micrometer Records Metrics
Prometheus Collects Metrics
Grafana Dashboards
AlertManager Notifications

Add Dependencies

Spring Boot Actuator

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

Prometheus Registry

<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

Configure Actuator

management.endpoints.web.exposure.include=health,info,prometheus

management.endpoint.health.show-details=always

management.metrics.export.prometheus.enabled=true

Metrics Endpoint

After starting the application, metrics are available at:

http://localhost:8080/actuator/prometheus

Metrics Flow

flowchart LR
    SPRING["Spring Boot"]
    ACTUATOR["/actuator/prometheus"]
    PROM["Prometheus"]
    GRAFANA["Grafana Dashboard"]

    SPRING --> ACTUATOR
    ACTUATOR --> PROM
    PROM --> GRAFANA

JVM Metrics

Micrometer automatically exposes JVM metrics.

Metric Description
jvm_memory_used_bytes Heap usage
jvm_memory_max_bytes Maximum heap
jvm_threads_live Active threads
jvm_gc_pause_seconds Garbage Collection pause
process_cpu_usage CPU utilization
system_cpu_usage Node CPU
process_uptime_seconds Application uptime

These metrics help identify JVM performance bottlenecks before they impact users.


HTTP Metrics

Micrometer automatically records HTTP request metrics.

Examples include:

  • Request count
  • Response time
  • Error count
  • HTTP status codes
  • Request duration
  • Active requests

Example metric:

http_server_requests_seconds_count

Database Metrics

If your application uses HikariCP, Micrometer automatically exposes connection pool metrics.

Examples:

  • Active Connections
  • Idle Connections
  • Maximum Pool Size
  • Connection Timeout Count
  • Connection Acquire Time

Monitoring these metrics helps prevent connection pool exhaustion during peak traffic.


ServiceMonitor

OpenShift Prometheus discovers Spring Boot services using a ServiceMonitor.

apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor

metadata:
  name: payment-service

spec:
  selector:
    matchLabels:
      app: payment-service

  endpoints:
    - port: http
      path: /actuator/prometheus

ServiceMonitor Architecture

flowchart LR
    A[Spring Boot Service]
    B[ServiceMonitor]
    C[Prometheus]

    A --> B
    B --> C

Custom Business Metrics

Technical metrics alone are not enough.

Business metrics provide insight into application behavior.

Examples:

  • Payments Processed
  • Orders Created
  • Login Success Rate
  • Failed Transactions
  • Loans Approved
  • Refund Requests
  • Kafka Messages Processed

These metrics allow operations teams to monitor business health alongside infrastructure health.


Custom Counter Example

@Autowired
private MeterRegistry meterRegistry;

private final Counter paymentCounter;

public PaymentService(MeterRegistry meterRegistry) {
    this.paymentCounter =
        meterRegistry.counter("payments.completed");
}

public void processPayment() {
    paymentCounter.increment();
}

This creates a Prometheus metric named:

payments_completed_total

Enterprise Banking Architecture

flowchart LR
    A[Payment Service]
    B[Customer Service]
    C[Loan Service]
    D[Micrometer]
    E[Prometheus]
    F[Grafana]
    G[Operations Team]

    A --> D
    B --> D
    C --> D
    D --> E
    E --> F
    F --> G

Summary

Spring Boot metrics provide real-time visibility into application health and performance.

Key takeaways:

  • Spring Boot Actuator exposes operational endpoints.
  • Micrometer collects JVM, HTTP, database, and custom business metrics.
  • Prometheus periodically scrapes metrics from /actuator/prometheus.
  • ServiceMonitor enables automatic discovery in OpenShift.
  • Grafana visualizes metrics through interactive dashboards.
  • Combining metrics with centralized logging provides comprehensive observability for enterprise Spring Boot applications.

Interview Questions

  1. What is Micrometer?
  2. Why is Spring Boot Actuator required?
  3. How does Prometheus collect metrics?
  4. What is a ServiceMonitor?
  5. Which JVM metrics are most important in production?
  6. How do you create custom business metrics?
  7. What is the difference between logs and metrics?
  8. How does Micrometer integrate with Prometheus?
  9. Why should you monitor HikariCP metrics?
  10. What are the production best practices for Spring Boot metrics?