diff --git a/extensions/opentelemetry/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/QuarkusContextStorage.java b/extensions/opentelemetry/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/QuarkusContextStorage.java index 091871ba873aa..ba7ad80b76113 100644 --- a/extensions/opentelemetry/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/QuarkusContextStorage.java +++ b/extensions/opentelemetry/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/QuarkusContextStorage.java @@ -1,5 +1,6 @@ package io.quarkus.opentelemetry.runtime; +import static io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle.setContextSafe; import static io.smallrye.common.vertx.VertxContext.getOrCreateDuplicatedContext; import static io.smallrye.common.vertx.VertxContext.isDuplicatedContext; @@ -85,8 +86,12 @@ public Scope attach(io.vertx.core.Context vertxContext, Context toAttach) { */ @Override public Context current() { - return Vertx.currentContext() != null ? getOrCreateDuplicatedContext(vertx).getLocal(OTEL_CONTEXT) - : DEFAULT_CONTEXT_STORAGE.current(); + io.vertx.core.Context current = getVertxContext(); + if (current != null) { + return current.getLocal(OTEL_CONTEXT); + } else { + return DEFAULT_CONTEXT_STORAGE.current(); + } } /** @@ -108,6 +113,12 @@ public static Context getContext(io.vertx.core.Context vertxContext) { * @return a duplicated Vert.x Context or null. */ private static io.vertx.core.Context getVertxContext() { - return Vertx.currentContext() != null ? getOrCreateDuplicatedContext(vertx) : null; + io.vertx.core.Context context = Vertx.currentContext(); + if (context != null) { + io.vertx.core.Context dc = getOrCreateDuplicatedContext(context); + setContextSafe(dc, true); + return dc; + } + return null; } } diff --git a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxRecorder.java b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxRecorder.java index 4802063fbf9c0..e23a3b7139e0c 100644 --- a/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxRecorder.java +++ b/extensions/vertx/runtime/src/main/java/io/quarkus/vertx/runtime/VertxRecorder.java @@ -1,5 +1,7 @@ package io.quarkus.vertx.runtime; +import static io.quarkus.vertx.core.runtime.context.VertxContextSafetyToggle.setContextSafe; + import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.List; @@ -100,6 +102,7 @@ public void handle(Message m) { if (invoker.isBlocking()) { // We need to create a duplicated context from the "context" Context dup = VertxContext.getOrCreateDuplicatedContext(context); + setContextSafe(dup, true); dup.executeBlocking(new Handler>() { @Override public void handle(Promise event) {