From 39aefed7ed1dc47cc9381aadbacb0aab9e04315b Mon Sep 17 00:00:00 2001 From: Georgios Andrianakis Date: Tue, 14 May 2024 16:17:15 +0300 Subject: [PATCH] Polish code with ResettableSystemProperties --- .../image/jib/deployment/JibProcessor.java | 21 +++++++------------ .../runtime/QuarkusHttpClientFactory.java | 18 +++++----------- .../VertxSpringCloudConfigGateway.java | 17 ++++----------- 3 files changed, 16 insertions(+), 40 deletions(-) diff --git a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java index 20ba3aea728c9..01f72dc80cec1 100644 --- a/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java +++ b/extensions/container-image/container-image-jib/deployment/src/main/java/io/quarkus/container/image/jib/deployment/JibProcessor.java @@ -84,6 +84,7 @@ import io.quarkus.deployment.util.ContainerRuntimeUtil; import io.quarkus.fs.util.ZipUtils; import io.quarkus.maven.dependency.ResolvedDependency; +import io.quarkus.runtime.ResettableSystemProperties; public class JibProcessor { @@ -255,15 +256,13 @@ private JibContainer containerize(ContainerImageConfig containerImageConfig, for (String additionalTag : containerImage.getAdditionalTags()) { containerizer.withAdditionalTag(additionalTag); } - String previousContextStorageSysProp = null; - try { - // Jib uses the Google HTTP Client under the hood which attempts to record traces via OpenCensus which is wired - // to delegate to OpenTelemetry. - // This can lead to problems with the Quarkus OpenTelemetry extension which expects Vert.x to be running, - // something that is not the case at build time, see https://github.com/quarkusio/quarkus/issues/22864. - previousContextStorageSysProp = System.setProperty(OPENTELEMETRY_CONTEXT_CONTEXT_STORAGE_PROVIDER_SYS_PROP, - "default"); + // Jib uses the Google HTTP Client under the hood which attempts to record traces via OpenCensus which is wired + // to delegate to OpenTelemetry. + // This can lead to problems with the Quarkus OpenTelemetry extension which expects Vert.x to be running, + // something that is not the case at build time, see https://github.com/quarkusio/quarkus/issues/22864. + try (var resettableSystemProperties = ResettableSystemProperties + .of(OPENTELEMETRY_CONTEXT_CONTEXT_STORAGE_PROVIDER_SYS_PROP, "default")) { JibContainer container = containerizeUnderLock(jibContainerBuilder, containerizer); log.infof("%s container image %s (%s)\n", containerImageConfig.isPushExplicitlyEnabled() ? "Pushed" : "Created", @@ -272,12 +271,6 @@ private JibContainer containerize(ContainerImageConfig containerImageConfig, return container; } catch (Exception e) { throw new RuntimeException("Unable to create container image", e); - } finally { - if (previousContextStorageSysProp == null) { - System.clearProperty(OPENTELEMETRY_CONTEXT_CONTEXT_STORAGE_PROVIDER_SYS_PROP); - } else { - System.setProperty(OPENTELEMETRY_CONTEXT_CONTEXT_STORAGE_PROVIDER_SYS_PROP, previousContextStorageSysProp); - } } } diff --git a/extensions/kubernetes-client/runtime-internal/src/main/java/io/quarkus/kubernetes/client/runtime/QuarkusHttpClientFactory.java b/extensions/kubernetes-client/runtime-internal/src/main/java/io/quarkus/kubernetes/client/runtime/QuarkusHttpClientFactory.java index a54a030e5b170..ab8d71db34a07 100644 --- a/extensions/kubernetes-client/runtime-internal/src/main/java/io/quarkus/kubernetes/client/runtime/QuarkusHttpClientFactory.java +++ b/extensions/kubernetes-client/runtime-internal/src/main/java/io/quarkus/kubernetes/client/runtime/QuarkusHttpClientFactory.java @@ -9,6 +9,7 @@ import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.http.HttpClient; import io.fabric8.kubernetes.client.vertx.VertxHttpClientBuilder; +import io.quarkus.runtime.ResettableSystemProperties; import io.vertx.core.Vertx; import io.vertx.core.VertxOptions; import io.vertx.core.file.FileSystemOptions; @@ -35,21 +36,12 @@ private Vertx createVertxInstance() { // This is done using the DISABLE_DNS_RESOLVER_PROP_NAME system property. // The DNS resolver used by vert.x is configured during the (synchronous) initialization. // So, we just need to disable the async resolver around the Vert.x instance creation. - String originalValue = System.getProperty(DISABLE_DNS_RESOLVER_PROP_NAME); - Vertx vertx; - try { - System.setProperty(DISABLE_DNS_RESOLVER_PROP_NAME, "true"); - vertx = Vertx.vertx(new VertxOptions().setFileSystemOptions( + try (var resettableSystemProperties = ResettableSystemProperties.of( + DISABLE_DNS_RESOLVER_PROP_NAME, "true")) { + return Vertx.vertx(new VertxOptions().setFileSystemOptions( new FileSystemOptions().setFileCachingEnabled(false).setClassPathResolvingEnabled(false))); - } finally { - // Restore the original value - if (originalValue == null) { - System.clearProperty(DISABLE_DNS_RESOLVER_PROP_NAME); - } else { - System.setProperty(DISABLE_DNS_RESOLVER_PROP_NAME, originalValue); - } + } - return vertx; } @Override diff --git a/extensions/spring-cloud-config-client/runtime/src/main/java/io/quarkus/spring/cloud/config/client/runtime/VertxSpringCloudConfigGateway.java b/extensions/spring-cloud-config-client/runtime/src/main/java/io/quarkus/spring/cloud/config/client/runtime/VertxSpringCloudConfigGateway.java index 5bd22c7833404..fb65b3a0d43d0 100644 --- a/extensions/spring-cloud-config-client/runtime/src/main/java/io/quarkus/spring/cloud/config/client/runtime/VertxSpringCloudConfigGateway.java +++ b/extensions/spring-cloud-config-client/runtime/src/main/java/io/quarkus/spring/cloud/config/client/runtime/VertxSpringCloudConfigGateway.java @@ -18,6 +18,7 @@ import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; +import io.quarkus.runtime.ResettableSystemProperties; import io.quarkus.runtime.util.ClassPathUtils; import io.smallrye.mutiny.Uni; import io.vertx.core.VertxOptions; @@ -62,20 +63,10 @@ private Vertx createVertxInstance() { // This is done using the DISABLE_DNS_RESOLVER_PROP_NAME system property. // The DNS resolver used by vert.x is configured during the (synchronous) initialization. // So, we just need to disable the async resolver around the Vert.x instance creation. - String originalValue = System.getProperty(DISABLE_DNS_RESOLVER_PROP_NAME); - Vertx vertx; - try { - System.setProperty(DISABLE_DNS_RESOLVER_PROP_NAME, "true"); - vertx = Vertx.vertx(new VertxOptions()); - } finally { - // Restore the original value - if (originalValue == null) { - System.clearProperty(DISABLE_DNS_RESOLVER_PROP_NAME); - } else { - System.setProperty(DISABLE_DNS_RESOLVER_PROP_NAME, originalValue); - } + try (var resettableSystemProperties = ResettableSystemProperties.of( + DISABLE_DNS_RESOLVER_PROP_NAME, "true")) { + return Vertx.vertx(new VertxOptions()); } - return vertx; } public static WebClient createHttpClient(Vertx vertx, SpringCloudConfigClientConfig config) {