#1
This guide shows how to connect ClickHouse with Grafana in a Spring Boot app to visualize metrics and query results.

1. Enable ClickHouse for Monitoring

Ensure ClickHouse is running and exposes metrics:
SELECT * FROM system.metrics LIMIT 5;
This confirms the database is ready for visualization.

2. Add REST Endpoints in Spring Boot

Expose data from ClickHouse to be consumed by Grafana:
@RestController
@RequestMapping("/analytics")
public class AnalyticsController {

    private final JdbcTemplate jdbcTemplate;

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

    @GetMapping("/sales")
    public List<Map<String, Object>> getSales() {
        return jdbcTemplate.queryForList(
            "SELECT toDate(order_date) as day, sum(amount) as total FROM sales GROUP BY day ORDER BY day"
        );
    }
}

3. Connect Grafana to ClickHouse

  • In Grafana, add a new data source.
  • Choose ClickHouse.
  • Provide JDBC or HTTP connection details.

4. Build Dashboards

Use SQL queries directly in Grafana panels, e.g.:
SELECT toDate(event_time) as day, count(*) as requests
FROM system.query_log
GROUP BY day
ORDER BY day;
Visualize metrics like sales trends, query counts, or error rates.

5. Combine with Spring Boot APIs

Grafana can pull directly from ClickHouse or from Spring Boot REST endpoints, giving flexibility in shaping the data.

image quote pre code