Polish "Add auto-configuration for Hibernate metrics"
Closes gh-12550
This commit is contained in:
parent
b6b92ba937
commit
a85998f4c3
|
|
@ -172,11 +172,6 @@
|
|||
<artifactId>javax.servlet-api</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.sf.ehcache</groupId>
|
||||
<artifactId>ehcache</artifactId>
|
||||
|
|
@ -238,6 +233,11 @@
|
|||
<artifactId>jersey-container-servlet-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
<artifactId>hibernate-core</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
|
|
|
|||
|
|
@ -30,23 +30,22 @@ import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.Simp
|
|||
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for metrics on all available
|
||||
* Hibernate {@link EntityManagerFactory entityManagerFactories} with statistics enabled.
|
||||
* <p>
|
||||
* {@link HibernateMetrics} can only monitor Hibernate {@link EntityManagerFactory}
|
||||
* instances with statistics enabled (for instance, by setting JPA property
|
||||
* <code>hibernate.generate_statistics</code> to <code>true</code>).
|
||||
* Hibernate {@link EntityManagerFactory} instances that have statistics enabled.
|
||||
*
|
||||
* @author Rui Figueira
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@Configuration
|
||||
@AutoConfigureAfter({ MetricsAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
|
||||
SimpleMetricsExportAutoConfiguration.class })
|
||||
@ConditionalOnClass({ EntityManagerFactory.class, MeterRegistry.class })
|
||||
@ConditionalOnBean({ EntityManagerFactory.class, MeterRegistry.class })
|
||||
public class HibernateMetricsAutoConfiguration {
|
||||
|
||||
|
|
@ -61,13 +60,12 @@ public class HibernateMetricsAutoConfiguration {
|
|||
@Autowired
|
||||
public void bindEntityManagerFactoriesToRegistry(
|
||||
Map<String, EntityManagerFactory> entityManagerFactories) {
|
||||
entityManagerFactories.forEach(this::maybeBindEntityManagerFactoryToRegistry);
|
||||
entityManagerFactories.forEach(this::bindEntityManagerFactoryToRegistry);
|
||||
}
|
||||
|
||||
private void maybeBindEntityManagerFactoryToRegistry(String beanName,
|
||||
private void bindEntityManagerFactoryToRegistry(String beanName,
|
||||
EntityManagerFactory entityManagerFactory) {
|
||||
String entityManagerFactoryName = getEntityManagerFactoryName(beanName);
|
||||
// HibernateMetrics internally checks if statistics are enabled before binding
|
||||
new HibernateMetrics(entityManagerFactory, entityManagerFactoryName,
|
||||
Collections.emptyList()).bindTo(this.registry);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,20 @@
|
|||
/*
|
||||
* Copyright 2012-2018 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Auto-configuration for JDBC metrics.
|
||||
*/
|
||||
package org.springframework.boot.actuate.autoconfigure.metrics.orm.jpa;
|
||||
|
|
@ -61,17 +61,18 @@ public class HibernateMetricsAutoConfigurationTests {
|
|||
.withConfiguration(AutoConfigurations.of(DataSourceAutoConfiguration.class,
|
||||
HibernateJpaAutoConfiguration.class,
|
||||
HibernateMetricsAutoConfiguration.class))
|
||||
.withUserConfiguration(BaseConfiguration.class).withPropertyValues(
|
||||
"spring.jpa.properties.hibernate.generate_statistics:true");
|
||||
.withUserConfiguration(BaseConfiguration.class);
|
||||
|
||||
@Test
|
||||
public void autoConfiguredEntityManagerFactoryWithStatsIsInstrumented() {
|
||||
this.contextRunner.run((context) -> {
|
||||
context.getBean(EntityManagerFactory.class).unwrap(SessionFactory.class);
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
registry.get("hibernate.statements")
|
||||
.tags("entityManagerFactory", "entityManagerFactory").meter();
|
||||
});
|
||||
this.contextRunner
|
||||
.withPropertyValues(
|
||||
"spring.jpa.properties.hibernate.generate_statistics:true")
|
||||
.run((context) -> {
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
registry.get("hibernate.statements")
|
||||
.tags("entityManagerFactory", "entityManagerFactory").meter();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -89,7 +90,8 @@ public class HibernateMetricsAutoConfigurationTests {
|
|||
|
||||
@Test
|
||||
public void entityManagerFactoryInstrumentationCanBeDisabled() {
|
||||
this.contextRunner.withPropertyValues("management.metrics.enable.hibernate=false")
|
||||
this.contextRunner.withPropertyValues("management.metrics.enable.hibernate=false",
|
||||
"spring.jpa.properties.hibernate.generate_statistics:true")
|
||||
.run((context) -> {
|
||||
context.getBean(EntityManagerFactory.class)
|
||||
.unwrap(SessionFactory.class);
|
||||
|
|
@ -101,6 +103,8 @@ public class HibernateMetricsAutoConfigurationTests {
|
|||
@Test
|
||||
public void allEntityManagerFactoriesCanBeInstrumented() {
|
||||
this.contextRunner
|
||||
.withPropertyValues(
|
||||
"spring.jpa.properties.hibernate.generate_statistics:true")
|
||||
.withUserConfiguration(TwoEntityManagerFactoriesConfiguration.class)
|
||||
.run((context) -> {
|
||||
context.getBean("firstEntityManagerFactory",
|
||||
|
|
@ -118,13 +122,15 @@ public class HibernateMetricsAutoConfigurationTests {
|
|||
@Test
|
||||
public void entityManagerFactoryInstrumentationIsDisabledIfNotHibernateSessionFactory() {
|
||||
this.contextRunner
|
||||
.withPropertyValues(
|
||||
"spring.jpa.properties.hibernate.generate_statistics:true")
|
||||
.withUserConfiguration(
|
||||
NonHibernateEntityManagerFactoryConfiguration.class)
|
||||
.run((context) -> {
|
||||
// ensure EntityManagerFactory is not an Hibernate SessionFactory
|
||||
assertThatThrownBy(() -> context.getBean(EntityManagerFactory.class)
|
||||
.unwrap(SessionFactory.class))
|
||||
.isInstanceOf(PersistenceException.class);
|
||||
.isInstanceOf(PersistenceException.class);
|
||||
MeterRegistry registry = context.getBean(MeterRegistry.class);
|
||||
assertThat(registry.find("hibernate.statements").meter()).isNull();
|
||||
});
|
||||
|
|
@ -146,6 +152,7 @@ public class HibernateMetricsAutoConfigurationTests {
|
|||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
|
@ -174,6 +181,7 @@ public class HibernateMetricsAutoConfigurationTests {
|
|||
new HibernateJpaVendorAdapter(), jpaProperties, null);
|
||||
return builder.dataSource(ds).packages(PACKAGE_CLASSES).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
|
|
@ -187,5 +195,7 @@ public class HibernateMetricsAutoConfigurationTests {
|
|||
.willThrow(PersistenceException.class);
|
||||
return mockedFactory;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1768,32 +1768,34 @@ Also, Hikari-specific metrics are exposed with a `hikaricp` prefix. Each metric
|
|||
by the name of the Pool (can be controlled with `spring.datasource.name`).
|
||||
|
||||
|
||||
[[production-ready-metrics-rabbitmq]]
|
||||
==== RabbitMQ Metrics
|
||||
Auto-configuration will enable the instrumentation of all available RabbitMQ connection
|
||||
factories with a metric named `rabbitmq`.
|
||||
|
||||
|
||||
[[production-ready-metrics-hibernate]]
|
||||
==== Hibernate Metrics
|
||||
Auto-configuration enables the instrumentation of all available Hibernate
|
||||
`EntityManagerFactory` objects that have statistics enabled with a metric named
|
||||
`EntityManagerFactory` instances that have statistics enabled with a metric named
|
||||
`hibernate`.
|
||||
|
||||
Metrics are also tagged by the name of the `EntityManagerFactory` that is derived from
|
||||
Metrics are also tagged by the name of the `EntityManagerFactory` that is derived from
|
||||
the bean name.
|
||||
|
||||
To enable statistics on Hibernate `EntityManagerFactory` instances, JPA property `hibernate.generate_statistics` must be set to `true`. For example, on the default
|
||||
`EntityManagerFactory`, you need to set the following property:
|
||||
To enable statistics, the standardJPA property `hibernate.generate_statistics` must be
|
||||
set to `true`. You can enable that on the auto-configured `EntityManagerFactory` as shown
|
||||
in the following example:
|
||||
|
||||
.application.properties
|
||||
[source,properties,indent=0]
|
||||
----
|
||||
spring.jpa.properties.hibernate.generate_statistics=true
|
||||
spring.jpa.properties.hibernate.generate_statistics=true
|
||||
----
|
||||
|
||||
|
||||
|
||||
[[production-ready-metrics-rabbitmq]]
|
||||
==== RabbitMQ Metrics
|
||||
Auto-configuration will enable the instrumentation of all available RabbitMQ connection
|
||||
factories with a metric named `rabbitmq`.
|
||||
|
||||
|
||||
|
||||
[[production-ready-metrics-custom]]
|
||||
=== Registering custom metrics
|
||||
To register custom metrics, inject `MeterRegistry` into your component, as shown in the
|
||||
|
|
|
|||
Loading…
Reference in New Issue