Skip to content

Integrate to Prometheus Spring Java

Rohith Kunnath Puthan Veedu edited this page Feb 2, 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());

       MetricPublisher.INSTANCE.executionTimeForInserts(200)
                .forEach((entity, count) -> new MicrometerMetricsCaptor(meterRegistry)
                        .gaugeBuilder("sql_inserts", count, value -> count.intValue())
                        .tag("entity", entity)
                        .build());

        MetricPublisher.INSTANCE.executionTimeForUpdates(200)
                .forEach((entity, count) -> new MicrometerMetricsCaptor(meterRegistry)
                        .gaugeBuilder("sql_updates", count, value -> count.intValue())
                        .tag("entity", entity)
                        .build());

        MetricPublisher.INSTANCE.executionTimeForDeletes(200)
                .forEach((entity, count) -> new MicrometerMetricsCaptor(meterRegistry)
                        .gaugeBuilder("sql_deletes", count, value -> count.intValue())
                        .tag("entity", entity)
                        .build());
    }
}

NOTE

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

Clone this wiki locally