From c97ff591d3802746db1b6f7d42886ecb99e4547a Mon Sep 17 00:00:00 2001 From: Thomas Segismont Date: Wed, 15 Jan 2025 15:09:36 +0100 Subject: [PATCH] Add a ContextLocal key for OpenTracing data Replace usage of generic context local storage. Signed-off-by: Thomas Segismont --- .../tracing/opentracing/OpenTracingTracer.java | 9 +++++---- .../opentracing/OpenTracingTracerFactory.java | 6 +++++- .../tracing/opentracing/OpenTracingUtil.java | 18 +++++++++--------- .../tracing/opentracing/OpenTracingTest.java | 9 ++++----- .../opentracing/OpenTracingUtilTest.java | 15 +++++++-------- 5 files changed, 30 insertions(+), 27 deletions(-) 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..feb6fe5 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 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()); });