-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30478 from OpenGuidou/OTEL-CP
Allow context propagation for OpenTelemetry
- Loading branch information
Showing
5 changed files
with
151 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...rkus/opentelemetry/deployment/propagation/OpenTelemetryMpContextPropagationProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package io.quarkus.opentelemetry.deployment.propagation; | ||
|
||
import io.quarkus.deployment.annotations.BuildProducer; | ||
import io.quarkus.deployment.annotations.BuildStep; | ||
import io.quarkus.deployment.annotations.BuildSteps; | ||
import io.quarkus.opentelemetry.deployment.OpenTelemetryEnabled; | ||
import io.quarkus.opentelemetry.runtime.propagation.OpenTelemetryMpContextPropagationProvider; | ||
import io.quarkus.smallrye.context.deployment.spi.ThreadContextProviderBuildItem; | ||
|
||
@BuildSteps(onlyIf = OpenTelemetryEnabled.class) | ||
public class OpenTelemetryMpContextPropagationProcessor { | ||
|
||
@BuildStep | ||
void registerOpenTelemetryThreadProvider( | ||
BuildProducer<ThreadContextProviderBuildItem> threadContextProvider) { | ||
threadContextProvider.produce(new ThreadContextProviderBuildItem(OpenTelemetryMpContextPropagationProvider.class)); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...o/quarkus/opentelemetry/deployment/propagation/OpenTelemetryMpContextPropagationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.quarkus.opentelemetry.deployment.propagation; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
|
||
import org.eclipse.microprofile.context.ThreadContext; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class OpenTelemetryMpContextPropagationTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest unitTest = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClass(OpenTelemetryMpContextPropagationTest.TestResource.class)); | ||
|
||
@Test | ||
void testOpenTelemetryContextPropagationWithCustomExecutorAndThreadContextProvider() { | ||
String message = RestAssured.when() | ||
.get("/helloWithContextPropagation").then() | ||
.statusCode(200) | ||
.extract().asString(); | ||
assertTrue(message.startsWith("Hello/")); | ||
String[] traceIds = message.split("/")[1].split("-"); | ||
assertEquals(2, traceIds.length); | ||
assertEquals(traceIds[0], traceIds[1]); | ||
} | ||
|
||
@ApplicationScoped | ||
@Path("/") | ||
public static class TestResource { | ||
|
||
private final ExecutorService customExecutorService; | ||
|
||
private final ThreadContext threadContext; | ||
|
||
@Inject | ||
TestResource(ThreadContext threadContext) { | ||
this.customExecutorService = Executors.newWorkStealingPool(); | ||
this.threadContext = threadContext; | ||
} | ||
|
||
@GET | ||
@Path("/helloWithContextPropagation") | ||
public CompletionStage<String> helloWithCustomExecutor() { | ||
String message = "Hello/" + Span.current().getSpanContext().getTraceId(); | ||
return this.threadContext | ||
.withContextCapture(CompletableFuture.supplyAsync( | ||
() -> message, this.customExecutorService)) | ||
.thenApplyAsync(msg -> msg + "-" + Span.current().getSpanContext().getTraceId()); | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
.../quarkus/opentelemetry/runtime/propagation/OpenTelemetryMpContextPropagationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.quarkus.opentelemetry.runtime.propagation; | ||
|
||
import java.util.Map; | ||
|
||
import org.eclipse.microprofile.context.spi.ThreadContextController; | ||
import org.eclipse.microprofile.context.spi.ThreadContextProvider; | ||
import org.eclipse.microprofile.context.spi.ThreadContextSnapshot; | ||
|
||
import io.quarkus.opentelemetry.runtime.QuarkusContextStorage; | ||
|
||
public class OpenTelemetryMpContextPropagationProvider implements ThreadContextProvider { | ||
|
||
@Override | ||
public ThreadContextSnapshot currentContext(Map<String, String> props) { | ||
|
||
io.opentelemetry.context.Context context = QuarkusContextStorage.INSTANCE.current(); | ||
|
||
// Use anonymous classes instad of lambdas for the native image | ||
return new ThreadContextSnapshot() { | ||
|
||
@Override | ||
public ThreadContextController begin() { | ||
io.opentelemetry.context.Context currentContext = QuarkusContextStorage.INSTANCE.current(); | ||
if (context != null) { | ||
QuarkusContextStorage.INSTANCE.attach(context); | ||
return new ThreadContextController() { | ||
@Override | ||
public void endContext() throws IllegalStateException { | ||
QuarkusContextStorage.INSTANCE.attach(currentContext); | ||
} | ||
}; | ||
} | ||
return new ThreadContextController() { | ||
@Override | ||
public void endContext() throws IllegalStateException { | ||
// nothing to do | ||
} | ||
}; | ||
} | ||
|
||
}; | ||
} | ||
|
||
@Override | ||
public ThreadContextSnapshot clearedContext(Map<String, String> props) { | ||
// Use anonymous classes instad of lambdas for the native image | ||
return new ThreadContextSnapshot() { | ||
@Override | ||
public ThreadContextController begin() { | ||
return new ThreadContextController() { | ||
@Override | ||
public void endContext() throws IllegalStateException { | ||
// nothring to do | ||
} | ||
}; | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public String getThreadContextType() { | ||
return "OpenTelemetry"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters