Skip to content

Commit

Permalink
Merge pull request #22671 from radcortez/fix-22568
Browse files Browse the repository at this point in the history
Copy duplicated local context to the event loop context
  • Loading branch information
radcortez authored Jan 7, 2022
2 parents dfa0f56 + 55e7587 commit 560d8da
Show file tree
Hide file tree
Showing 11 changed files with 486 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.opentelemetry.context.ContextStorage;
import io.opentelemetry.context.Scope;
import io.vertx.core.Vertx;
import io.vertx.core.impl.ContextInternal;

public enum QuarkusContextStorage implements ContextStorage {
INSTANCE;
Expand Down Expand Up @@ -40,6 +41,7 @@ public Scope attach(io.vertx.core.Context vertxContext, Context toAttach) {
}
if (beforeAttach == null) {
vertxContext.removeLocal(ACTIVE_CONTEXT);
((ContextInternal) vertxContext).unwrap().removeLocal(ACTIVE_CONTEXT);
} else {
vertxContext.putLocal(ACTIVE_CONTEXT, beforeAttach);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.netty.util.concurrent.ScheduledFuture;
import io.vertx.core.AsyncResult;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
Expand Down Expand Up @@ -60,6 +61,8 @@ public VertxResteasyReactiveRequestContext(Deployment deployment, ProvidersImpl
context.addHeadersEndHandler(this);
String expect = request.getHeader(HttpHeaderNames.EXPECT);
ContextInternal internal = ((ConnectionBase) context.request().connection()).getContext();
ContextInternal current = (ContextInternal) Vertx.currentContext();
internal.localContextData().putAll(current.localContextData());
if (expect != null && expect.equalsIgnoreCase(CONTINUE)) {
continueState = ContinueState.REQUIRED;
}
Expand Down
177 changes: 177 additions & 0 deletions integration-tests/opentelemetry-reactive/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-integration-tests-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>

<artifactId>quarkus-integration-test-opentelemetry-reactive</artifactId>
<name>Quarkus - Integration Tests - OpenTelemetry Reactive</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-opentelemetry</artifactId>
</dependency>

<!-- JAX-RS -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>

<!-- JAX-RS Client -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-reactive</artifactId>
</dependency>

<!-- Needed for InMemorySpanExporter to verify captured traces -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>

<!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-reactive-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-opentelemetry-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>

<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${native.surefire.skip}</skipTests>
</configuration>
</plugin>

<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.quarkus.it.opentelemetry.reactive;

import static java.util.Comparator.comparingLong;

import java.util.List;
import java.util.stream.Collectors;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.opentelemetry.sdk.trace.data.SpanData;

@Path("")
public class ExporterResource {
@Inject
InMemorySpanExporter inMemorySpanExporter;

@GET
@Path("/reset")
public Response reset() {
inMemorySpanExporter.reset();
return Response.ok().build();
}

@GET
@Path("/export")
public List<SpanData> export() {
return inMemorySpanExporter.getFinishedSpanItems()
.stream()
.filter(sd -> !sd.getName().contains("export") && !sd.getName().contains("reset"))
.sorted(comparingLong(SpanData::getStartEpochNanos).reversed())
.collect(Collectors.toList());
}

@ApplicationScoped
static class InMemorySpanExporterProducer {
@Produces
@Singleton
InMemorySpanExporter inMemorySpanExporter() {
return InMemorySpanExporter.create();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.it.opentelemetry.reactive;

import java.time.Duration;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.smallrye.mutiny.Uni;

@Path("/reactive")
public class ReactiveResource {
@Inject
Tracer tracer;

@GET
public Uni<String> helloGet(@QueryParam("name") String name) {
Span span = tracer.spanBuilder("helloGet").startSpan();
return Uni.createFrom().item("Hello " + name).onItem().delayIt().by(Duration.ofSeconds(2))
.eventually((Runnable) span::end);
}

@POST
public Uni<String> helloPost(String body) {
Span span = tracer.spanBuilder("helloPost").startSpan();
return Uni.createFrom().item("Hello " + body).onItem().delayIt().by(Duration.ofSeconds(2))
.eventually((Runnable) span::end);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.it.opentelemetry.reactive;

import io.quarkus.runtime.annotations.RegisterForReflection;

@RegisterForReflection(classNames = {
"io.opentelemetry.sdk.trace.data.SpanData",
"io.opentelemetry.sdk.trace.SpanWrapper",
"io.opentelemetry.sdk.trace.AutoValue_SpanWrapper",
"io.opentelemetry.sdk.trace.data.StatusData",
"io.opentelemetry.sdk.trace.data.ImmutableStatusData",
"io.opentelemetry.sdk.trace.data.AutoValue_ImmutableStatusData",
"io.opentelemetry.api.trace.SpanContext",
"io.opentelemetry.api.internal.ImmutableSpanContext",
"io.opentelemetry.api.internal.AutoValue_ImmutableSpanContext",
"io.opentelemetry.api.trace.TraceFlags",
"io.opentelemetry.api.trace.ImmutableTraceFlags",
"io.opentelemetry.api.trace.TraceState",
"io.opentelemetry.api.trace.ArrayBasedTraceState",
"io.opentelemetry.api.trace.AutoValue_ArrayBasedTraceState",
"io.opentelemetry.sdk.common.InstrumentationLibraryInfo",
"io.opentelemetry.sdk.common.AutoValue_InstrumentationLibraryInfo",
"io.opentelemetry.sdk.resources.Resource",
"io.opentelemetry.sdk.resources.AutoValue_Resource",
"io.opentelemetry.api.common.Attributes",
"io.quarkus.opentelemetry.runtime.tracing.DelayedAttributes"
})
public class SpanData {
}
Loading

0 comments on commit 560d8da

Please sign in to comment.