OpenShift Monitoring with Prometheus

Learn how monitoring works in OpenShift using Prometheus. Understand Prometheus architecture, Spring Boot Actuator, Micrometer integration, metrics collection, ServiceMonitor, PromQL, AlertManager, and enterprise monitoring best practices.


Introduction

Imagine your Spring Boot application is successfully deployed on OpenShift.

Everything appears healthy, but suddenly users report:

  • Slow API responses
  • High CPU usage
  • Memory leaks
  • Frequent Pod restarts
  • Database connection failures
  • Increased response times

Logs tell what happened.

Metrics tell how the system is behaving.

This is why every enterprise OpenShift platform uses Prometheus to continuously monitor applications and infrastructure.

Prometheus automatically collects metrics from Spring Boot applications and OpenShift components, enabling teams to visualize performance, create dashboards, and trigger alerts before users experience problems.


Learning Objectives

By the end of this article, you will understand:

  • What is Prometheus?
  • Monitoring Architecture
  • Spring Boot Actuator
  • Micrometer Integration
  • Prometheus Metrics
  • ServiceMonitor
  • PromQL
  • AlertManager
  • Enterprise Monitoring Best Practices

Why Monitoring?

Without monitoring:

  • Performance issues go unnoticed.
  • Memory leaks remain hidden.
  • CPU spikes are not detected.
  • Applications fail without alerts.
  • Capacity planning becomes difficult.

Monitoring provides real-time visibility into system health.


Monitoring Architecture

flowchart LR
    A[Spring Boot Application]
    B[Spring Boot Actuator]
    C[Micrometer]
    D[Prometheus]
    E[Grafana]
    F[Operations Team]

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

Metrics Collection Flow

sequenceDiagram
    participant App as Spring Boot
    participant Prom as Prometheus
    participant Graf as Grafana

    Prom->>App: GET /actuator/prometheus
    App-->>Prom: Metrics
    Prom->>Graf: Store Metrics

Prometheus periodically scrapes metrics instead of applications pushing them.


What is Prometheus?

Prometheus is an open-source monitoring and alerting system designed for cloud-native applications.

It:

  • Collects metrics
  • Stores time-series data
  • Executes queries
  • Generates alerts
  • Integrates with Grafana

Prometheus Architecture

flowchart LR
    A[Spring Boot Pods]
    B[ServiceMonitor]
    C[Prometheus Server]
    D[Time Series Database]
    E[Grafana]

    A --> B
    B --> C
    C --> D
    D --> E

Spring Boot Actuator

Spring Boot exposes operational endpoints through Spring Boot Actuator.

Dependency

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

Micrometer Dependency

Micrometer exposes metrics in Prometheus format.

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

Actuator Configuration

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

management.endpoint.health.show-details=always

management.metrics.export.prometheus.enabled=true

Metrics Endpoint

Prometheus scrapes

/actuator/prometheus

Example

http://payment-api:8080/actuator/prometheus

Monitoring Flow

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

    SPRING --> ENDPOINT
    ENDPOINT --> PROM
    PROM --> GRAFANA

Common JVM Metrics

Micrometer automatically exposes:

Metric Description
JVM Heap Heap Memory
JVM Threads Thread Count
CPU Usage Process CPU
GC Time Garbage Collection
HTTP Requests API Metrics
Response Time Latency
Disk Usage Storage
Uptime Application Runtime

Sample Metrics

jvm_memory_used_bytes

jvm_threads_live

http_server_requests_seconds_count

system_cpu_usage

ServiceMonitor

Prometheus discovers applications using 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

Scraping Process

flowchart LR
    PROM["Prometheus"]
    ENDPOINT["/actuator/prometheus"]
    METRICS["Metrics Database"]

    PROM --> ENDPOINT
    ENDPOINT --> METRICS

PromQL

Prometheus uses PromQL for querying metrics.

Examples

CPU Usage

process_cpu_usage

JVM Heap

jvm_memory_used_bytes

HTTP Requests

http_server_requests_seconds_count

