Library to enable minimal statistical recording of database queries.
Currently, it is impossible to only log or record the query and the time taken to execute them. Default implementation from hibernate is costly in terms of,
- Does a lot of things without asking what does the user need.
- Store a lot of data in the heap, some of them are even strong references.
- Tightly coupled and not possible to extend.
- Usage of Hibernate-sessions can be seen everywhere.
Instead this library helps us to achieve,
- Activate only the metrics which are needed.
- Enable or disable logging of Statistics type (query time, row fetch count).
- Enable or disable storing and reporting of Statistics via Prometheus or similar time series database.
- Easy customizations using system properties.
- Injectable even on non-hibernate or non-spring projects by little tweaks.
- Create alerts to notify about queries that needs to be improved.
- Manually looking at the Database Reports on long running queries or running "Explain" command in the database.
- Log sql queries with time taken to execute.
- Prometheus alerts for queries taking more time than expected.
- Prometheus alerts for rows being fetched for select queries.
- Record querys only if it takes time more than expected. Eg: Log queries if the execution time is more than 250ms.
- Record insert, update and delete operations of entities if commit time is taking more than 250ms.
- Only report to prometheus or log if the query takes more than 1 sec (Can avoid Thread pool starvations).
- Only report to prometheus or log if the number of rows fetched is more than 100 (Can avoid wasting resources for in-memory loading).
- Only report to prometheus or log if the Transactions are taking more than 2 sec (Possiblity to double check indexes being updated/created are needed or not).
Dependency available here
Configurable Properties.
- "min.hibernate.stats.logging.enabled" - True/False (Enable logging)
- "min.hibernate.stats.metrics.publish" - True/False (Enable storing in-memory and enabling it to be available via MetricsPublisher class)
- "min.hibernate.stats.loggers" - QUERY_EXECUTION_TIME (Enables logging of query and the execution time)
- "min.hibernate.stats.query.logging.min.executionTime.millis" - 250 (Minimum Milliseconds needed to report the metric).
- "min.hibernate.stats.query.logging.min.rows" - 20 (Minimum number of rows fetched to report the metric).
This property helps to instantiate our library. "hibernate.stats.factory", new MinimalStatisticsFactory());
Instantiate this "new MinimalStatisticsFactory()" and makes use of Classes in "com.rohith.hibernateminimallogger.loggers" package.
Time taken for INSERTS, DELETES and UPDATES could also be recorded.
Configurable Properties.
- "min.hibernate.stats.logging.updates.enabled" - Update listener is enabled, so time taken for updates are recorded.
- "min.hibernate.stats.logging.deletes.enabled" - Delete listener is enabled, so time taken for deletes are recorded.
- "min.hibernate.stats.logging.inserts.enabled" - Insert listener is enabled, so time taken for inserts are recorded.
"min.hibernate.stats.logging.updates.samples" "min.hibernate.stats.logging.inserts.samples" "min.hibernate.stats.logging.deletes.samples"
=> Limit how many samples could be collected to compare instead of recording every transaction and wasting the resources.
This will then run comparisons on those collected samples based on each thread id.
However, once the metric is reported to prometheus via MetricPublisher, the collected data is cleared and starts collecting samples again.
This property helps to instantiate our library. "hibernate.integrator_provider", new ListenerIntegrator(hibernateProperties)
- Enable saving non-transactional queries related stats by enabling "min.hibernate.stats.metrics.publish" property.
- Enable saving transactional queries related stats by enabling "min.hibernate.stats.logging.*" properties.
- And finally, scrape information from methods here based on necessity.
- Latest hibernate versions may need changes in this file. Since it implements the hibernate provided Interface.