Class HibernateStatisticsCollector


  • public class HibernateStatisticsCollector
    extends io.prometheus.client.Collector
    Collect metrics from one or more Hibernate SessionFactory instances.

    Usage example for a single session factory:

     new HibernateStatisticsCollector(sessionFactory, "myapp").register();
     
    Usage example for multiple session factories:
     new HibernateStatisticsCollector()
         .add(sessionFactory1, "myapp1")
         .add(sessionFactory2, "myapp2")
         .register();
     
    If you are in a JPA environment, you can obtain the SessionFactory like this:
     SessionFactory sessionFactory =
         entityManagerFactory.unwrap(SessionFactory.class);
     

    When enablePerQueryMetrics() has been called, certain metrics like execution time are collected per query. This may create a lot of monitoring data, so it should be used with caution.

    Author:
    Christian Kaltepoth
    • Nested Class Summary

      • Nested classes/interfaces inherited from class io.prometheus.client.Collector

        io.prometheus.client.Collector.Describable, io.prometheus.client.Collector.MetricFamilySamples, io.prometheus.client.Collector.Type
    • Field Summary

      • Fields inherited from class io.prometheus.client.Collector

        MILLISECONDS_PER_SECOND, NANOSECONDS_PER_SECOND
    • Constructor Detail

      • HibernateStatisticsCollector

        public HibernateStatisticsCollector​(org.hibernate.SessionFactory sessionFactory,
                                            String name)
        Creates a new collector for the given session factory. Calling this constructor has the same effect as creating an empty collector and adding the session factory using add(SessionFactory, String).
        Parameters:
        sessionFactory - The Hibernate SessionFactory to collect metrics for
        name - A unique name for this SessionFactory
    • Method Detail

      • add

        public HibernateStatisticsCollector add​(org.hibernate.SessionFactory sessionFactory,
                                                String name)
        Registers a Hibernate SessionFactory with this collector.
        Parameters:
        sessionFactory - The Hibernate SessionFactory to collect metrics for
        name - A unique name for this SessionFactory
        Returns:
        Returns the collector
      • enablePerQueryMetrics

        public HibernateStatisticsCollector enablePerQueryMetrics()
        Enables collection of per-query metrics. Produces a lot of monitoring data, so use with caution.

        Per-query metrics have a label "query" with the actual HQL query as value. The query will contain placeholders ("?") instead of the real parameter values (example: select u from User u where id=?).

        Returns:
        Returns the collector
      • collect

        public List<io.prometheus.client.Collector.MetricFamilySamples> collect()
        Specified by:
        collect in class io.prometheus.client.Collector
      • register

        public <T extends io.prometheus.client.Collector> T register​(io.prometheus.client.CollectorRegistry registry)
        Overrides:
        register in class io.prometheus.client.Collector