Skip to content
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
Expand Up @@ -4,7 +4,7 @@
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.trace.*;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.core.eventbus.DeliveryOptions;
Expand All @@ -15,6 +15,7 @@
import io.vertx.core.tracing.TracingPolicy;
import io.vertx.docgen.Source;
import io.vertx.tracing.opentelemetry.OpenTelemetryOptions;
import io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory;

@Source
public class OpenTelemetryExamples {
Expand All @@ -28,11 +29,10 @@ public void ex1() {
}

public void ex2(OpenTelemetry openTelemetry) {
Vertx vertx = Vertx.vertx(new VertxOptions()
.setTracingOptions(
new OpenTelemetryOptions(openTelemetry)
)
);
Vertx vertx = Vertx
.builder()
.withTracer(new OpenTelemetryTracingFactory(openTelemetry))
.build();
}

public void ex3(Vertx vertx) {
Expand Down Expand Up @@ -61,13 +61,16 @@ public void ex6(Vertx vertx) {
vertx.eventBus().send("the-address", "foo", options);
}

public void ex7(VertxOptions vertxOptions) {
public void ex7() {
SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder().build();
OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider)
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
.buildAndRegisterGlobal();

vertxOptions.setTracingOptions(new OpenTelemetryOptions(openTelemetry));
Vertx vertx = Vertx
.builder()
.withTracer(new OpenTelemetryTracingFactory(openTelemetry))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,25 @@
*/
package io.vertx.tracing.opentelemetry;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.vertx.codegen.annotations.DataObject;
import io.vertx.core.json.JsonObject;
import io.vertx.core.spi.tracing.VertxTracer;
import io.vertx.core.tracing.TracingOptions;

@DataObject
public class OpenTelemetryOptions extends TracingOptions {

private OpenTelemetry openTelemetry;

public OpenTelemetryOptions(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
public OpenTelemetryOptions() {
}

public OpenTelemetryOptions() {
public OpenTelemetryOptions(OpenTelemetryOptions other) {
}

public OpenTelemetryOptions(JsonObject json) {
super(json);
}

public VertxTracer<Operation, Operation> buildTracer() {
if (openTelemetry != null) {
return new OpenTelemetryTracer(openTelemetry);
} else {
return new OpenTelemetryTracer(GlobalOpenTelemetry.get());
}
@Override
public OpenTelemetryOptions copy() {
return new OpenTelemetryOptions(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*/
package io.vertx.tracing.opentelemetry;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.context.Context;
import io.vertx.core.json.JsonObject;
import io.vertx.core.spi.VertxTracerFactory;
Expand All @@ -21,24 +23,32 @@ public class OpenTelemetryTracingFactory implements VertxTracerFactory {

static final ContextLocal<Context> ACTIVE_CONTEXT = ContextLocal.registerLocal(Context.class);

private final OpenTelemetry openTelemetry;

public OpenTelemetryTracingFactory() {
this(null);
}

public OpenTelemetryTracingFactory(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
}

@Override
public VertxTracer<?, ?> tracer(final TracingOptions options) {
OpenTelemetryOptions openTelemetryOptions;
if (options instanceof OpenTelemetryOptions) {
openTelemetryOptions = (OpenTelemetryOptions) options;
if (openTelemetry != null) {
return new OpenTelemetryTracer(openTelemetry);
} else {
openTelemetryOptions = new OpenTelemetryOptions(options.toJson());
return new OpenTelemetryTracer(GlobalOpenTelemetry.get());
}
return openTelemetryOptions.buildTracer();
}

@Override
public TracingOptions newOptions() {
public OpenTelemetryOptions newOptions() {
return new OpenTelemetryOptions();
}

@Override
public TracingOptions newOptions(JsonObject jsonObject) {
public OpenTelemetryOptions newOptions(JsonObject jsonObject) {
return new OpenTelemetryOptions(jsonObject);
}
}
2 changes: 2 additions & 0 deletions vertx-opentelemetry/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
requires io.opentelemetry.sdk.trace;
requires io.vertx.core;

exports io.vertx.tracing.opentelemetry;

provides io.opentelemetry.context.ContextStorageProvider with io.vertx.tracing.opentelemetry.VertxContextStorageProvider;
provides io.vertx.core.spi.VertxServiceProvider with io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.vertx.core.*;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.eventbus.DeliveryOptions;
import io.vertx.core.http.*;
import io.vertx.core.tracing.TracingPolicy;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import io.vertx.tracing.opentelemetry.OpenTelemetryOptions;
import io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -44,7 +47,10 @@ public class EventBusTest {

@BeforeEach
public void setUp() throws Exception {
vertx = Vertx.vertx(new VertxOptions().setTracingOptions(new OpenTelemetryOptions(otelTesting.getOpenTelemetry())));
vertx = Vertx
.builder()
.withTracer(new OpenTelemetryTracingFactory(otelTesting.getOpenTelemetry()))
.build();
client = vertx.createHttpClient(new HttpClientOptions().setDefaultPort(8080));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
import io.opentelemetry.context.propagation.TextMapSetter;
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.vertx.core.*;
import io.vertx.core.DeploymentOptions;
import io.vertx.core.Future;
import io.vertx.core.ThreadingModel;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.*;
import io.vertx.core.tracing.TracingPolicy;
import io.vertx.junit5.Checkpoint;
import io.vertx.junit5.VertxExtension;
import io.vertx.junit5.VertxTestContext;
import io.vertx.tracing.opentelemetry.OpenTelemetryOptions;
import io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -62,7 +65,10 @@ public class OpenTelemetryIntegrationTest {

@BeforeEach
public void setUp() throws Exception {
vertx = Vertx.vertx(new VertxOptions().setTracingOptions(new OpenTelemetryOptions(otelTesting.getOpenTelemetry())));
vertx = Vertx
.builder()
.withTracer(new OpenTelemetryTracingFactory(otelTesting.getOpenTelemetry()))
.build();
textMapPropagator = otelTesting.getOpenTelemetry().getPropagators().getTextMapPropagator();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.vertx.core.tracing.TracingPolicy;
import io.vertx.junit5.VertxExtension;
import io.vertx.tracing.opentelemetry.OpenTelemetryOptions;
import io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory;
import io.vertx.tracing.opentelemetry.Operation;
import io.vertx.tracing.opentelemetry.VertxContextStorageProvider.VertxContextStorage;
import org.junit.jupiter.api.Test;
Expand All @@ -46,7 +47,7 @@ public class OpenTelemetryTracingFactoryTest {

@Test
public void receiveRequestShouldNotReturnSpanIfPolicyIsIgnore(final Vertx vertx) {
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer();

final Operation operation = tracer.receiveRequest(
vertx.getOrCreateContext(),
Expand All @@ -61,9 +62,18 @@ public void receiveRequestShouldNotReturnSpanIfPolicyIsIgnore(final Vertx vertx)
assertThat(operation).isNull();
}

static VertxTracer<Operation, Operation> buildTracer() {
return buildTracer(OpenTelemetry.noop());
}

@SuppressWarnings("unchecked")
static VertxTracer<Operation, Operation> buildTracer(OpenTelemetry openTelemetry) {
return (VertxTracer<Operation, Operation>) new OpenTelemetryTracingFactory(openTelemetry).tracer(new OpenTelemetryOptions());
}

@Test
public void receiveRequestShouldNotReturnSpanIfPolicyIsPropagateAndPreviousContextIsNotPresent(final Vertx vertx) {
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer();

final Operation operation = tracer.receiveRequest(
vertx.getOrCreateContext(),
Expand All @@ -80,9 +90,7 @@ public void receiveRequestShouldNotReturnSpanIfPolicyIsPropagateAndPreviousConte

@Test
public void receiveRequestShouldReturnSpanIfPolicyIsPropagateAndPreviousContextIsPresent(final Vertx vertx) {
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(
OpenTelemetry.propagating(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer(OpenTelemetry.propagating(ContextPropagators.create(W3CTraceContextPropagator.getInstance())));

final Iterable<Map.Entry<String, String>> headers = Collections.singletonList(
new SimpleImmutableEntry<>("traceparent", "00-83ebbd06a32c2eaa8d5bf4b060d7cbfa-140cd1a04ab7be4b-01")
Expand All @@ -107,9 +115,7 @@ public void receiveRequestShouldReturnSpanIfPolicyIsPropagateAndPreviousContextI

@Test
public void receiveRequestShouldReturnAParentedSpanIfPolicyIsPropagateAndTheOtelContextHasAnOngoingSpan(final Vertx vertx) throws ExecutionException, InterruptedException {
final OpenTelemetry openTelemetry = OpenTelemetry.propagating(
ContextPropagators.create(W3CTraceContextPropagator.getInstance())
);
final OpenTelemetry openTelemetry = OpenTelemetry.propagating(ContextPropagators.create(W3CTraceContextPropagator.getInstance()));

final Tracer otelTracer = openTelemetry.getTracer("example-lib");

Expand All @@ -121,7 +127,7 @@ public void receiveRequestShouldReturnAParentedSpanIfPolicyIsPropagateAndTheOtel
vertx.runOnContext(unused -> {
parentSpan.makeCurrent();

VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(openTelemetry).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer(openTelemetry);

final Operation operation = tracer.receiveRequest(
vertx.getOrCreateContext(),
Expand All @@ -147,7 +153,7 @@ public void receiveRequestShouldReturnAParentedSpanIfPolicyIsPropagateAndTheOtel

@Test
public void sendResponseEndsSpan(final Vertx vertx) {
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer();

Span span = mock(Span.class);
doNothing().when(span).end();
Expand All @@ -167,7 +173,7 @@ public void sendResponseEndsSpan(final Vertx vertx) {

@Test
public void sendResponseShouldNotThrowExceptionWhenSpanIsNull(final Vertx vertx) {
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer();

assertThatNoException().isThrownBy(() -> tracer.sendResponse(
vertx.getOrCreateContext(),
Expand All @@ -180,7 +186,7 @@ public void sendResponseShouldNotThrowExceptionWhenSpanIsNull(final Vertx vertx)

@Test
public void sendRequestShouldNotReturnSpanIfRequestIsNull(final Vertx vertx) {
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer();

final Context ctx = vertx.getOrCreateContext();
VertxContextStorage.INSTANCE.attach(io.opentelemetry.context.Context.current());
Expand All @@ -201,7 +207,7 @@ public void sendRequestShouldNotReturnSpanIfRequestIsNull(final Vertx vertx) {

@Test
public void sendRequestShouldNotReturnSpanIfPolicyIsIgnore(final Vertx vertx) {
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer();

final Context ctx = vertx.getOrCreateContext();
VertxContextStorage.INSTANCE.attach(io.opentelemetry.context.Context.current());
Expand All @@ -223,7 +229,7 @@ public void sendRequestShouldNotReturnSpanIfPolicyIsIgnore(final Vertx vertx) {

@Test
public void sendRequestShouldNotReturnSpanIfPolicyIsPropagateAndPreviousContextIsNotPresent(final Vertx vertx) {
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer();

final Operation operation = tracer.sendRequest(
vertx.getOrCreateContext(),
Expand All @@ -241,7 +247,7 @@ public void sendRequestShouldNotReturnSpanIfPolicyIsPropagateAndPreviousContextI

@Test
public void sendRequestShouldReturnSpanIfPolicyIsPropagateAndPreviousContextIsPresent(final Vertx vertx) {
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer();

final Context ctx = vertx.getOrCreateContext();
VertxContextStorage.INSTANCE.attach((ContextInternal) ctx, io.opentelemetry.context.Context.current());
Expand All @@ -262,7 +268,7 @@ public void sendRequestShouldReturnSpanIfPolicyIsPropagateAndPreviousContextIsPr

@Test
public void sendRequestShouldReturnSpanIfPolicyIsAlwaysAndPreviousContextIsNotPresent(final Vertx vertx) {
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer();

final Context ctx = vertx.getOrCreateContext();

Expand All @@ -282,7 +288,7 @@ public void sendRequestShouldReturnSpanIfPolicyIsAlwaysAndPreviousContextIsNotPr

@Test
public void receiveResponseEndsSpan(final Vertx vertx) {
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer();

Span span = mock(Span.class);
doNothing().when(span).end();
Expand All @@ -302,7 +308,7 @@ public void receiveResponseEndsSpan(final Vertx vertx) {

@Test
public void receiveResponseShouldNotThrowExceptionWhenSpanIsNull(final Vertx vertx) {
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
VertxTracer<Operation, Operation> tracer = buildTracer();

assertThatNoException().isThrownBy(() -> tracer.receiveResponse(
vertx.getOrCreateContext(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
import io.opentelemetry.sdk.trace.data.SpanData;
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;
Expand All @@ -27,7 +26,7 @@
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.Tuple;
import io.vertx.tracing.opentelemetry.OpenTelemetryOptions;
import io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand Down Expand Up @@ -75,7 +74,10 @@ public static void stopDB() {

@BeforeEach
public void before() {
vertx = Vertx.vertx(new VertxOptions().setTracingOptions(new OpenTelemetryOptions(otelTesting.getOpenTelemetry())));
vertx = Vertx
.builder()
.withTracer(new OpenTelemetryTracingFactory(otelTesting.getOpenTelemetry()))
.build();
pool = PgBuilder.pool().connectingTo(connectOptions).using(vertx).build();
}

Expand Down
Loading