Skip to content

Commit

Permalink
Merge pull request #45824 from zakkak/2025-01-23-micrometer-processor…
Browse files Browse the repository at this point in the history
…-registrations

Register more elements to prevent unregistered accesses at runtime
  • Loading branch information
geoand authored Jan 23, 2025
2 parents 7d37ec4 + e35f992 commit c91718e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
import io.quarkus.deployment.builditem.nativeimage.NativeImageProxyDefinitionBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveMethodBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
import io.quarkus.deployment.index.IndexingUtil;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
Expand Down Expand Up @@ -311,6 +312,7 @@ public void configurationDescriptorBuilding(
BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles,
BuildProducer<PersistenceUnitDescriptorBuildItem> persistenceUnitDescriptors,
BuildProducer<ReflectiveMethodBuildItem> reflectiveMethods,
List<DatabaseKindDialectBuildItem> dbKindMetadataBuildItems) {

if (!hasEntities(jpaModel)) {
Expand Down Expand Up @@ -348,7 +350,7 @@ public void configurationDescriptorBuilding(
handleHibernateORMWithNoPersistenceXml(hibernateOrmConfig, index, persistenceXmlDescriptors,
jdbcDataSources, applicationArchivesBuildItem, launchMode.getLaunchMode(), jpaModel, capabilities,
systemProperties, nativeImageResources, hotDeploymentWatchedFiles, persistenceUnitDescriptors,
dbKindMetadataBuildItems);
reflectiveMethods, dbKindMetadataBuildItems);
}
}

Expand Down Expand Up @@ -824,6 +826,7 @@ private void handleHibernateORMWithNoPersistenceXml(
BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles,
BuildProducer<PersistenceUnitDescriptorBuildItem> persistenceUnitDescriptors,
BuildProducer<ReflectiveMethodBuildItem> reflectiveMethods,
List<DatabaseKindDialectBuildItem> dbKindMetadataBuildItems) {
if (!descriptors.isEmpty()) {
if (hibernateOrmConfig.isAnyNonPersistenceXmlPropertySet()) {
Expand Down Expand Up @@ -872,7 +875,7 @@ private void handleHibernateORMWithNoPersistenceXml(
jpaModel.getXmlMappings(PersistenceUnitUtil.DEFAULT_PERSISTENCE_UNIT_NAME),
jdbcDataSources, applicationArchivesBuildItem, launchMode, capabilities,
systemProperties, nativeImageResources, hotDeploymentWatchedFiles, persistenceUnitDescriptors,
storageEngineCollector, dbKindMetadataBuildItems);
reflectiveMethods, storageEngineCollector, dbKindMetadataBuildItems);
} else if (!modelClassesAndPackagesForDefaultPersistenceUnit.isEmpty()
&& (!hibernateOrmConfig.defaultPersistenceUnit().datasource().isPresent()
|| DataSourceUtil.isDefault(hibernateOrmConfig.defaultPersistenceUnit().datasource().get()))
Expand All @@ -892,7 +895,7 @@ private void handleHibernateORMWithNoPersistenceXml(
jpaModel.getXmlMappings(persistenceUnitEntry.getKey()),
jdbcDataSources, applicationArchivesBuildItem, launchMode, capabilities,
systemProperties, nativeImageResources, hotDeploymentWatchedFiles, persistenceUnitDescriptors,
storageEngineCollector, dbKindMetadataBuildItems);
reflectiveMethods, storageEngineCollector, dbKindMetadataBuildItems);
}

