Skip to content

Integrate to Prometheus Spring Java

Rohith Kunnath Puthan Veedu edited this page Jan 27, 2021 · 3 revisions

Configuration Class

@Configuration
@EnableScheduling
public class HibernateMetricsConfig {

    private final MeterRegistry meterRegistry;

    public HibernateMetricsConfig(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
    }


    @Scheduled(cron = "*/50 * * * * *")
    public void updateMetric() {
        MetricPublisher.INSTANCE.queryWithMaxRows()
                .forEach((query, count) -> {
                    new MicrometerMetricsCaptor(meterRegistry)
                            .gaugeBuilder("sql_fetch_rows", count, value -> count.intValue())
                            .tag("sql", query)
                            .build();
                });

        MetricPublisher.INSTANCE.queryWithMaxExecutionTime()
                .forEach((query, count) -> {
                    new MicrometerMetricsCaptor(meterRegistry)
                            .gaugeBuilder("sql_fetch_time", count, value -> count.intValue())
                            .tag("sql", query)
                            .build();
                });
    }
}

NOTE

The data is added to meter registry every 50 seconds and this will be later available in prometheus.

Clone this wiki locally