Add JPA statistics support for eclipselink and hibernate (#2202)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-01-20 13:17:55 +02:00
committed by GitHub
parent 357c81fbf4
commit 1f71d6ddb0
10 changed files with 436 additions and 12 deletions

View File

@@ -36,6 +36,12 @@
<artifactId>hibernate-core</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
<optional>true</optional>
</dependency>
<!-- Static class generation -->
<dependency>
<groupId>org.hibernate.orm</groupId>

View File

@@ -0,0 +1,77 @@
/**
* Copyright (c) 2025 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.repository.jpa;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* (Experimental) Report Hibernate statistics to Micrometer.
* <p/>
* To be enabled:
* <ol>
* <li>The Spring property spring.jpa.properties.hibernate.generate_statistics=true shall be set - enables Hibernate statistics
* collecting</li>
* <li>If don't need log in the stdout set logging.level.org.hibernate.engine.internal.StatisticalLoggingSessionEventListener=WARN</li>
* <li>The MeterRegistry shall be registered available - e.g. include org.springframework.boot:spring-boot-actuator-autoconfigure</li>
* <li>Hibernate reporting to micrometer shall be enabled - include org.hibernate.orm:hibernate-micrometer</li>
* <li>(?) When using in test the metrics MAYBE shall be enabled with @AutoConfigureObservability(tracing = false)</li>
* </ol>
*/
public class Statistics {
public static final String METER_PREFIX = "hibernate.";
private static final Statistics INSTANCE = new Statistics();
// if meter registry is unavailable, the statistics will not send to metrics
@Getter
public MeterRegistry meterRegistry;
/**
* @return the singleton {@link Statistics} instance
*/
public static Statistics getInstance() {
return INSTANCE;
}
@Autowired
public void setMeterRegistry(final MeterRegistry meterRegistry) {
this.meterRegistry = meterRegistry;
}
// flushes the statistics to the meter registry (if needed)
public static void flush() {
// nothing to do for Hibernate
}
// autoconfigure after CompositeMeterRegistryAutoConfiguration, so when the autoconfiguration is being processed the MeterRegistry
// has already been registered / resolved (if it is to be registered at all) - otherwise @ConditionalOnBean(MeterRegistry.class) may not be
// met event if the MeterRegistry is registered (if resolved later).
// 'autoconfigure after' relies on this is being an AutoConfiguration
@AutoConfiguration(afterName = "org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration")
@Configuration
public static class StatisticsAutoConfiguration {
@ConditionalOnProperty(prefix = "spring.jpa.properties.hibernate", name = "generate_statistics", havingValue = "true")
@ConditionalOnBean(MeterRegistry.class)
@Bean
public Statistics statistics() {
// injects the singleton Statistics, and start scheduler
return Statistics.getInstance();
}
}
}

View File

@@ -0,0 +1 @@
org.eclipse.hawkbit.repository.jpa.Statistics.StatisticsAutoConfiguration