Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce dedicated ContextLocal keys #90

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand All @@ -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<Operation, Operation> {

Expand Down Expand Up @@ -61,7 +60,8 @@ public <R> 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();
}
Expand All @@ -81,7 +81,7 @@ public <R> 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);
}
Expand Down Expand Up @@ -128,7 +128,7 @@ public <R> 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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Context> ACTIVE_CONTEXT = ContextLocal.registerLocal(Context.class);

@Override
public VertxTracer<?, ?> tracer(final TracingOptions options) {
OpenTelemetryOptions openTelemetryOptions;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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() {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -244,8 +243,8 @@ public void sendRequestShouldNotReturnSpanIfPolicyIsPropagateAndPreviousContextI
public void sendRequestShouldReturnSpanIfPolicyIsPropagateAndPreviousContextIsPresent(final Vertx vertx) {
VertxTracer<Operation, Operation> 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,
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 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 final 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
Loading
Loading