Spring Boot Request Flow

flowchart LR
    A[Client]
    B[Spring Boot]
    C[Micrometer]
    D[Prometheus]
    E[Grafana]

    A --> B
    B --> C
    C --> D
    D --> E

Custom Metrics

Spring Boot allows custom business metrics.

@Autowired
MeterRegistry registry;

Counter paymentCounter =
registry.counter("payments.completed");

paymentCounter.increment();

Prometheus now tracks completed payments.


Banking Example

Monitor:

  • Payments Completed
  • Failed Transactions
  • Average Response Time
  • Database Calls
  • Kafka Messages
  • Active Users

Enterprise Monitoring Architecture

flowchart TD
    A[Payment Service]
    B[Customer Service]
    C[Loan Service]

    D[Prometheus]

    E[AlertManager]

    F[Grafana]

    G[Operations Team]

    A --> D
    B --> D
    C --> D

    D --> E
    D --> F

    E --> G

AlertManager

Prometheus detects problems.

AlertManager sends notifications.

Examples:

  • Email
  • Slack
  • Microsoft Teams
  • PagerDuty
  • Webhook

Alert Flow

flowchart LR
    A[Metric Threshold Exceeded]
    B[Prometheus]
    C[AlertManager]
    D[Email]
    E[Slack]
    F[Teams]

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

Example Alerts

Alert Threshold
CPU Usage >80%
Memory Usage >85%
Pod Restarts >3
JVM Heap >90%
Response Time >2 Seconds
Error Rate >5%

OpenShift Commands

View Pods

oc get pods

View ServiceMonitor

oc get servicemonitor

Describe ServiceMonitor

oc describe servicemonitor payment-service

View Routes

oc get route

Common Problems

Metrics Not Appearing

Verify:

  • Actuator dependency
  • Micrometer dependency
  • /actuator/prometheus
  • ServiceMonitor labels

Prometheus Cannot Scrape

Check:

  • Service exists
  • Port name matches
  • Endpoint path
  • NetworkPolicy

Empty Dashboard

Possible causes:

  • Wrong PromQL
  • Incorrect metric names
  • ServiceMonitor missing

Missing JVM Metrics

Verify:

management.metrics.export.prometheus.enabled=true

Production Best Practices

  • Enable Spring Boot Actuator.
  • Use Micrometer.
  • Expose only required endpoints.
  • Monitor JVM metrics.
  • Create custom business metrics.
  • Configure AlertManager.
  • Secure monitoring endpoints.
  • Use Grafana dashboards.
  • Monitor infrastructure and application metrics together.
  • Review alerts regularly.

Common Mistakes

❌ Exposing every Actuator endpoint publicly.

❌ Ignoring JVM memory metrics.

❌ Creating too many custom metrics.

❌ Forgetting ServiceMonitor configuration.

❌ Monitoring only infrastructure.

❌ No alert thresholds.


Advantages

  • Real-time monitoring
  • Historical metrics
  • Capacity planning
  • Performance analysis
  • Automated alerting
  • Cloud-native monitoring
  • Enterprise observability
  • Easy Grafana integration

Summary

Prometheus is the standard monitoring solution for OpenShift and Spring Boot applications.

Key takeaways:

  • Spring Boot Actuator and Micrometer expose application metrics.
  • Prometheus scrapes metrics from /actuator/prometheus.
  • ServiceMonitor enables automatic target discovery.
  • PromQL allows powerful querying of time-series data.
  • AlertManager sends notifications when thresholds are exceeded.
  • Combining Prometheus with Grafana provides comprehensive observability for enterprise applications.

Interview Questions

  1. What is Prometheus?
  2. How does Prometheus collect metrics?
  3. What is Spring Boot Actuator?
  4. Why is Micrometer required?
  5. What is a ServiceMonitor?
  6. What is PromQL?
  7. How does AlertManager work?
  8. What are the most important JVM metrics?
  9. How do you create custom metrics in Spring Boot?
  10. What are the best practices for monitoring applications on OpenShift?