Video in progress
Which metrics and logs do you make mandatory for watching the Hibernate layer on prod?
Query duration and the number of queries per request, connection pool state (active, pending, acquire time), the slow query log, L2 cache hit/miss, and the count of optimistic lock failures. Hibernate statistics are turned on carefully on prod, through Micrometer.
📖 Answer
I look at them in three layers.
Connection pool (Hikari, automatic through Micrometer): hikaricp.connections.active, pending, acquire time, the timeout count. Half of prod problems show up here first.
The SQL level. Hibernate 6 has a dedicated log for slow queries:
spring:
jpa:
properties:
hibernate.log_slow_query: 300 # ms, Hibernate 6.2+
logging:
level:
org.hibernate.SQL_SLOW: info
On top of that, through datasource-proxy or p6spy I add the SQL count and total time for each HTTP request to tracing as a span attribute. Then in Grafana the question "which endpoint is making 200 queries" gets answered in a minute.
Hibernate statistics. hibernate.generate_statistics=true + HibernateMetrics from the hibernate-micrometer module: L2 cache hit/miss, entity loads, flushes, optimistic failure count. Collecting statistics has overhead; I turn it on in prod, but I measure the difference in a load test.
At the business level, counters for OptimisticLockException, deadlocks and LazyInitializationException. If they climb, it is a sign that the code or the load has changed.
No show_sql=true on prod: it writes to stdout, without parameters, and you can't filter it.
Sign up to mark studied questions and save your progress.