Skip to content

Commit 23223b5

Browse files
authored
In Vert.x 5, custom tracers should be setup with the Vert.x builder (#98)
* Fix OpenTelemetry setup for Vert.x 5 - Export package in module-info.java for modularized apps - Option class should no longer let users configure OpenTelemetry - Factory class should let users provide a custom OpenTelemetry instance Signed-off-by: Thomas Segismont <[email protected]> * Fix Zipkin setup for Vert.x 5 - Export package in module-info.java for modularized apps - Option class should no longer let users configure Zipkin classes - Factory class should let users provide custom Zipkin instance Signed-off-by: Thomas Segismont <[email protected]> --------- Signed-off-by: Thomas Segismont <[email protected]>
1 parent 7d98b12 commit 23223b5

File tree

15 files changed

+148
-128
lines changed

15 files changed

+148
-128
lines changed

vertx-opentelemetry/src/main/java/examples/OpenTelemetryExamples.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
55
import io.opentelemetry.context.propagation.ContextPropagators;
66
import io.opentelemetry.sdk.OpenTelemetrySdk;
7-
import io.opentelemetry.sdk.trace.*;
7+
import io.opentelemetry.sdk.trace.SdkTracerProvider;
88
import io.vertx.core.Vertx;
99
import io.vertx.core.VertxOptions;
1010
import io.vertx.core.eventbus.DeliveryOptions;
@@ -15,6 +15,7 @@
1515
import io.vertx.core.tracing.TracingPolicy;
1616
import io.vertx.docgen.Source;
1717
import io.vertx.tracing.opentelemetry.OpenTelemetryOptions;
18+
import io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory;
1819

1920
@Source
2021
public class OpenTelemetryExamples {
@@ -28,11 +29,10 @@ public void ex1() {
2829
}
2930

3031
public void ex2(OpenTelemetry openTelemetry) {
31-
Vertx vertx = Vertx.vertx(new VertxOptions()
32-
.setTracingOptions(
33-
new OpenTelemetryOptions(openTelemetry)
34-
)
35-
);
32+
Vertx vertx = Vertx
33+
.builder()
34+
.withTracer(new OpenTelemetryTracingFactory(openTelemetry))
35+
.build();
3636
}
3737

3838
public void ex3(Vertx vertx) {
@@ -61,13 +61,16 @@ public void ex6(Vertx vertx) {
6161
vertx.eventBus().send("the-address", "foo", options);
6262
}
6363

64-
public void ex7(VertxOptions vertxOptions) {
64+
public void ex7() {
6565
SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder().build();
6666
OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
6767
.setTracerProvider(sdkTracerProvider)
6868
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
6969
.buildAndRegisterGlobal();
7070

71-
vertxOptions.setTracingOptions(new OpenTelemetryOptions(openTelemetry));
71+
Vertx vertx = Vertx
72+
.builder()
73+
.withTracer(new OpenTelemetryTracingFactory(openTelemetry))
74+
.build();
7275
}
7376
}

vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/OpenTelemetryOptions.java

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,25 @@
1010
*/
1111
package io.vertx.tracing.opentelemetry;
1212

13-
import io.opentelemetry.api.GlobalOpenTelemetry;
14-
import io.opentelemetry.api.OpenTelemetry;
1513
import io.vertx.codegen.annotations.DataObject;
1614
import io.vertx.core.json.JsonObject;
17-
import io.vertx.core.spi.tracing.VertxTracer;
1815
import io.vertx.core.tracing.TracingOptions;
1916

2017
@DataObject
2118
public class OpenTelemetryOptions extends TracingOptions {
2219

23-
private OpenTelemetry openTelemetry;
24-
25-
public OpenTelemetryOptions(OpenTelemetry openTelemetry) {
26-
this.openTelemetry = openTelemetry;
20+
public OpenTelemetryOptions() {
2721
}
2822

29-
public OpenTelemetryOptions() {
23+
public OpenTelemetryOptions(OpenTelemetryOptions other) {
3024
}
3125

3226
public OpenTelemetryOptions(JsonObject json) {
3327
super(json);
3428
}
3529

36-
public VertxTracer<Operation, Operation> buildTracer() {
37-
if (openTelemetry != null) {
38-
return new OpenTelemetryTracer(openTelemetry);
39-
} else {
40-
return new OpenTelemetryTracer(GlobalOpenTelemetry.get());
41-
}
30+
@Override
31+
public OpenTelemetryOptions copy() {
32+
return new OpenTelemetryOptions(this);
4233
}
43-
4434
}

vertx-opentelemetry/src/main/java/io/vertx/tracing/opentelemetry/OpenTelemetryTracingFactory.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*/
1111
package io.vertx.tracing.opentelemetry;
1212

13+
import io.opentelemetry.api.GlobalOpenTelemetry;
14+
import io.opentelemetry.api.OpenTelemetry;
1315
import io.opentelemetry.context.Context;
1416
import io.vertx.core.json.JsonObject;
1517
import io.vertx.core.spi.VertxTracerFactory;
@@ -21,24 +23,32 @@ public class OpenTelemetryTracingFactory implements VertxTracerFactory {
2123

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

26+
private final OpenTelemetry openTelemetry;
27+
28+
public OpenTelemetryTracingFactory() {
29+
this(null);
30+
}
31+
32+
public OpenTelemetryTracingFactory(OpenTelemetry openTelemetry) {
33+
this.openTelemetry = openTelemetry;
34+
}
35+
2436
@Override
2537
public VertxTracer<?, ?> tracer(final TracingOptions options) {
26-
OpenTelemetryOptions openTelemetryOptions;
27-
if (options instanceof OpenTelemetryOptions) {
28-
openTelemetryOptions = (OpenTelemetryOptions) options;
38+
if (openTelemetry != null) {
39+
return new OpenTelemetryTracer(openTelemetry);
2940
} else {
30-
openTelemetryOptions = new OpenTelemetryOptions(options.toJson());
41+
return new OpenTelemetryTracer(GlobalOpenTelemetry.get());
3142
}
32-
return openTelemetryOptions.buildTracer();
3343
}
3444

3545
@Override
36-
public TracingOptions newOptions() {
46+
public OpenTelemetryOptions newOptions() {
3747
return new OpenTelemetryOptions();
3848
}
3949

4050
@Override
41-
public TracingOptions newOptions(JsonObject jsonObject) {
51+
public OpenTelemetryOptions newOptions(JsonObject jsonObject) {
4252
return new OpenTelemetryOptions(jsonObject);
4353
}
4454
}

vertx-opentelemetry/src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
requires io.opentelemetry.sdk.trace;
2121
requires io.vertx.core;
2222

23+
exports io.vertx.tracing.opentelemetry;
24+
2325
provides io.opentelemetry.context.ContextStorageProvider with io.vertx.tracing.opentelemetry.VertxContextStorageProvider;
2426
provides io.vertx.core.spi.VertxServiceProvider with io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory;
2527

vertx-opentelemetry/src/test/java/io/vertx/tests/opentelemetry/EventBusTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@
1414
import io.opentelemetry.api.common.AttributeKey;
1515
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
1616
import io.opentelemetry.sdk.trace.data.SpanData;
17-
import io.vertx.core.*;
17+
import io.vertx.core.AbstractVerticle;
18+
import io.vertx.core.Future;
19+
import io.vertx.core.Promise;
20+
import io.vertx.core.Vertx;
1821
import io.vertx.core.eventbus.DeliveryOptions;
1922
import io.vertx.core.http.*;
2023
import io.vertx.core.tracing.TracingPolicy;
2124
import io.vertx.junit5.VertxExtension;
2225
import io.vertx.junit5.VertxTestContext;
23-
import io.vertx.tracing.opentelemetry.OpenTelemetryOptions;
26+
import io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory;
2427
import org.junit.jupiter.api.AfterEach;
2528
import org.junit.jupiter.api.BeforeEach;
2629
import org.junit.jupiter.api.Test;
@@ -44,7 +47,10 @@ public class EventBusTest {
4447

4548
@BeforeEach
4649
public void setUp() throws Exception {
47-
vertx = Vertx.vertx(new VertxOptions().setTracingOptions(new OpenTelemetryOptions(otelTesting.getOpenTelemetry())));
50+
vertx = Vertx
51+
.builder()
52+
.withTracer(new OpenTelemetryTracingFactory(otelTesting.getOpenTelemetry()))
53+
.build();
4854
client = vertx.createHttpClient(new HttpClientOptions().setDefaultPort(8080));
4955
}
5056

vertx-opentelemetry/src/test/java/io/vertx/tests/opentelemetry/OpenTelemetryIntegrationTest.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717
import io.opentelemetry.context.propagation.TextMapSetter;
1818
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
1919
import io.opentelemetry.sdk.trace.data.SpanData;
20-
import io.vertx.core.*;
20+
import io.vertx.core.DeploymentOptions;
21+
import io.vertx.core.Future;
22+
import io.vertx.core.ThreadingModel;
23+
import io.vertx.core.Vertx;
2124
import io.vertx.core.buffer.Buffer;
2225
import io.vertx.core.http.*;
2326
import io.vertx.core.tracing.TracingPolicy;
2427
import io.vertx.junit5.Checkpoint;
2528
import io.vertx.junit5.VertxExtension;
2629
import io.vertx.junit5.VertxTestContext;
27-
import io.vertx.tracing.opentelemetry.OpenTelemetryOptions;
30+
import io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory;
2831
import org.junit.jupiter.api.AfterEach;
2932
import org.junit.jupiter.api.Assertions;
3033
import org.junit.jupiter.api.BeforeEach;
@@ -62,7 +65,10 @@ public class OpenTelemetryIntegrationTest {
6265

6366
@BeforeEach
6467
public void setUp() throws Exception {
65-
vertx = Vertx.vertx(new VertxOptions().setTracingOptions(new OpenTelemetryOptions(otelTesting.getOpenTelemetry())));
68+
vertx = Vertx
69+
.builder()
70+
.withTracer(new OpenTelemetryTracingFactory(otelTesting.getOpenTelemetry()))
71+
.build();
6672
textMapPropagator = otelTesting.getOpenTelemetry().getPropagators().getTextMapPropagator();
6773
}
6874

vertx-opentelemetry/src/test/java/io/vertx/tests/opentelemetry/OpenTelemetryTracingFactoryTest.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.vertx.core.tracing.TracingPolicy;
2626
import io.vertx.junit5.VertxExtension;
2727
import io.vertx.tracing.opentelemetry.OpenTelemetryOptions;
28+
import io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory;
2829
import io.vertx.tracing.opentelemetry.Operation;
2930
import io.vertx.tracing.opentelemetry.VertxContextStorageProvider.VertxContextStorage;
3031
import org.junit.jupiter.api.Test;
@@ -46,7 +47,7 @@ public class OpenTelemetryTracingFactoryTest {
4647

4748
@Test
4849
public void receiveRequestShouldNotReturnSpanIfPolicyIsIgnore(final Vertx vertx) {
49-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
50+
VertxTracer<Operation, Operation> tracer = buildTracer();
5051

5152
final Operation operation = tracer.receiveRequest(
5253
vertx.getOrCreateContext(),
@@ -61,9 +62,18 @@ public void receiveRequestShouldNotReturnSpanIfPolicyIsIgnore(final Vertx vertx)
6162
assertThat(operation).isNull();
6263
}
6364

65+
static VertxTracer<Operation, Operation> buildTracer() {
66+
return buildTracer(OpenTelemetry.noop());
67+
}
68+
69+
@SuppressWarnings("unchecked")
70+
static VertxTracer<Operation, Operation> buildTracer(OpenTelemetry openTelemetry) {
71+
return (VertxTracer<Operation, Operation>) new OpenTelemetryTracingFactory(openTelemetry).tracer(new OpenTelemetryOptions());
72+
}
73+
6474
@Test
6575
public void receiveRequestShouldNotReturnSpanIfPolicyIsPropagateAndPreviousContextIsNotPresent(final Vertx vertx) {
66-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
76+
VertxTracer<Operation, Operation> tracer = buildTracer();
6777

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

8191
@Test
8292
public void receiveRequestShouldReturnSpanIfPolicyIsPropagateAndPreviousContextIsPresent(final Vertx vertx) {
83-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(
84-
OpenTelemetry.propagating(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
85-
).buildTracer();
93+
VertxTracer<Operation, Operation> tracer = buildTracer(OpenTelemetry.propagating(ContextPropagators.create(W3CTraceContextPropagator.getInstance())));
8694

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

108116
@Test
109117
public void receiveRequestShouldReturnAParentedSpanIfPolicyIsPropagateAndTheOtelContextHasAnOngoingSpan(final Vertx vertx) throws ExecutionException, InterruptedException {
110-
final OpenTelemetry openTelemetry = OpenTelemetry.propagating(
111-
ContextPropagators.create(W3CTraceContextPropagator.getInstance())
112-
);
118+
final OpenTelemetry openTelemetry = OpenTelemetry.propagating(ContextPropagators.create(W3CTraceContextPropagator.getInstance()));
113119

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

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

124-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(openTelemetry).buildTracer();
130+
VertxTracer<Operation, Operation> tracer = buildTracer(openTelemetry);
125131

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

148154
@Test
149155
public void sendResponseEndsSpan(final Vertx vertx) {
150-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
156+
VertxTracer<Operation, Operation> tracer = buildTracer();
151157

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

168174
@Test
169175
public void sendResponseShouldNotThrowExceptionWhenSpanIsNull(final Vertx vertx) {
170-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
176+
VertxTracer<Operation, Operation> tracer = buildTracer();
171177

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

181187
@Test
182188
public void sendRequestShouldNotReturnSpanIfRequestIsNull(final Vertx vertx) {
183-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
189+
VertxTracer<Operation, Operation> tracer = buildTracer();
184190

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

202208
@Test
203209
public void sendRequestShouldNotReturnSpanIfPolicyIsIgnore(final Vertx vertx) {
204-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
210+
VertxTracer<Operation, Operation> tracer = buildTracer();
205211

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

224230
@Test
225231
public void sendRequestShouldNotReturnSpanIfPolicyIsPropagateAndPreviousContextIsNotPresent(final Vertx vertx) {
226-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
232+
VertxTracer<Operation, Operation> tracer = buildTracer();
227233

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

242248
@Test
243249
public void sendRequestShouldReturnSpanIfPolicyIsPropagateAndPreviousContextIsPresent(final Vertx vertx) {
244-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
250+
VertxTracer<Operation, Operation> tracer = buildTracer();
245251

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

263269
@Test
264270
public void sendRequestShouldReturnSpanIfPolicyIsAlwaysAndPreviousContextIsNotPresent(final Vertx vertx) {
265-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
271+
VertxTracer<Operation, Operation> tracer = buildTracer();
266272

267273
final Context ctx = vertx.getOrCreateContext();
268274

@@ -282,7 +288,7 @@ public void sendRequestShouldReturnSpanIfPolicyIsAlwaysAndPreviousContextIsNotPr
282288

283289
@Test
284290
public void receiveResponseEndsSpan(final Vertx vertx) {
285-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
291+
VertxTracer<Operation, Operation> tracer = buildTracer();
286292

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

303309
@Test
304310
public void receiveResponseShouldNotThrowExceptionWhenSpanIsNull(final Vertx vertx) {
305-
VertxTracer<Operation, Operation> tracer = new OpenTelemetryOptions(OpenTelemetry.noop()).buildTracer();
311+
VertxTracer<Operation, Operation> tracer = buildTracer();
306312

307313
assertThatNoException().isThrownBy(() -> tracer.receiveResponse(
308314
vertx.getOrCreateContext(),

vertx-opentelemetry/src/test/java/io/vertx/tests/opentelemetry/SqlClientTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension;
1515
import io.opentelemetry.sdk.trace.data.SpanData;
1616
import io.vertx.core.Vertx;
17-
import io.vertx.core.VertxOptions;
1817
import io.vertx.core.http.HttpClient;
1918
import io.vertx.core.http.HttpClientOptions;
2019
import io.vertx.core.http.HttpMethod;
@@ -27,7 +26,7 @@
2726
import io.vertx.sqlclient.Row;
2827
import io.vertx.sqlclient.RowSet;
2928
import io.vertx.sqlclient.Tuple;
30-
import io.vertx.tracing.opentelemetry.OpenTelemetryOptions;
29+
import io.vertx.tracing.opentelemetry.OpenTelemetryTracingFactory;
3130
import org.junit.jupiter.api.*;
3231
import org.junit.jupiter.api.extension.ExtendWith;
3332
import org.junit.jupiter.api.extension.RegisterExtension;
@@ -75,7 +74,10 @@ public static void stopDB() {
7574

7675
@BeforeEach
7776
public void before() {
78-
vertx = Vertx.vertx(new VertxOptions().setTracingOptions(new OpenTelemetryOptions(otelTesting.getOpenTelemetry())));
77+
vertx = Vertx
78+
.builder()
79+
.withTracer(new OpenTelemetryTracingFactory(otelTesting.getOpenTelemetry()))
80+
.build();
7981
pool = PgBuilder.pool().connectingTo(connectOptions).using(vertx).build();
8082
}
8183

0 commit comments

Comments
 (0)