Observability Concept with Spring Boot, Prometheus, and JSON Logging
Observability refers to a system’s ability to provide insights into its internal state by exposing external outputs. It is typically built on three pillars: Logging, Monitoring, and Alerting.
1. 🔍 Logging
Logging captures structured or unstructured information about system events. In a production-ready setup, logs are written in JSON format to facilitate automated processing and analysis.
1.1. Technology: Spring Boot + JSON Logging
-
Use a logging framework such as
LogbackorLog4j2with a JSON encoder. -
Example configuration using
Logbackinlogback-spring.xml:
<configuration>
<appender name="JSON_CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder" />
</appender>
<root level="INFO">
<appender-ref ref="JSON_CONSOLE" />
</root>
</configuration>
-
Example log output:
{
"timestamp": "2025-04-13T12:34:56.789Z",
"level": "INFO",
"logger": "com.example.MyService",
"message": "User successfully created",
"userId": "12345"
}
2. 📊 Monitoring
Monitoring collects metrics (e.g. latency, memory usage, throughput) to continuously observe system performance and health.
2.1. Technology: Spring Boot Actuator + Prometheus
-
spring-boot-actuatorexposes metrics via the/actuator/metricsendpoint. -
Integration with
micrometerenables Prometheus as a metrics backend.
2.2. Example configuration (application.yml)
management:
endpoints:
web:
exposure:
include: prometheus
metrics:
export:
prometheus:
enabled: true
-
Prometheus is configured with a
scrapejob to periodically collect metrics:
scrape_configs:
- job_name: 'spring-boot-app'
static_configs:
- targets: ['localhost:8080']
3. 🚨 Alerting
Alerting generates notifications based on defined thresholds in metrics, enabling rapid responses to potential issues.
3.1. Technology: Prometheus + Alertmanager
-
Prometheus evaluates metrics and triggers alerts according to alerting rules.
-
Example permission (Prometheus):
groups:
- name: spring-alerts
rules:
- alert: HighErrorRate
expr: rate(http_server_requests_seconds_count{status=~"5.."}[1m]) > 5
for: 1m
labels:
severity: warning
annotations:
summary: "High error rate detected"
description: "More than 5 errors per second in the last 60 seconds"
-
Alertmanager handles routing and escalation (e.g. email, Slack).
4. ✅ Summary
The observability setup is composed of the following components:
| Component | Technology |
|---|---|
Logging |
Spring Boot + Logback + JSON |
Monitoring |
Spring Boot Actuator + Micrometer + Prometheus |
Alerting |
Prometheus Alert Rules + Alertmanager |
This setup provides deep visibility into the system’s behavior and ensures quick incident response.