Skip to content

Commit

Permalink
OpenTelemetry: fine-grained instrumentation enablement
Browse files Browse the repository at this point in the history
- resolves quarkusio#35912
  • Loading branch information
mkouba committed Jan 8, 2024
1 parent 3b727fa commit 42ffdef
Show file tree
Hide file tree
Showing 11 changed files with 414 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.exporter.otlp.internal.OtlpSpanExporterProvider;
import io.opentelemetry.instrumentation.annotations.AddingSpanAttributes;
import io.opentelemetry.instrumentation.annotations.SpanAttribute;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigurablePropagatorProvider;
Expand All @@ -47,6 +46,8 @@
import io.quarkus.arc.processor.InterceptorBindingRegistrar;
import io.quarkus.arc.processor.Transformation;
import io.quarkus.datasource.common.runtime.DataSourceUtil;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
Expand Down Expand Up @@ -92,7 +93,6 @@ public boolean test(AnnotationInstance annotationInstance) {
private static final DotName WITH_SPAN_INTERCEPTOR = DotName.createSimple(WithSpanInterceptor.class.getName());
private static final DotName ADD_SPAN_ATTRIBUTES_INTERCEPTOR = DotName
.createSimple(AddingSpanAttributesInterceptor.class.getName());
private static final DotName SPAN_ATTRIBUTE = DotName.createSimple(SpanAttribute.class.getName());

@BuildStep
AdditionalBeanBuildItem ensureProducerIsRetained() {
Expand Down Expand Up @@ -263,10 +263,14 @@ void createOpenTelemetry(

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
void setupVertx(InstrumentationRecorder recorder,
BeanContainerBuildItem beanContainerBuildItem) {

recorder.setupVertxTracer(beanContainerBuildItem.getValue());
void setupVertx(InstrumentationRecorder recorder, BeanContainerBuildItem beanContainerBuildItem,
Capabilities capabilities) {
boolean sqlClientAvailable = capabilities.isPresent(Capability.REACTIVE_DB2_CLIENT)
|| capabilities.isPresent(Capability.REACTIVE_MSSQL_CLIENT)
|| capabilities.isPresent(Capability.REACTIVE_MYSQL_CLIENT)
|| capabilities.isPresent(Capability.REACTIVE_ORACLE_CLIENT)
|| capabilities.isPresent(Capability.REACTIVE_PG_CLIENT);
recorder.setupVertxTracer(beanContainerBuildItem.getValue(), sqlClientAvailable);
}

@BuildStep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.AdditionalIndexedClassesBuildItem;
import io.quarkus.opentelemetry.deployment.tracing.TracerEnabled;
import io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig;
import io.quarkus.opentelemetry.runtime.tracing.intrumentation.InstrumentationRecorder;
import io.quarkus.opentelemetry.runtime.tracing.intrumentation.grpc.GrpcTracingClientInterceptor;
import io.quarkus.opentelemetry.runtime.tracing.intrumentation.grpc.GrpcTracingServerInterceptor;
Expand Down Expand Up @@ -71,17 +72,21 @@ public boolean getAsBoolean() {
}

@BuildStep(onlyIf = GrpcExtensionAvailable.class)
void grpcTracers(BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
additionalBeans.produce(new AdditionalBeanBuildItem(GrpcTracingServerInterceptor.class));
additionalBeans.produce(new AdditionalBeanBuildItem(GrpcTracingClientInterceptor.class));
void grpcTracers(BuildProducer<AdditionalBeanBuildItem> additionalBeans, OTelBuildConfig config) {
if (config.instrument().grpc()) {
additionalBeans.produce(new AdditionalBeanBuildItem(GrpcTracingServerInterceptor.class));
additionalBeans.produce(new AdditionalBeanBuildItem(GrpcTracingClientInterceptor.class));
}
}

@BuildStep
void registerRestClientClassicProvider(
Capabilities capabilities,
BuildProducer<AdditionalIndexedClassesBuildItem> additionalIndexed,
BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
if (capabilities.isPresent(Capability.REST_CLIENT) && capabilities.isMissing(Capability.REST_CLIENT_REACTIVE)) {
BuildProducer<AdditionalBeanBuildItem> additionalBeans,
OTelBuildConfig config) {
if (capabilities.isPresent(Capability.REST_CLIENT) && capabilities.isMissing(Capability.REST_CLIENT_REACTIVE)
&& config.instrument().restClientClassic()) {
additionalIndexed.produce(new AdditionalIndexedClassesBuildItem(OpenTelemetryClientFilter.class.getName()));
additionalBeans.produce(new AdditionalBeanBuildItem(OpenTelemetryClientFilter.class));
}
Expand All @@ -90,8 +95,9 @@ void registerRestClientClassicProvider(
@BuildStep
void registerReactiveMessagingMessageDecorator(
Capabilities capabilities,
BuildProducer<AdditionalBeanBuildItem> additionalBeans) {
if (capabilities.isPresent(Capability.SMALLRYE_REACTIVE_MESSAGING)) {
BuildProducer<AdditionalBeanBuildItem> additionalBeans,
OTelBuildConfig config) {
if (capabilities.isPresent(Capability.SMALLRYE_REACTIVE_MESSAGING) && config.instrument().reactiveMessaging()) {
additionalBeans.produce(new AdditionalBeanBuildItem(ReactiveMessagingTracingOutgoingDecorator.class));
additionalBeans.produce(new AdditionalBeanBuildItem(ReactiveMessagingTracingIncomingDecorator.class));
additionalBeans.produce(new AdditionalBeanBuildItem(ReactiveMessagingTracingEmitterDecorator.class));
Expand All @@ -115,35 +121,27 @@ VertxOptionsConsumerBuildItem vertxTracingOptions(

// RESTEasy and Vert.x web
@BuildStep
void registerResteasyClassicAndOrResteasyReactiveProvider(
void registerResteasyClassicAndOrResteasyReactiveProvider(OTelBuildConfig config,
Capabilities capabilities,
BuildProducer<ResteasyJaxrsProviderBuildItem> resteasyJaxrsProviderBuildItemBuildProducer) {

boolean isResteasyClassicAvailable = capabilities.isPresent(Capability.RESTEASY);

if (!isResteasyClassicAvailable) {
// if RestEasy is not available then no need to continue
return;
if (capabilities.isPresent(Capability.RESTEASY) && config.instrument().resteasyClassic()) {
resteasyJaxrsProviderBuildItemBuildProducer
.produce(new ResteasyJaxrsProviderBuildItem(OpenTelemetryClassicServerFilter.class.getName()));
}

resteasyJaxrsProviderBuildItemBuildProducer
.produce(new ResteasyJaxrsProviderBuildItem(OpenTelemetryClassicServerFilter.class.getName()));
}

@BuildStep
void resteasyReactiveIntegration(
Capabilities capabilities,
BuildProducer<CustomContainerRequestFilterBuildItem> containerRequestFilterBuildItemBuildProducer,
BuildProducer<PreExceptionMapperHandlerBuildItem> preExceptionMapperHandlerBuildItemBuildProducer) {

if (!capabilities.isPresent(Capability.RESTEASY_REACTIVE)) {
// if RESTEasy Reactive is not available then no need to continue
return;
BuildProducer<PreExceptionMapperHandlerBuildItem> preExceptionMapperHandlerBuildItemBuildProducer,
OTelBuildConfig config) {
if (capabilities.isPresent(Capability.RESTEASY_REACTIVE) && config.instrument().resteasyReactive()) {
containerRequestFilterBuildItemBuildProducer
.produce(new CustomContainerRequestFilterBuildItem(OpenTelemetryReactiveServerFilter.class.getName()));
preExceptionMapperHandlerBuildItemBuildProducer
.produce(new PreExceptionMapperHandlerBuildItem(new AttachExceptionHandler()));
}

containerRequestFilterBuildItemBuildProducer
.produce(new CustomContainerRequestFilterBuildItem(OpenTelemetryReactiveServerFilter.class.getName()));
preExceptionMapperHandlerBuildItemBuildProducer
.produce(new PreExceptionMapperHandlerBuildItem(new AttachExceptionHandler()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public List<SpanData> getFinishedSpanItems(int spanCount) {
}

public void assertSpanCount(int spanCount) {
await().atMost(30, SECONDS).untilAsserted(() -> assertEquals(spanCount, finishedSpanItems.size()));
await().atMost(30, SECONDS).untilAsserted(
() -> assertEquals(spanCount, finishedSpanItems.size(), "Spans: " + finishedSpanItems.toString()));
}

public void reset() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package io.quarkus.opentelemetry.deployment.instrumentation;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.api.trace.SpanKind.INTERNAL;
import static io.quarkus.opentelemetry.deployment.common.TestSpanExporter.getSpanByKindAndParentId;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.Duration;
import java.util.List;

import jakarta.inject.Inject;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.quarkus.grpc.GrpcClient;
import io.quarkus.grpc.GrpcService;
import io.quarkus.opentelemetry.deployment.Greeter;
import io.quarkus.opentelemetry.deployment.GreeterBean;
import io.quarkus.opentelemetry.deployment.GreeterClient;
import io.quarkus.opentelemetry.deployment.GreeterGrpc;
import io.quarkus.opentelemetry.deployment.HelloProto;
import io.quarkus.opentelemetry.deployment.HelloReply;
import io.quarkus.opentelemetry.deployment.HelloReplyOrBuilder;
import io.quarkus.opentelemetry.deployment.HelloRequest;
import io.quarkus.opentelemetry.deployment.HelloRequestOrBuilder;
import io.quarkus.opentelemetry.deployment.MutinyGreeterGrpc;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporter;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider;
import io.quarkus.test.QuarkusUnitTest;
import io.smallrye.mutiny.Uni;

public class GrpcOpenInstrumentationDisabledTest {

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.withApplicationRoot(root -> root
.addClasses(TestSpanExporter.class, TestSpanExporterProvider.class)
.addClasses(HelloService.class)
.addClasses(GreeterGrpc.class, MutinyGreeterGrpc.class,
Greeter.class, GreeterBean.class, GreeterClient.class,
HelloProto.class, HelloRequest.class, HelloRequestOrBuilder.class,
HelloReply.class, HelloReplyOrBuilder.class)
.addAsResource(new StringAsset(TestSpanExporterProvider.class.getCanonicalName()),
"META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider"))
.withConfigurationResource("application-default.properties")
.overrideConfigKey("quarkus.grpc.clients.hello.host", "localhost")
.overrideConfigKey("quarkus.grpc.clients.hello.port", "9001")
.overrideConfigKey("quarkus.otel.instrument.grpc", "false");

@Inject
TestSpanExporter spanExporter;

@GrpcClient
Greeter hello;

@AfterEach
void tearDown() {
spanExporter.reset();
}

@Test
void testTratestTracingDisabled() {
String response = hello.sayHello(
HelloRequest.newBuilder().setName("ping").build())
.map(HelloReply::getMessage)
.await().atMost(Duration.ofSeconds(5));
assertEquals("Hello ping", response);

List<SpanData> spans = spanExporter.getFinishedSpanItems(1);
assertEquals(1, spans.size());

SpanData internal = getSpanByKindAndParentId(spans, INTERNAL, "0000000000000000");
assertEquals("span.internal", internal.getName());
assertEquals("value", internal.getAttributes().get(stringKey("grpc.internal")));
}

@GrpcService
public static class HelloService implements Greeter {

@Inject
Tracer tracer;

@Override
public Uni<HelloReply> sayHello(HelloRequest request) {
Span span = tracer.spanBuilder("span.internal")
.setSpanKind(INTERNAL)
.setAttribute("grpc.internal", "value")
.startSpan();
span.end();
return Uni.createFrom().item(HelloReply.newBuilder().setMessage("Hello " + request.getName()).build());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package io.quarkus.opentelemetry.deployment.instrumentation;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.api.trace.SpanKind.INTERNAL;
import static io.quarkus.opentelemetry.deployment.common.TestSpanExporter.getSpanByKindAndParentId;
import static java.net.HttpURLConnection.HTTP_OK;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.util.List;

import jakarta.enterprise.event.Observes;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporter;
import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider;
import io.quarkus.opentelemetry.deployment.common.TestUtil;
import io.quarkus.test.QuarkusUnitTest;
import io.quarkus.vertx.ConsumeEvent;
import io.restassured.RestAssured;
import io.vertx.core.eventbus.EventBus;
import io.vertx.ext.web.Router;

public class VertxEventBusInstrumentationDisabledTest {

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.withApplicationRoot(root -> root
.addClasses(Events.class, TestUtil.class, TestSpanExporter.class, TestSpanExporterProvider.class)
.addAsResource(new StringAsset(TestSpanExporterProvider.class.getCanonicalName()),
"META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider"))
.overrideConfigKey("quarkus.otel.traces.exporter", "test-span-exporter")
.overrideConfigKey("quarkus.otel.metrics.exporter", "none")
.overrideConfigKey("quarkus.otel.logs.exporter", "none")
.overrideConfigKey("quarkus.otel.bsp.schedule.delay", "200")
.overrideConfigKey("quarkus.otel.instrument.vertx-event-bus", "false");

@Inject
TestSpanExporter spanExporter;

@AfterEach
void tearDown() {
spanExporter.reset();
}

@Test
void testTracingDisabled() throws Exception {

RestAssured.when().get("/hello/event")
.then()
.statusCode(HTTP_OK)
.body(equalTo("BAR"));

// http request and dummy
List<SpanData> spans = spanExporter.getFinishedSpanItems(2);
assertEquals(2, spans.size());

SpanData internal = getSpanByKindAndParentId(spans, INTERNAL, "0000000000000000");
assertEquals("io.quarkus.vertx.opentelemetry", internal.getName());
assertEquals("dummy", internal.getAttributes().get(stringKey("test.message")));
}

@Singleton
public static class Events {

@Inject
Tracer tracer;

@ConsumeEvent("foo")
String echo(String foo) {
tracer.spanBuilder("io.quarkus.vertx.opentelemetry").startSpan()
.setAttribute("test.message", "dummy")
.end();
return foo.toUpperCase();
}

void registerRoutes(@Observes Router router, EventBus eventBus) {
router.get("/hello/event").handler(rc -> {
eventBus.request("foo", "bar").onComplete(r -> rc.end(r.result().body().toString()));
});
}
}

}
Loading

0 comments on commit 42ffdef

Please sign in to comment.