Skip to content

Enable Statistics in Springboot

Rohith Kunnath Puthan Veedu edited this page Feb 2, 2021 · 3 revisions

bootstrap.yml

min-hibernate-stats:
  loggingEnabled: true
  metricPublishingEnabled: true
  noOfRowsInMin: 10
  minExecutionTimeInMillis: 250
  createInsertListeners: true
  createUpdateListeners: true
  createDeleteListeners: true
  insertSamples: 10
  updateSamples: 10
  deleteSamples: 10

PropertiesFile.java

@ConfigurationProperties("min-hibernate-stats")
@Configuration
public class HibernateStatsProperties {

    private boolean loggingEnabled = true;
    private boolean metricPublishingEnabled = true;
    private Integer noOfRowsInMin = 10;
    private Integer minExecutionTimeInMillis = 250;
    private boolean createInsertListeners = true;
    private boolean createUpdateListeners = true;
    private boolean createDeleteListeners = true;
    private Integer insertSamples = 10;
    private Integer updateSamples = 10;
    private Integer deleteSamples = 10;
}

SpringBootApplication.class

@SpringBootApplication
public class App extends ResourceServerConfigurerAdapter {
   
  @Autowired
  private HibernateStatsProperties hibernateStatsProperties;
  

  @Autowired
  public void setInfoProperties(ConfigurableEnvironment env) {
    System.getProperties().put("min.hibernate.stats.query.logging.min.rows", hibernateStatsProperties.getNoOfRowsInMin());
    System.getProperties().put("min.hibernate.stats.query.logging.min.executionTime.millis", hibernateStatsProperties.getMinExecutionTimeInMillis());
    System.getProperties().put("min.hibernate.stats.metrics.publish", hibernateStatsProperties.isMetricPublishingEnabled());
    System.getProperties().put("min.hibernate.stats.logging.enabled", hibernateStatsProperties.isLoggingEnabled());
 
    System.getProperties().put("hibernate.stats.factory", new MinimalStatisticsFactory());
  }
}

Enable Transactional Metrics

@Component
public class HibernatePropsCustomizer implements HibernatePropertiesCustomizer {

    private final HibernateStatsProperties hibernateStatsProperties;

    public HibernatePropsCustomizer(HibernateStatsProperties hibernateStatsProperties) {
        this.hibernateStatsProperties = hibernateStatsProperties;
    }

    @Override
    public void customize(Map<String, Object> hibernateProperties) {
        //Hibernate logging configs
        hibernateProperties.put("min.hibernate.stats.logging.updates.samples", hibernateStatsProperties.getUpdateSamples());
        hibernateProperties.put("min.hibernate.stats.logging.deletes.samples", hibernateStatsProperties.getInsertSamples());
        hibernateProperties.put("min.hibernate.stats.logging.inserts.samples", hibernateStatsProperties.getDeleteSamples());

        hibernateProperties.put("min.hibernate.stats.logging.updates.enabled", hibernateStatsProperties.isCreateUpdateListeners());
        hibernateProperties.put("min.hibernate.stats.logging.deletes.enabled", hibernateStatsProperties.isCreateDeleteListeners());
        hibernateProperties.put("min.hibernate.stats.logging.inserts.enabled", hibernateStatsProperties.isCreateInsertListeners());

        hibernateProperties.put("hibernate.integrator_provider", new ListenerIntegrator(hibernateProperties));
    }
}

Note

Since the Internal object creation of hibernate only grabs the metadata from global properties and discards if you add it directly to the yml or property files.

Clone this wiki locally