Skip to content

Commit

Permalink
Add OTEL Config Params
Browse files Browse the repository at this point in the history
  • Loading branch information
luneo7 committed Jun 16, 2021
1 parent b520fac commit 9fd7d49
Show file tree
Hide file tree
Showing 24 changed files with 816 additions and 22 deletions.
4 changes: 2 additions & 2 deletions bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
<opentracing-jdbc.version>0.2.12</opentracing-jdbc.version>
<opentracing-kafka.version>0.1.15</opentracing-kafka.version>
<opentracing-mongo.version>0.1.5</opentracing-mongo.version>
<opentelemetry.version>1.2.0</opentelemetry.version>
<opentelemetry-alpha.version>1.2.0-alpha</opentelemetry-alpha.version>
<opentelemetry.version>1.3.0</opentelemetry.version>
<opentelemetry-alpha.version>1.3.0-alpha</opentelemetry-alpha.version>
<jaeger.version>1.4.0</jaeger.version>
<quarkus-http.version>4.1.1</quarkus-http.version>
<jboss-servlet-api_4.0_spec.version>1.0.0.Final</jboss-servlet-api_4.0_spec.version>
Expand Down
16 changes: 16 additions & 0 deletions extensions/opentelemetry/opentelemetry/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-extension-aws</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-aws</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-extension-resources</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,17 @@ void registerOpenTelemetryContextStorage(

@BuildStep(onlyIf = OpenTelemetryEnabled.class)
@Record(ExecutionTime.STATIC_INIT)
void createOpenTelemetry(OpenTelemetryRecorder recorder, Optional<TracerProviderBuildItem> tracerProviderBuildItem,
void createOpenTelemetry(OpenTelemetryConfig openTelemetryConfig,
OpenTelemetryRecorder recorder,
Optional<TracerProviderBuildItem> tracerProviderBuildItem,
LaunchModeBuildItem launchMode) {
if (launchMode.getLaunchMode() == LaunchMode.DEVELOPMENT) {
recorder.resetGlobalOpenTelemetryForDevMode();
}

RuntimeValue<SdkTracerProvider> tracerProvider = tracerProviderBuildItem.map(TracerProviderBuildItem::getTracerProvider)
.orElse(null);
recorder.createOpenTelemetry(tracerProvider);
recorder.createOpenTelemetry(tracerProvider, openTelemetryConfig);
recorder.eagerlyCreateContextStorage();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
import org.jboss.jandex.IndexView;
import org.jboss.jandex.MethodInfo;

import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.IdGenerator;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
Expand All @@ -32,10 +35,14 @@
import io.quarkus.opentelemetry.runtime.OpenTelemetryConfig;
import io.quarkus.opentelemetry.runtime.tracing.TracerProducer;
import io.quarkus.opentelemetry.runtime.tracing.TracerRecorder;
import io.quarkus.opentelemetry.runtime.tracing.TracerRuntimeConfig;
import io.quarkus.runtime.configuration.ConfigurationException;
import io.quarkus.vertx.core.deployment.VertxOptionsConsumerBuildItem;

public class TracerProcessor {
private static final DotName ID_GENERATOR = DotName.createSimple(IdGenerator.class.getName());
private static final DotName RESOURCE = DotName.createSimple(Resource.class.getName());
private static final DotName SAMPLER = DotName.createSimple(Sampler.class.getName());
private static final DotName SPAN_EXPORTER = DotName.createSimple(SpanExporter.class.getName());
private static final DotName SPAN_PROCESSOR = DotName.createSimple(SpanProcessor.class.getName());

Expand Down Expand Up @@ -69,6 +76,15 @@ UnremovableBeanBuildItem ensureProducersAreRetained(

// Find all known SpanExporters and SpanProcessors
Collection<String> knownClasses = new HashSet<>();
knownClasses.add(ID_GENERATOR.toString());
index.getAllKnownImplementors(ID_GENERATOR)
.forEach(classInfo -> knownClasses.add(classInfo.name().toString()));
knownClasses.add(RESOURCE.toString());
index.getAllKnownImplementors(RESOURCE)
.forEach(classInfo -> knownClasses.add(classInfo.name().toString()));
knownClasses.add(SAMPLER.toString());
index.getAllKnownImplementors(SAMPLER)
.forEach(classInfo -> knownClasses.add(classInfo.name().toString()));
knownClasses.add(SPAN_EXPORTER.toString());
index.getAllKnownImplementors(SPAN_EXPORTER)
.forEach(classInfo -> knownClasses.add(classInfo.name().toString()));
Expand Down Expand Up @@ -111,18 +127,23 @@ VertxOptionsConsumerBuildItem vertxTracingOptions(TracerRecorder recorder) {

@BuildStep(onlyIf = TracerEnabled.class)
@Record(ExecutionTime.STATIC_INIT)
TracerProviderBuildItem createTracerProvider(TracerRecorder recorder,
TracerProviderBuildItem createTracerProvider(OpenTelemetryConfig config,
TracerRecorder recorder,
ApplicationInfoBuildItem appInfo,
ShutdownContextBuildItem shutdownContext,
BeanContainerBuildItem beanContainerBuildItem) {
String serviceName = appInfo.getName();
String serviceVersion = appInfo.getVersion();
return new TracerProviderBuildItem(recorder.createTracerProvider(serviceName, serviceVersion, shutdownContext));
return new TracerProviderBuildItem(
recorder.createTracerProvider(config.tracer, serviceName, serviceVersion, shutdownContext));
}

@BuildStep(onlyIf = TracerEnabled.class)
@Record(ExecutionTime.RUNTIME_INIT)
void setupVertxTracer(TracerRecorder recorder) {
void setupTracer(TracerRuntimeConfig runtimeConfig,
TracerRecorder recorder) {
recorder.setupResources(runtimeConfig);
recorder.setupSampler(runtimeConfig);
recorder.setupVertxTracer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.quarkus.opentelemetry.deployment;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;

import java.lang.reflect.InvocationTargetException;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.extension.aws.trace.AwsXrayIdGenerator;
import io.opentelemetry.sdk.trace.IdGenerator;
import io.quarkus.test.QuarkusUnitTest;

public class OpenTelemetryIdGeneratorTest {
@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClass(TestUtil.class));

@Inject
OpenTelemetry openTelemetry;

@Test
void test() throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
IdGenerator idGenerator = TestUtil.getIdGenerator(openTelemetry);

assertThat(idGenerator, instanceOf(AwsXrayIdGenerator.class));
}

@ApplicationScoped
public static class OtelConfiguration {

@Produces
public IdGenerator idGenerator() {
return AwsXrayIdGenerator.getInstance();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.quarkus.opentelemetry.deployment;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.ArrayMatching.arrayContainingInAnyOrder;

import javax.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.extension.aws.AwsXrayPropagator;
import io.quarkus.test.QuarkusUnitTest;

public class OpenTelemetryPropagatorsTest {
@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.overrideConfigKey("quarkus.opentelemetry.propagators", "tracecontext,xray")
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClass(TestUtil.class));

@Inject
OpenTelemetry openTelemetry;

@Test
void test() throws NoSuchFieldException, IllegalAccessException {
TextMapPropagator[] textMapPropagators = TestUtil.getTextMapPropagators(openTelemetry);

assertThat(textMapPropagators, arrayContainingInAnyOrder(W3CTraceContextPropagator.getInstance(),
AwsXrayPropagator.getInstance()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.quarkus.opentelemetry.deployment;

import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyOrNullString;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.lang.reflect.InvocationTargetException;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.extension.resources.OsResource;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
import io.quarkus.test.QuarkusUnitTest;

public class OpenTelemetryResourceTest {
private static final String RESOURCE_ATTRIBUTES = "quarkus.opentelemetry.tracer.resource-attributes";

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.overrideConfigKey("quarkus.opentelemetry.tracer.resources", "os")
.setBeforeAllCustomizer(() -> System.setProperty(RESOURCE_ATTRIBUTES, "service.name=authservice"))
.setAfterAllCustomizer(() -> System.getProperties().remove(RESOURCE_ATTRIBUTES))
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClass(TestUtil.class)
.addAsResource("resource-config/application.properties"));

@Inject
OpenTelemetry openTelemetry;

@Test
void test() throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Resource resource = TestUtil.getResource(openTelemetry);

assertEquals("authservice", resource.getAttributes().get(ResourceAttributes.SERVICE_NAME));
assertThat(resource.getAttributes().get(ResourceAttributes.OS_TYPE), not(emptyOrNullString()));
}

@ApplicationScoped
public static class OtelConfiguration {

@Produces
public Resource resource() {
return OsResource.get();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.quarkus.opentelemetry.deployment;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.stringContainsInOrder;

import java.lang.reflect.InvocationTargetException;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.quarkus.test.QuarkusUnitTest;

public class OpenTelemetrySamplerBeanTest {
@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClass(TestUtil.class));

@Inject
OpenTelemetry openTelemetry;

@Test
void test() throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Sampler sampler = TestUtil.getSampler(openTelemetry);

assertThat(sampler.getDescription(), stringContainsInOrder("AlwaysOffSampler"));
}

@ApplicationScoped
public static class OtelConfiguration {

@Produces
public Sampler sampler() {
return Sampler.alwaysOff();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.quarkus.opentelemetry.deployment;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.lang.reflect.InvocationTargetException;

import javax.inject.Inject;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.quarkus.test.QuarkusUnitTest;

public class OpenTelemetrySamplerConfigTest {
@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.overrideConfigKey("quarkus.opentelemetry.tracer.sampler", "ratio")
.overrideConfigKey("quarkus.opentelemetry.tracer.sampler.ratio", "0.5")
.overrideConfigKey("quarkus.opentelemetry.tracer.sampler.parent-based", "false")
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClass(TestUtil.class));

@Inject
OpenTelemetry openTelemetry;

@Test
void test() throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Sampler sampler = TestUtil.getSampler(openTelemetry);

assertEquals(String.format("TraceIdRatioBased{%.6f}", 0.5d), sampler.getDescription());
}
}
Loading

0 comments on commit 9fd7d49

Please sign in to comment.