#1
This guide shows how to monitor ClickHouse metrics in a Spring Boot application for better visibility and performance.

1. Expose ClickHouse System Tables

ClickHouse provides monitoring data via system.* tables:
SELECT * FROM system.metrics LIMIT 10;
SELECT * FROM system.parts LIMIT 10;
SELECT * FROM system.query_log ORDER BY event_time DESC LIMIT 5;

2. Query Metrics with Spring Boot

Use JdbcTemplate to fetch metrics directly:
List<Map<String, Object>> metrics = jdbcTemplate.queryForList(
    "SELECT metric, value FROM system.metrics"
);
You can map them to DTOs and expose through REST.

3. Create Monitoring Endpoints

Add a controller in Spring Boot:
@RestController
@RequestMapping("/monitor")
public class MonitoringController {

    private final JdbcTemplate jdbcTemplate;

    public MonitoringController(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @GetMapping("/metrics")
    public List<Map<String, Object>> getMetrics() {
        return jdbcTemplate.queryForList("SELECT metric, value FROM system.metrics");
    }
}

4. Integrate with Actuator

Enable Spring Boot Actuator and expose custom metrics through Micrometer. You can push them to Prometheus and visualize with Grafana.

5. Track Query Performance

Monitor system.query_log for slow queries:
SELECT query, query_duration_ms 
FROM system.query_log 
WHERE type='QueryFinish' 
ORDER BY query_duration_ms DESC 
LIMIT 5;

image quote pre code