diff --git a/vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/OpenTelemetryTracer.java b/vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/OpenTelemetryTracer.java index 8512729..23d36c5 100644 --- a/vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/OpenTelemetryTracer.java +++ b/vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/OpenTelemetryTracer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2023 Contributors to the Eclipse Foundation + * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -22,7 +22,6 @@ import io.opentelemetry.context.propagation.TextMapSetter; import io.vertx.core.Context; import io.vertx.core.internal.ContextInternal; -import io.vertx.core.internal.VertxInternal; import io.vertx.core.spi.tracing.SpanKind; import io.vertx.core.spi.tracing.TagExtractor; import io.vertx.core.spi.tracing.VertxTracer; @@ -32,7 +31,7 @@ import java.util.Map.Entry; import java.util.function.BiConsumer; -import static io.vertx.tracing.opentelemetry.VertxContextStorageProvider.ACTIVE_CONTEXT; +import static io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory.ACTIVE_CONTEXT; class OpenTelemetryTracer implements VertxTracer { @@ -61,7 +60,8 @@ public Operation receiveRequest( return null; } - io.opentelemetry.context.Context otelCtx = ((ContextInternal)context).getLocal(ACTIVE_CONTEXT); + ContextInternal ctx = (ContextInternal) context; + io.opentelemetry.context.Context otelCtx = ctx.getLocal(ACTIVE_CONTEXT); if (otelCtx == null) { otelCtx = io.opentelemetry.context.Context.root(); } @@ -81,7 +81,7 @@ public Operation receiveRequest( .setSpanKind(spanKind); Span span = reportTagsAndStart(spanBuilder, request, tagExtractor, false); - Scope scope = VertxContextStorage.INSTANCE.attach((ContextInternal) context, span.storeInContext(otelCtx)); + Scope scope = VertxContextStorage.INSTANCE.attach(ctx, span.storeInContext(otelCtx)); return new Operation(span, scope); } @@ -128,7 +128,7 @@ public Operation sendRequest( return null; } - io.opentelemetry.context.Context otelCtx = ((ContextInternal)context).getLocal(ACTIVE_CONTEXT); + io.opentelemetry.context.Context otelCtx = ((ContextInternal) context).getLocal(ACTIVE_CONTEXT); if (otelCtx == null) { if (!TracingPolicy.ALWAYS.equals(policy)) { diff --git a/vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/OpenTelemetryTracingFactory.java b/vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/OpenTelemetryTracingFactory.java index 0856675..3994fb1 100644 --- a/vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/OpenTelemetryTracingFactory.java +++ b/vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/OpenTelemetryTracingFactory.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2023 Contributors to the Eclipse Foundation + * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -10,13 +10,17 @@ */ package io.vertx.tracing.opentelemetry; +import io.opentelemetry.context.Context; import io.vertx.core.json.JsonObject; import io.vertx.core.spi.VertxTracerFactory; +import io.vertx.core.spi.context.storage.ContextLocal; import io.vertx.core.spi.tracing.VertxTracer; import io.vertx.core.tracing.TracingOptions; public class OpenTelemetryTracingFactory implements VertxTracerFactory { + static final ContextLocal ACTIVE_CONTEXT = ContextLocal.registerLocal(Context.class); + @Override public VertxTracer tracer(final TracingOptions options) { OpenTelemetryOptions openTelemetryOptions; diff --git a/vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/VertxContextStorageProvider.java b/vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/VertxContextStorageProvider.java index 3cb7b84..84ed5a4 100644 --- a/vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/VertxContextStorageProvider.java +++ b/vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/VertxContextStorageProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2023 Contributors to the Eclipse Foundation + * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -17,9 +17,10 @@ import io.opentelemetry.context.Scope; import io.vertx.core.internal.ContextInternal; -public class VertxContextStorageProvider implements ContextStorageProvider { +import static io.vertx.core.spi.context.storage.AccessMode.CONCURRENT; +import static io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory.ACTIVE_CONTEXT; - public static final Object ACTIVE_CONTEXT = new Object(); +public class VertxContextStorageProvider implements ContextStorageProvider { @Override public ContextStorage get() { @@ -45,12 +46,12 @@ public Scope attach(io.vertx.core.internal.ContextInternal vertxCtx, Context toA return Scope.noop(); } - vertxCtx.putLocal(ACTIVE_CONTEXT, toAttach); + vertxCtx.putLocal(ACTIVE_CONTEXT, CONCURRENT, toAttach); if (current == null) { - return () -> vertxCtx.removeLocal(ACTIVE_CONTEXT); + return () -> vertxCtx.removeLocal(ACTIVE_CONTEXT, CONCURRENT); } - return () -> vertxCtx.putLocal(ACTIVE_CONTEXT, current); + return () -> vertxCtx.putLocal(ACTIVE_CONTEXT, CONCURRENT, current); } @Override diff --git a/vertx-opentelemetry/src/test/java/io/vertx/tracing/opentelemetry/tests/OpenTelemetryTracingFactoryTest.java b/vertx-opentelemetry/src/test/java/io/vertx/tracing/opentelemetry/tests/OpenTelemetryTracingFactoryTest.java index ca17df0..ee15374 100644 --- a/vertx-opentelemetry/src/test/java/io/vertx/tracing/opentelemetry/tests/OpenTelemetryTracingFactoryTest.java +++ b/vertx-opentelemetry/src/test/java/io/vertx/tracing/opentelemetry/tests/OpenTelemetryTracingFactoryTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2023 Contributors to the Eclipse Foundation + * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -26,7 +26,6 @@ import io.vertx.junit5.VertxExtension; import io.vertx.tracing.opentelemetry.OpenTelemetryOptions; import io.vertx.tracing.opentelemetry.Operation; -import io.vertx.tracing.opentelemetry.VertxContextStorageProvider; import io.vertx.tracing.opentelemetry.VertxContextStorageProvider.VertxContextStorage; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -244,8 +243,8 @@ public void sendRequestShouldNotReturnSpanIfPolicyIsPropagateAndPreviousContextI public void sendRequestShouldReturnSpanIfPolicyIsPropagateAndPreviousContextIsPresent(final Vertx vertx) { VertxTracer tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer(); - final ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext(); - ctx.putLocal(VertxContextStorageProvider.ACTIVE_CONTEXT, io.opentelemetry.context.Context.current()); + final Context ctx = vertx.getOrCreateContext(); + VertxContextStorage.INSTANCE.attach((ContextInternal) ctx, io.opentelemetry.context.Context.current()); final Operation operation = tracer.sendRequest( ctx, diff --git a/vertx-opentracing/src/main/java/io/vertx/tracing/opentracing/OpenTracingTracer.java b/vertx-opentracing/src/main/java/io/vertx/tracing/opentracing/OpenTracingTracer.java index d1d6381..5a9b1dc 100644 --- a/vertx-opentracing/src/main/java/io/vertx/tracing/opentracing/OpenTracingTracer.java +++ b/vertx-opentracing/src/main/java/io/vertx/tracing/opentracing/OpenTracingTracer.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2020 Contributors to the Eclipse Foundation + * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -29,7 +29,8 @@ import java.util.Map; import java.util.function.BiConsumer; -import static io.vertx.tracing.opentracing.OpenTracingUtil.ACTIVE_SPAN; +import static io.vertx.core.spi.context.storage.AccessMode.CONCURRENT; +import static io.vertx.tracing.opentracing.OpenTracingTracerFactory.ACTIVE_SPAN; /** * - https://github.com/opentracing/specification/blob/master/semantic_conventions.md @@ -87,7 +88,7 @@ public void put(String key, String value) { .withTag(Tags.COMPONENT.getKey(), "vertx") .start(); reportTags(span, request, tagExtractor); - ((ContextInternal)context).putLocal(ACTIVE_SPAN, span); + ((ContextInternal) context).putLocal(ACTIVE_SPAN, CONCURRENT, span); return span; } } @@ -98,7 +99,7 @@ public void put(String key, String value) { public void sendResponse( Context context, R response, Span span, Throwable failure, TagExtractor tagExtractor) { if (span != null) { - ((ContextInternal)context).removeLocal(ACTIVE_SPAN); + ((ContextInternal) context).removeLocal(ACTIVE_SPAN, CONCURRENT); if (failure != null) { reportFailure(span, failure); } diff --git a/vertx-opentracing/src/main/java/io/vertx/tracing/opentracing/OpenTracingTracerFactory.java b/vertx-opentracing/src/main/java/io/vertx/tracing/opentracing/OpenTracingTracerFactory.java index c53350f..a19f064 100644 --- a/vertx-opentracing/src/main/java/io/vertx/tracing/opentracing/OpenTracingTracerFactory.java +++ b/vertx-opentracing/src/main/java/io/vertx/tracing/opentracing/OpenTracingTracerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2020 Contributors to the Eclipse Foundation + * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -10,14 +10,18 @@ */ package io.vertx.tracing.opentracing; +import io.opentracing.Span; import io.opentracing.Tracer; import io.vertx.core.json.JsonObject; import io.vertx.core.spi.VertxTracerFactory; +import io.vertx.core.spi.context.storage.ContextLocal; import io.vertx.core.spi.tracing.VertxTracer; import io.vertx.core.tracing.TracingOptions; public class OpenTracingTracerFactory implements VertxTracerFactory { + static final ContextLocal ACTIVE_SPAN = ContextLocal.registerLocal(Span.class); + private final Tracer tracer; public OpenTracingTracerFactory() { diff --git a/vertx-opentracing/src/main/java/io/vertx/tracing/opentracing/OpenTracingUtil.java b/vertx-opentracing/src/main/java/io/vertx/tracing/opentracing/OpenTracingUtil.java index 97edc65..02757e9 100644 --- a/vertx-opentracing/src/main/java/io/vertx/tracing/opentracing/OpenTracingUtil.java +++ b/vertx-opentracing/src/main/java/io/vertx/tracing/opentracing/OpenTracingUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2020 Contributors to the Eclipse Foundation + * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -12,24 +12,24 @@ import io.opentracing.Span; import io.vertx.core.Context; -import io.vertx.core.Vertx; import io.vertx.core.internal.ContextInternal; +import static io.vertx.core.spi.context.storage.AccessMode.CONCURRENT; +import static io.vertx.tracing.opentracing.OpenTracingTracerFactory.ACTIVE_SPAN; + /** * OpenTracingContext adds helpers for associating and disassociating spans with the current {@link * Context} */ public final class OpenTracingUtil { - static final String ACTIVE_SPAN = "vertx.tracing.opentracing.span"; - /** * Get the active span from the current {@link Context} * * @return a {@link Span} or null */ public static Span getSpan() { - ContextInternal c = (ContextInternal) Vertx.currentContext(); + ContextInternal c = ContextInternal.current(); return c == null ? null : c.getLocal(ACTIVE_SPAN); } @@ -40,9 +40,9 @@ public static Span getSpan() { */ public static void setSpan(Span span) { if (span != null) { - ContextInternal c = (ContextInternal) Vertx.currentContext(); + ContextInternal c = ContextInternal.current(); if (c != null) { - c.putLocal(ACTIVE_SPAN, span); + c.putLocal(ACTIVE_SPAN, CONCURRENT, span); } } } @@ -51,9 +51,9 @@ public static void setSpan(Span span) { * Remove any active span on the context. */ public static void clearContext() { - ContextInternal c = (ContextInternal) Vertx.currentContext(); + ContextInternal c = ContextInternal.current(); if (c != null) { - c.removeLocal(ACTIVE_SPAN); + c.removeLocal(ACTIVE_SPAN, CONCURRENT); } } } diff --git a/vertx-opentracing/src/test/java/io/vertx/tracing/opentracing/OpenTracingTest.java b/vertx-opentracing/src/test/java/io/vertx/tracing/opentracing/OpenTracingTest.java index 9c7e4e5..8ac8bca 100644 --- a/vertx-opentracing/src/test/java/io/vertx/tracing/opentracing/OpenTracingTest.java +++ b/vertx-opentracing/src/test/java/io/vertx/tracing/opentracing/OpenTracingTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2020 Contributors to the Eclipse Foundation + * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -16,13 +16,11 @@ import io.opentracing.propagation.TextMapAdapter; import io.opentracing.tag.Tags; import io.vertx.core.Vertx; -import io.vertx.core.VertxOptions; import io.vertx.core.http.HttpClient; import io.vertx.core.http.HttpClientOptions; import io.vertx.core.http.HttpMethod; import io.vertx.core.http.HttpServerOptions; import io.vertx.core.internal.ContextInternal; -import io.vertx.core.tracing.TracingOptions; import io.vertx.core.tracing.TracingPolicy; import io.vertx.ext.unit.Async; import io.vertx.ext.unit.TestContext; @@ -38,6 +36,7 @@ import java.util.List; import java.util.Optional; +import static io.vertx.tracing.opentracing.OpenTracingTracerFactory.ACTIVE_SPAN; import static org.junit.Assert.assertEquals; @RunWith(VertxUnitRunner.class) @@ -108,9 +107,9 @@ private void testHttpServerRequestPolicy(TestContext ctx, Async listenLatch = ctx.async(); vertx.createHttpServer(options).requestHandler(req -> { if (expectTrace) { - ctx.assertNotNull(((ContextInternal)Vertx.currentContext()).getLocal(OpenTracingUtil.ACTIVE_SPAN)); + ctx.assertNotNull(ContextInternal.current().getLocal(ACTIVE_SPAN)); } else { - ctx.assertNull(((ContextInternal)Vertx.currentContext()).getLocal(OpenTracingUtil.ACTIVE_SPAN)); + ctx.assertNull(ContextInternal.current().getLocal(ACTIVE_SPAN)); } req.response().end(); }).listen(8080).onComplete(ctx.asyncAssertSuccess(v -> listenLatch.countDown())); diff --git a/vertx-opentracing/src/test/java/io/vertx/tracing/opentracing/OpenTracingUtilTest.java b/vertx-opentracing/src/test/java/io/vertx/tracing/opentracing/OpenTracingUtilTest.java index 1f6945c..59e4618 100644 --- a/vertx-opentracing/src/test/java/io/vertx/tracing/opentracing/OpenTracingUtilTest.java +++ b/vertx-opentracing/src/test/java/io/vertx/tracing/opentracing/OpenTracingUtilTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2020 Contributors to the Eclipse Foundation + * Copyright (c) 2011-2025 Contributors to the Eclipse Foundation * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -10,15 +10,9 @@ */ package io.vertx.tracing.opentracing; -import static io.vertx.tracing.opentracing.OpenTracingUtil.ACTIVE_SPAN; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; - import io.opentracing.Span; import io.opentracing.mock.MockTracer; -import io.vertx.core.Context; import io.vertx.core.Vertx; -import io.vertx.core.VertxOptions; import io.vertx.core.internal.ContextInternal; import io.vertx.ext.unit.TestContext; import io.vertx.ext.unit.junit.VertxUnitRunner; @@ -27,6 +21,11 @@ import org.junit.Test; import org.junit.runner.RunWith; +import static io.vertx.core.spi.context.storage.AccessMode.CONCURRENT; +import static io.vertx.tracing.opentracing.OpenTracingTracerFactory.ACTIVE_SPAN; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + @RunWith(VertxUnitRunner.class) public class OpenTracingUtilTest { @@ -50,7 +49,7 @@ public void getSpan_should_retrieve_a_span_from_the_currentContext(TestContext c vertx.runOnContext(ignored -> { assertNull(OpenTracingUtil.getSpan()); ContextInternal context = (ContextInternal) Vertx.currentContext(); - context.putLocal(ACTIVE_SPAN, span); + context.putLocal(ACTIVE_SPAN, CONCURRENT, span); assertSame(span, OpenTracingUtil.getSpan()); }); diff --git a/vertx-zipkin/src/main/java/io/vertx/tracing/zipkin/ZipkinTracer.java b/vertx-zipkin/src/main/java/io/vertx/tracing/zipkin/ZipkinTracer.java index 963727e..894d8e2 100644 --- a/vertx-zipkin/src/main/java/io/vertx/tracing/zipkin/ZipkinTracer.java +++ b/vertx-zipkin/src/main/java/io/vertx/tracing/zipkin/ZipkinTracer.java @@ -21,7 +21,6 @@ import brave.propagation.TraceContext; import brave.propagation.TraceContextOrSamplingFlags; import io.vertx.core.Context; -import io.vertx.core.Vertx; import io.vertx.core.http.HttpServerRequest; import io.vertx.core.http.HttpServerResponse; import io.vertx.core.internal.ContextInternal; @@ -39,6 +38,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import static io.vertx.core.spi.context.storage.AccessMode.CONCURRENT; +import static io.vertx.tracing.zipkin.ZipkinTracerFactory.*; + /** * Instrumenting a library. *

@@ -46,12 +48,6 @@ */ public class ZipkinTracer implements io.vertx.core.spi.tracing.VertxTracer> { - // docker run --rm -ti -p 9411:9411 openzipkin/zipkin - - public static final String ACTIVE_SPAN = "vertx.tracing.zipkin.active_span"; - public static final String ACTIVE_CONTEXT = "vertx.tracing.zipkin.active_context"; - public static final String ACTIVE_REQUEST = "vertx.tracing.zipkin.active_request"; - private static class HttpServerRequestAdapter extends brave.http.HttpServerRequest { final HttpServerRequest request; @@ -221,7 +217,7 @@ public VertxSender sender() { * @return the current active {@link Span} otherwise {@code null} */ public static Span activeSpan() { - ContextInternal ctx = (ContextInternal) Vertx.currentContext(); + ContextInternal ctx = ContextInternal.current(); if (ctx != null) { return ctx.getLocal(ACTIVE_SPAN); } @@ -232,7 +228,7 @@ public static Span activeSpan() { * @return the current active {@link TraceContext} otherwise {@code null} */ public static TraceContext activeContext() { - ContextInternal ctx = (ContextInternal) Vertx.currentContext(); + ContextInternal ctx = ContextInternal.current(); if (ctx != null) { return ctx.getLocal(ACTIVE_CONTEXT); } @@ -268,6 +264,7 @@ public Span receiveRequest(Context context, SpanKind kind, TracingPolicy pol if (policy == TracingPolicy.IGNORE) { return null; } + ContextInternal ctx = (ContextInternal) context; Span span; if (request instanceof HttpServerRequest) { HttpServerRequest httpReq = (HttpServerRequest) request; @@ -276,6 +273,7 @@ public Span receiveRequest(Context context, SpanKind kind, TracingPolicy pol return null; } span = httpServerHandler.handleReceive(new HttpServerRequestAdapter(httpReq)); + ctx.putLocal(ACTIVE_REQUEST, CONCURRENT, httpReq); } else { Map headerMap = new HashMap<>(); for (Map.Entry header : headers) { @@ -294,22 +292,22 @@ public Span receiveRequest(Context context, SpanKind kind, TracingPolicy pol span.name(operation); reportTags(request, tagExtractor, span); } - ((ContextInternal)context).putLocal(ACTIVE_SPAN, span); - ((ContextInternal)context).putLocal(ACTIVE_REQUEST, request); - ((ContextInternal)context).putLocal(ACTIVE_CONTEXT, span.context()); + ctx.putLocal(ACTIVE_SPAN, CONCURRENT, span); + ctx.putLocal(ACTIVE_CONTEXT, CONCURRENT, span.context()); return span; } @Override public void sendResponse(Context context, R response, Span span, Throwable failure, TagExtractor tagExtractor) { if (span != null) { - ((ContextInternal)context).removeLocal(ACTIVE_SPAN); + ContextInternal ctx = (ContextInternal) context; + ctx.removeLocal(ACTIVE_SPAN, CONCURRENT); if (response instanceof HttpServerResponse) { - httpServerHandler.handleSend(new HttpServerResponseAdapter(((ContextInternal) context).getLocal(ACTIVE_REQUEST), (HttpServerResponse) response, failure), span); + httpServerHandler.handleSend(new HttpServerResponseAdapter(ctx.getLocal(ACTIVE_REQUEST), (HttpServerResponse) response, failure), span); + ctx.removeLocal(ACTIVE_REQUEST, CONCURRENT); } else { span.finish(); } - ((ContextInternal)context).removeLocal(ACTIVE_REQUEST); } } @@ -403,9 +401,9 @@ public void close() { * Remove any active context. */ public static void clearContext() { - ContextInternal c = (ContextInternal) Vertx.currentContext(); + ContextInternal c = ContextInternal.current(); if (c != null) { - c.removeLocal(ACTIVE_CONTEXT); + c.removeLocal(ACTIVE_CONTEXT, CONCURRENT); } } @@ -413,9 +411,9 @@ public static void clearContext() { * Remove any active span. */ public static void clearSpan() { - ContextInternal c = (ContextInternal) Vertx.currentContext(); + ContextInternal c = ContextInternal.current(); if (c != null) { - c.removeLocal(ACTIVE_SPAN); + c.removeLocal(ACTIVE_SPAN, CONCURRENT); } } @@ -423,9 +421,9 @@ public static void clearSpan() { * Import traceId. */ public static void importTraceId(String traceId) { - ContextInternal ctx = (ContextInternal) Vertx.currentContext(); + ContextInternal ctx = ContextInternal.current(); if (ctx != null) { - ctx.putLocal(ACTIVE_CONTEXT, B3SingleFormat.parseB3SingleFormat(traceId).context()); + ctx.putLocal(ACTIVE_CONTEXT, CONCURRENT, B3SingleFormat.parseB3SingleFormat(traceId).context()); } } @@ -444,9 +442,9 @@ public static String exportTraceId() { * Set active {@link TraceContext}. */ public static void setTraceContext(TraceContext context) { - ContextInternal ctx = (ContextInternal) Vertx.currentContext(); + ContextInternal ctx = ContextInternal.current(); if (ctx != null) { - ctx.putLocal(ACTIVE_CONTEXT, context); + ctx.putLocal(ACTIVE_CONTEXT, CONCURRENT, context); } } @@ -454,9 +452,9 @@ public static void setTraceContext(TraceContext context) { * Set active {@link Span}. */ public static void setSpan(Span span) { - ContextInternal ctx = (ContextInternal) Vertx.currentContext(); + ContextInternal ctx = ContextInternal.current(); if (ctx != null) { - ctx.putLocal(ACTIVE_SPAN, span); + ctx.putLocal(ACTIVE_SPAN, CONCURRENT, span); } } } diff --git a/vertx-zipkin/src/main/java/io/vertx/tracing/zipkin/ZipkinTracerFactory.java b/vertx-zipkin/src/main/java/io/vertx/tracing/zipkin/ZipkinTracerFactory.java index eb38cc7..3fa0ee0 100644 --- a/vertx-zipkin/src/main/java/io/vertx/tracing/zipkin/ZipkinTracerFactory.java +++ b/vertx-zipkin/src/main/java/io/vertx/tracing/zipkin/ZipkinTracerFactory.java @@ -10,13 +10,19 @@ */ package io.vertx.tracing.zipkin; +import brave.Span; +import brave.propagation.TraceContext; +import io.vertx.core.http.HttpServerRequest; import io.vertx.core.json.JsonObject; import io.vertx.core.spi.VertxTracerFactory; +import io.vertx.core.spi.context.storage.ContextLocal; import io.vertx.core.tracing.TracingOptions; public class ZipkinTracerFactory implements VertxTracerFactory { - static final ZipkinTracerFactory INSTANCE = new ZipkinTracerFactory(); + static final ContextLocal ACTIVE_SPAN = ContextLocal.registerLocal(Span.class); + static final ContextLocal ACTIVE_CONTEXT = ContextLocal.registerLocal(TraceContext.class); + static final ContextLocal ACTIVE_REQUEST = ContextLocal.registerLocal(HttpServerRequest.class); @Override public ZipkinTracer tracer(TracingOptions options) { diff --git a/vertx-zipkin/src/test/java/io/vertx/tracing/zipkin/tests/ZipkinGenericPropagationTest.java b/vertx-zipkin/src/test/java/io/vertx/tracing/zipkin/tests/ZipkinGenericPropagationTest.java index 9fdacf6..658a92c 100644 --- a/vertx-zipkin/src/test/java/io/vertx/tracing/zipkin/tests/ZipkinGenericPropagationTest.java +++ b/vertx-zipkin/src/test/java/io/vertx/tracing/zipkin/tests/ZipkinGenericPropagationTest.java @@ -42,7 +42,7 @@ public void serverToClient(TestContext ctx) throws Exception { vertx.createHttpServer().requestHandler(req -> { ContextInternal current = (ContextInternal) Vertx.currentContext(); VertxTracer tracer = current.tracer(); - TraceContext requestContext = current.getLocal(ZipkinTracer.ACTIVE_CONTEXT); + TraceContext requestContext = ZipkinTracer.activeContext(); Object request = new Object(); Map headers = new HashMap<>(); Object trace = tracer.sendRequest(current, SpanKind.RPC, TracingPolicy.PROPAGATE, request, "my_op", (BiConsumer) headers::put, TagExtractor.empty()); diff --git a/vertx-zipkin/src/test/java/io/vertx/tracing/zipkin/tests/ZipkinHttpClientITTest.java b/vertx-zipkin/src/test/java/io/vertx/tracing/zipkin/tests/ZipkinHttpClientITTest.java index 10cf1c0..4598601 100644 --- a/vertx-zipkin/src/test/java/io/vertx/tracing/zipkin/tests/ZipkinHttpClientITTest.java +++ b/vertx-zipkin/src/test/java/io/vertx/tracing/zipkin/tests/ZipkinHttpClientITTest.java @@ -18,7 +18,6 @@ import io.vertx.core.VertxOptions; import io.vertx.core.buffer.Buffer; import io.vertx.core.http.*; -import io.vertx.core.internal.ContextInternal; import io.vertx.core.tracing.TracingPolicy; import io.vertx.tracing.zipkin.ZipkinTracer; import io.vertx.tracing.zipkin.ZipkinTracingOptions; @@ -104,11 +103,8 @@ private Future request(HttpClient client, String pathIncludingQuery, Strin }; TraceContext traceCtx = currentTraceContext.get(); if (traceCtx != null) { - // Create a context and associate it with the trace context - ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext(); - ctx.localContextData().put(ZipkinTracer.ACTIVE_CONTEXT, traceCtx); - ctx.runOnContext(v -> { - // Run task on this context so the tracer will resolve it from the local storage + vertx.runOnContext(v -> { + ZipkinTracer.setTraceContext(traceCtx); task.run(); }); } else {