micronaut-projects / micronaut-micrometer

Integration between Micronaut and Micrometer
Apache License 2.0
36 stars 66 forks source link

Shaded Netty within Micrometer Statsd Causes Native Image Build Failure #257

Open brianwyka opened 3 years ago

brianwyka commented 3 years ago

Thanks for reporting an issue, please review the task list below before submitting the issue. Your issue report will be closed if the issue is incomplete and the below tasks not completed.

NOTE: If you are unsure about something and the issue is more of a question a better place to ask questions is on Stack Overflow (https://stackoverflow.com/tags/micronaut) or Gitter (https://gitter.im/micronautfw/). DO NOT use the issue tracker to ask questions.

Task List

Expected Behaviour

The native image build should succeed

Actual Behaviour

Native image build fails with error:

.Example error (but not comprehensive)

Error: Classes that should be initialized at run time got initialized during image building:
 io.micrometer.shaded.io.netty.util.internal.PlatformDependent was unintentionally initialized at build time. To see why io.micrometer.shaded.io.netty.util.internal.PlatformDependent got initialized use -H:+TraceClassInitialization
io.micrometer.shaded.io.netty.util.internal.PlatformDependent0 was unintentionally initialized at build time. To see why io.micrometer.shaded.io.netty.util.internal.PlatformDependent0 got initialized use -H:+TraceClassInitialization

Environment Information

Example Application

https://github.com/brianwyka/micronaut-graal-micrometer-statsd

Steps to Reproduce

./mvnw package -Dpackaging=native-image

Attempted Resolutions

Updated the native-image.properties configuration to include:

--initialize-at-build-time=io.micrometer.shaded.io.netty.util
--initialize-at-build-time=io.micrometer.shaded.io.netty.util.concurrent
--initialize-at-run-time=io.micrometer.shaded.io.netty.util.internal.logging.Log4JLogger

Then results in this:

Error: Unsupported features in 2 methods
Detailed message:
Error: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: No instances of java.net.Inet4Address are allowed in the image heap as this class should be initialized at image runtime. Object has been initialized without the native-image initialization instrumentation and the stack trace can't be tracked.
Trace: 
        at parsing io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhost4LazyHolder.access$000(NetUtilSubstitutions.java:54)
Call path from entry point to io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhost4LazyHolder.access$000(): 
        no path found from entry point to target method

Error: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: No instances of java.net.Inet6Address are allowed in the image heap as this class should be initialized at image runtime. Object has been initialized without the native-image initialization instrumentation and the stack trace can't be tracked.
Trace: 
        at parsing io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhostLazyHolder.access$200(NetUtilSubstitutions.java:84)
Call path from entry point to io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhostLazyHolder.access$200(): 
        at io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhostLazyHolder.access$200(NetUtilSubstitutions.java:84)
        at io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhostAccessor.get(NetUtilSubstitutions.java:76)
        at io.micrometer.shaded.reactor.netty.udp.UdpClient.getHost(UdpClient.java:552)
        at io.micrometer.shaded.reactor.netty.udp.UdpClient.lambda$port$6(UdpClient.java:334)
        at io.micrometer.shaded.reactor.netty.udp.UdpClient$$Lambda$3461/0x00000007c2461c40.apply(Unknown Source)
        at sun.security.ec.XECParameters$1.get(XECParameters.java:183)
        at com.oracle.svm.core.jdk.SystemPropertiesSupport.initializeLazyValue(SystemPropertiesSupport.java:190)
        at com.oracle.svm.core.jdk.SystemPropertiesSupport.getProperty(SystemPropertiesSupport.java:143)
        at com.oracle.svm.core.jdk.Target_java_lang_System.getProperty(JavaLangSubstitutions.java:331)
        at com.oracle.svm.jni.JNIJavaCallWrappers.jniInvoke_ARRAY:Ljava_lang_System_2_0002egetProperty_00028Ljava_lang_String_2_00029Ljava_lang_String_2(generated:0)

com.oracle.svm.core.util.UserError$UserException: Unsupported features in 2 methods
Detailed message:
Error: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: No instances of java.net.Inet4Address are allowed in the image heap as this class should be initialized at image runtime. Object has been initialized without the native-image initialization instrumentation and the stack trace can't be tracked.
Trace: 
        at parsing io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhost4LazyHolder.access$000(NetUtilSubstitutions.java:54)
Call path from entry point to io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhost4LazyHolder.access$000(): 
        no path found from entry point to target method

Error: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: No instances of java.net.Inet6Address are allowed in the image heap as this class should be initialized at image runtime. Object has been initialized without the native-image initialization instrumentation and the stack trace can't be tracked.
Trace: 
        at parsing io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhostLazyHolder.access$200(NetUtilSubstitutions.java:84)
Call path from entry point to io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhostLazyHolder.access$200(): 
        at io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhostLazyHolder.access$200(NetUtilSubstitutions.java:84)
        at io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhostAccessor.get(NetUtilSubstitutions.java:76)
        at io.micrometer.shaded.reactor.netty.udp.UdpClient.getHost(UdpClient.java:552)
        at io.micrometer.shaded.reactor.netty.udp.UdpClient.lambda$port$6(UdpClient.java:334)
        at io.micrometer.shaded.reactor.netty.udp.UdpClient$$Lambda$3461/0x00000007c2461c40.apply(Unknown Source)
        at sun.security.ec.XECParameters$1.get(XECParameters.java:183)
        at com.oracle.svm.core.jdk.SystemPropertiesSupport.initializeLazyValue(SystemPropertiesSupport.java:190)
        at com.oracle.svm.core.jdk.SystemPropertiesSupport.getProperty(SystemPropertiesSupport.java:143)
        at com.oracle.svm.core.jdk.Target_java_lang_System.getProperty(JavaLangSubstitutions.java:331)
        at com.oracle.svm.jni.JNIJavaCallWrappers.jniInvoke_ARRAY:Ljava_lang_System_2_0002egetProperty_00028Ljava_lang_String_2_00029Ljava_lang_String_2(generated:0)

        at com.oracle.svm.core.util.UserError.abort(UserError.java:79)
        at com.oracle.svm.hosted.FallbackFeature.reportAsFallback(FallbackFeature.java:217)
        at com.oracle.svm.hosted.NativeImageGenerator.runPointsToAnalysis(NativeImageGenerator.java:765)
        at com.oracle.svm.hosted.NativeImageGenerator.doRun(NativeImageGenerator.java:555)
        at com.oracle.svm.hosted.NativeImageGenerator.lambda$run$0(NativeImageGenerator.java:468)
        at java.base/java.util.concurrent.ForkJoinTask$AdaptedRunnableAction.exec(ForkJoinTask.java:1407)
        at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
        at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
        at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
        at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
        at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Caused by: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: Unsupported features in 2 methods
Detailed message:
Error: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: No instances of java.net.Inet4Address are allowed in the image heap as this class should be initialized at image runtime. Object has been initialized without the native-image initialization instrumentation and the stack trace can't be tracked.
Trace: 
        at parsing io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhost4LazyHolder.access$000(NetUtilSubstitutions.java:54)
Call path from entry point to io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhost4LazyHolder.access$000(): 
        no path found from entry point to target method

Error: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: No instances of java.net.Inet6Address are allowed in the image heap as this class should be initialized at image runtime. Object has been initialized without the native-image initialization instrumentation and the stack trace can't be tracked.
Trace: 
        at parsing io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhostLazyHolder.access$200(NetUtilSubstitutions.java:84)
Call path from entry point to io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhostLazyHolder.access$200(): 
        at io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhostLazyHolder.access$200(NetUtilSubstitutions.java:84)
        at io.micrometer.shaded.io.netty.util.NetUtilSubstitutions$NetUtilLocalhostAccessor.get(NetUtilSubstitutions.java:76)
        at io.micrometer.shaded.reactor.netty.udp.UdpClient.getHost(UdpClient.java:552)
        at io.micrometer.shaded.reactor.netty.udp.UdpClient.lambda$port$6(UdpClient.java:334)
        at io.micrometer.shaded.reactor.netty.udp.UdpClient$$Lambda$3461/0x00000007c2461c40.apply(Unknown Source)
        at sun.security.ec.XECParameters$1.get(XECParameters.java:183)
        at com.oracle.svm.core.jdk.SystemPropertiesSupport.initializeLazyValue(SystemPropertiesSupport.java:190)
        at com.oracle.svm.core.jdk.SystemPropertiesSupport.getProperty(SystemPropertiesSupport.java:143)
        at com.oracle.svm.core.jdk.Target_java_lang_System.getProperty(JavaLangSubstitutions.java:331)
        at com.oracle.svm.jni.JNIJavaCallWrappers.jniInvoke_ARRAY:Ljava_lang_System_2_0002egetProperty_00028Ljava_lang_String_2_00029Ljava_lang_String_2(generated:0)

        at com.oracle.graal.pointsto.constraints.UnsupportedFeatures.report(UnsupportedFeatures.java:129)
        at com.oracle.svm.hosted.NativeImageGenerator.runPointsToAnalysis(NativeImageGenerator.java:762)
        ... 8 more
Error: Image build request failed with exit status 1

The build succeeded when I removed the io.micronaut.micrometer:micronaut-micrometer-registry-statsd dependency.

brianwyka commented 3 years ago

@ilopmar, seems like your area 🥇

cohenor commented 2 years ago

Hey, would be glad to know if there's any progress on this issue please 🙏 I'm experiencing the same behavior with Micronaut 3.2.3 and JDK GraalVM CE 21.3.0 (Java 17.0.1). @brianwyka Were you able to work around this somehow?

Thanks.

brianwyka commented 2 years ago

Sorry @cohenor, I have not tried this since the failure, nor have I worked on a workaround.

cohenor commented 2 years ago

Sorry @cohenor, I have not tried this since the failure, nor have I worked on a workaround.

Gotcha, thanks for your response.

graemerocher commented 2 years ago

The issue is that by shading Netty all of the GraalVM native config has to be re-applied to the native version. A feature would need to be added like this one https://github.com/micronaut-projects/micronaut-core/blob/1a15f0b5aba53da56f8915fc904ae4b7512e6f15/http-netty/src/main/java/io/micronaut/http/netty/graal/HttpNettyFeature.java and