Exposing Hibernate metrics via Spring Boot Actuator

Viewed 2178

I was doing some research on how to expose Hibernate metrics via Prometheus via Spring Boot Actuator, but I was struggling to find out how to do that.

I added the following property to my Spring Boot: Application:spring.jpa.properties.hibernate.generate_statistics=true

But all that did was add logging from Hibernate. I found these two PRs from the Spring Boot project:

but it looks like publishing Hibernate metrics via Spring Boot Actuator never made it in because of Micrometer adoption.

Is there a way to expose the metrics that the Hibernate EntityManagerFactory collects via Spring Boot Actuator?

1 Answers

Currently it is supported by Spring Boot out of the box.

Besides the property, dependency org.hibernate:hibernate-micrometer has to be on classpath. Compare Spring Actuator documentation.

Previously you could create a HibernateQueryMetrics bean as described in this micrometer issue.

@Bean
MeterBinder hibernateQueryMetrics(EntityManagerFactory entityManager) {
    return new HibernateQueryMetrics(entityManager.unwrap(SessionFactoryImplementor.class), "hibernateSession", Tags.empty());
}

Support for hibernate metrics has been moved from micrometer to hibernate project.

Related