Skip to content

Commit

Permalink
Add a ContextLocal key for OpenTracing data
Browse files Browse the repository at this point in the history
Replace usage of generic context local storage.

Signed-off-by: Thomas Segismont <[email protected]>
  • Loading branch information
tsegismont committed Jan 15, 2025
1 parent 8932399 commit c97ff59
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -98,7 +99,7 @@ public void put(String key, String value) {
public <R> void sendResponse(
Context context, R response, Span span, Throwable failure, TagExtractor<R> tagExtractor) {
if (span != null) {
((ContextInternal)context).removeLocal(ACTIVE_SPAN);
((ContextInternal) context).removeLocal(ACTIVE_SPAN, CONCURRENT);
if (failure != null) {
reportFailure(span, failure);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Span> ACTIVE_SPAN = ContextLocal.registerLocal(Span.class);

private final Tracer tracer;

public OpenTracingTracerFactory() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}

Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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 {

Expand All @@ -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());
});
Expand Down

0 comments on commit c97ff59

Please sign in to comment.