Skip to content

Commit

Permalink
Always create a duplicated context in OpenTelemetry when executing cl…
Browse files Browse the repository at this point in the history
…ient requests
  • Loading branch information
radcortez committed Jun 23, 2022
1 parent 853d8d7 commit 6597ccf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_TARGET;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_URL;
import static java.net.HttpURLConnection.HTTP_OK;
import static java.util.stream.Collectors.toSet;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.net.URI;
Expand All @@ -27,6 +28,8 @@
import io.quarkus.runtime.StartupEvent;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.test.common.http.TestHTTPResource;
import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.ext.web.Router;
Expand Down Expand Up @@ -114,14 +117,40 @@ void path() throws Exception {
assertEquals(server.getParentSpanId(), client.getSpanId());
}

@Test
void multiple() throws Exception {
HttpResponse<Buffer> response = WebClient.create(vertx)
.get(uri.getPort(), uri.getHost(), "/multiple")
.putHeader("host", uri.getHost())
.putHeader("port", uri.getPort() + "")
.send()
.toCompletionStage().toCompletableFuture()
.get();

assertEquals(HTTP_OK, response.statusCode());

List<SpanData> spans = spanExporter.getFinishedSpanItems(6);
assertEquals(1, spans.stream().map(SpanData::getTraceId).collect(toSet()).size());
}

@ApplicationScoped
public static class HelloRouter {
@Inject
Router router;
@Inject
Vertx vertx;

public void register(@Observes StartupEvent ev) {
router.get("/hello").handler(rc -> rc.response().end("hello"));
router.get("/hello/:name").handler(rc -> rc.response().end("hello " + rc.pathParam("name")));
router.get("/multiple").handler(rc -> {
String host = rc.request().getHeader("host");
int port = Integer.parseInt(rc.request().getHeader("port"));
WebClient webClient = WebClient.create(vertx);
Future<HttpResponse<Buffer>> one = webClient.get(port, host, "/hello/naruto").send();
Future<HttpResponse<Buffer>> two = webClient.get(port, host, "/hello/goku").send();
CompositeFuture.join(one, two).onComplete(event -> rc.response().end());
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ default <R> SpanOperation sendRequest(
if (instrumenter.shouldStart(parentContext, (REQ) request)) {
io.opentelemetry.context.Context spanContext = instrumenter.start(parentContext,
writableHeaders((REQ) request, headers));
Context duplicatedContext = VertxContext.getOrCreateDuplicatedContext(context);
Context duplicatedContext = VertxContext.createNewDuplicatedContext();
setContextSafe(duplicatedContext, true);
Scope scope = QuarkusContextStorage.INSTANCE.attach(duplicatedContext, spanContext);
return spanOperation(duplicatedContext, (REQ) request, toMultiMap(headers), spanContext, scope);
Expand Down

0 comments on commit 6597ccf

Please sign in to comment.