if (storageEngineCollector.size() > 1) {
Expand All @@ -915,6 +918,7 @@ private static void producePersistenceUnitDescriptorFromConfig(
BuildProducer<NativeImageResourceBuildItem> nativeImageResources,
BuildProducer<HotDeploymentWatchedFileBuildItem> hotDeploymentWatchedFiles,
BuildProducer<PersistenceUnitDescriptorBuildItem> persistenceUnitDescriptors,
BuildProducer<ReflectiveMethodBuildItem> reflectiveMethods,
Set<String> storageEngineCollector,
List<DatabaseKindDialectBuildItem> dbKindMetadataBuildItems) {
Optional<JdbcDataSourceBuildItem> jdbcDataSource = findJdbcDataSource(persistenceUnitName, persistenceUnitConfig,
Expand Down Expand Up @@ -943,7 +947,7 @@ private static void producePersistenceUnitDescriptorFromConfig(
MultiTenancyStrategy multiTenancyStrategy = getMultiTenancyStrategy(persistenceUnitConfig.multitenant());
collectDialectConfig(persistenceUnitName, persistenceUnitConfig,
dbKindMetadataBuildItems, jdbcDataSource, multiTenancyStrategy,
systemProperties, descriptor.getProperties()::setProperty, storageEngineCollector);
systemProperties, reflectiveMethods, descriptor.getProperties()::setProperty, storageEngineCollector);

// Physical Naming Strategy
persistenceUnitConfig.physicalNamingStrategy().ifPresent(
Expand Down Expand Up @@ -1134,6 +1138,7 @@ private static void collectDialectConfig(String persistenceUnitName,
List<DatabaseKindDialectBuildItem> dbKindMetadataBuildItems, Optional<JdbcDataSourceBuildItem> jdbcDataSource,
MultiTenancyStrategy multiTenancyStrategy,
BuildProducer<SystemPropertyBuildItem> systemProperties,
BuildProducer<ReflectiveMethodBuildItem> reflectiveMethods,
BiConsumer<String, String> puPropertiesCollector, Set<String> storageEngineCollector) {
Optional<String> explicitDialect = persistenceUnitConfig.dialect().dialect();
Optional<String> dbKind = jdbcDataSource.map(JdbcDataSourceBuildItem::getDbKind);
Expand Down Expand Up @@ -1210,6 +1215,14 @@ private static void collectDialectConfig(String persistenceUnitName,
if (dbProductVersion.isPresent()) {
puPropertiesCollector.accept(AvailableSettings.JAKARTA_HBM2DDL_DB_VERSION, dbProductVersion.get());
}

if ((dbKind.isPresent() && DatabaseKind.isPostgreSQL(dbKind.get())
|| (dialect.isPresent() && dialect.get().toLowerCase(Locale.ROOT).contains("postgres")))) {
// Workaround for https://hibernate.atlassian.net/browse/HHH-19063
reflectiveMethods.produce(new ReflectiveMethodBuildItem(
"Accessed in org.hibernate.engine.jdbc.env.internal.DefaultSchemaNameResolver.determineAppropriateResolverDelegate",
true, "org.postgresql.jdbc.PgConnection", "getSchema"));
}
}

private static void collectDialectConfigForPersistenceXml(String persistenceUnitName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.NativeImageFeatureBuildItem;
import io.quarkus.deployment.builditem.SslNativeConfigBuildItem;
import io.quarkus.deployment.builditem.nativeimage.NativeImageResourceBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeReinitializedClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
Expand All @@ -40,9 +41,12 @@ RuntimeReinitializedClassBuildItem runtimeReinitialize() {

@BuildStep
void registerDriver(BuildProducer<JdbcDriverBuildItem> jdbcDriver,
BuildProducer<NativeImageResourceBuildItem> resources,
SslNativeConfigBuildItem sslNativeConfigBuildItem) {
jdbcDriver.produce(new JdbcDriverBuildItem(DatabaseKind.POSTGRESQL, "org.postgresql.Driver",
"org.postgresql.xa.PGXADataSource"));
// Accessed in org.postgresql.Driver.loadDefaultProperties
resources.produce(new NativeImageResourceBuildItem("org/postgresql/driverconfig.properties"));
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import io.quarkus.deployment.builditem.ShutdownContextBuildItem;
import io.quarkus.deployment.builditem.SystemPropertyBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveMethodBuildItem;
import io.quarkus.deployment.logging.LoggingSetupBuildItem;
import io.quarkus.deployment.metrics.MetricsCapabilityBuildItem;
import io.quarkus.deployment.metrics.MetricsFactoryConsumerBuildItem;
Expand Down Expand Up @@ -79,6 +80,11 @@ public class MicrometerProcessor {
private static final DotName TIMED_INTERCEPTOR = DotName.createSimple(MicrometerTimedInterceptor.class.getName());
private static final DotName METER_TAG_SUPPORT = DotName.createSimple(MeterTagsSupport.class.getName());

private static final List<String> OPERATING_SYSTEM_BEAN_CLASS_NAMES = List.of(
"com.ibm.lang.management.OperatingSystemMXBean", // J9
"com.sun.management.OperatingSystemMXBean" // HotSpot
);

public static class MicrometerEnabled implements BooleanSupplier {
MicrometerConfig mConfig;

Expand Down Expand Up @@ -116,7 +122,8 @@ UnremovableBeanBuildItem registerAdditionalBeans(CombinedIndexBuildItem indexBui
BuildProducer<MicrometerRegistryProviderBuildItem> providerClasses,
BuildProducer<ReflectiveClassBuildItem> reflectiveClasses,
BuildProducer<AdditionalBeanBuildItem> additionalBeans,
BuildProducer<InterceptorBindingRegistrarBuildItem> interceptorBindings) {
BuildProducer<InterceptorBindingRegistrarBuildItem> interceptorBindings,
BuildProducer<ReflectiveMethodBuildItem> reflectiveMethods) {

// Create and keep some basic Providers
additionalBeans.produce(AdditionalBeanBuildItem.builder()
Expand Down Expand Up @@ -158,6 +165,14 @@ public List<InterceptorBinding> getAdditionalBindings() {
"org.HdrHistogram.ConcurrentHistogram")
.build());

for (String beanClassName : OPERATING_SYSTEM_BEAN_CLASS_NAMES) {
String reason = "Accessed by io.micrometer.core.instrument.binder.system.ProcessorMetrics.ProcessorMetrics(java.lang.Iterable<io.micrometer.core.instrument.Tag>)";
reflectiveMethods.produce(new ReflectiveMethodBuildItem(reason, false, beanClassName, "getCpuLoad"));
reflectiveMethods.produce(new ReflectiveMethodBuildItem(reason, false, beanClassName, "getSystemCpuLoad"));
reflectiveMethods.produce(new ReflectiveMethodBuildItem(reason, false, beanClassName, "getProcessCpuLoad"));
reflectiveMethods.produce(new ReflectiveMethodBuildItem(reason, false, beanClassName, "getProcessCpuTime"));
}

return UnremovableBeanBuildItem.beanTypes(METER_REGISTRY, METER_BINDER, METER_FILTER, METER_REGISTRY_CUSTOMIZER,
NAMING_CONVENTION);
}
Expand Down

0 comments on commit c91718e

Please sign in to comment.