Skip to content

Commit

Permalink
Add DelayedAttributes and LateBoundSampler
Browse files Browse the repository at this point in the history
  • Loading branch information
luneo7 committed Jun 15, 2021
1 parent cfaf807 commit dc8bd7a
Show file tree
Hide file tree
Showing 16 changed files with 327 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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;
Expand All @@ -34,11 +35,13 @@
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 @@ -76,6 +79,9 @@ UnremovableBeanBuildItem ensureProducersAreRetained(
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()));
Expand Down Expand Up @@ -121,9 +127,9 @@ VertxOptionsConsumerBuildItem vertxTracingOptions(TracerRecorder recorder) {

@BuildStep(onlyIf = TracerEnabled.class)
@Record(ExecutionTime.STATIC_INIT)
TracerProviderBuildItem createTracerProvider(TracerRecorder recorder,
TracerProviderBuildItem createTracerProvider(OpenTelemetryConfig config,
TracerRecorder recorder,
ApplicationInfoBuildItem appInfo,
OpenTelemetryConfig config,
ShutdownContextBuildItem shutdownContext,
BeanContainerBuildItem beanContainerBuildItem) {
String serviceName = appInfo.getName();
Expand All @@ -134,7 +140,10 @@ TracerProviderBuildItem createTracerProvider(TracerRecorder recorder,

@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
@@ -1,5 +1,6 @@
package io.quarkus.opentelemetry.deployment;

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

import java.lang.reflect.InvocationTargetException;
Expand All @@ -8,7 +9,6 @@
import javax.enterprise.inject.Produces;
import javax.inject.Inject;

import org.hamcrest.MatcherAssert;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
Expand All @@ -31,7 +31,7 @@ public class OpenTelemetryIdGeneratorTest {
void test() throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
IdGenerator idGenerator = TestUtil.getIdGenerator(openTelemetry);

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

@ApplicationScoped
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.quarkus.opentelemetry.deployment;

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

import javax.inject.Inject;

import org.hamcrest.MatcherAssert;
import org.hamcrest.collection.ArrayMatching;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
Expand All @@ -28,8 +29,7 @@ public class OpenTelemetryPropagatorsTest {
void test() throws NoSuchFieldException, IllegalAccessException {
TextMapPropagator[] textMapPropagators = TestUtil.getTextMapPropagators(openTelemetry);

MatcherAssert.assertThat(textMapPropagators,
ArrayMatching.arrayContainingInAnyOrder(W3CTraceContextPropagator.getInstance(),
AwsXrayPropagator.getInstance()));
assertThat(textMapPropagators, arrayContainingInAnyOrder(W3CTraceContextPropagator.getInstance(),
AwsXrayPropagator.getInstance()));
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
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.hamcrest.CoreMatchers;
import org.hamcrest.MatcherAssert;
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")
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClass(TestUtil.class));
.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;
Expand All @@ -31,7 +41,16 @@ public class OpenTelemetryResourceTest {
void test() throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Resource resource = TestUtil.getResource(openTelemetry);

MatcherAssert.assertThat(resource.getAttributes().get(ResourceAttributes.OS_TYPE),
CoreMatchers.not(emptyOrNullString()));
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
@@ -1,14 +1,14 @@
package io.quarkus.opentelemetry.deployment;

import static org.hamcrest.Matchers.instanceOf;
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.hamcrest.MatcherAssert;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
Expand All @@ -18,7 +18,7 @@
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.quarkus.test.QuarkusUnitTest;

public class OpenTelemetrySamplerTest {
public class OpenTelemetrySamplerBeanTest {
@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class).addClass(TestUtil.class));
Expand All @@ -30,14 +30,14 @@ public class OpenTelemetrySamplerTest {
void test() throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
Sampler sampler = TestUtil.getSampler(openTelemetry);

MatcherAssert.assertThat(sampler, instanceOf(Sampler.alwaysOff().getClass()));
assertThat(sampler.getDescription(), stringContainsInOrder("AlwaysOffSampler"));
}

@ApplicationScoped
public static class OtelConfiguration {

@Produces
public Sampler idGenerator() {
public Sampler sampler() {
return Sampler.alwaysOff();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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", "traceidratio")
.overrideConfigKey("quarkus.opentelemetry.tracer.sampler.ratio", "0.5")
.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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_STATUS_CODE;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_TARGET;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HTTP_USER_AGENT;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.stringContainsInOrder;
import static org.hamcrest.collection.ArrayMatching.arrayContainingInAnyOrder;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

Expand All @@ -24,13 +26,13 @@
import javax.enterprise.event.Observes;
import javax.inject.Inject;

import org.hamcrest.MatcherAssert;
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.baggage.propagation.W3CBaggagePropagator;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.TextMapPropagator;
Expand Down Expand Up @@ -63,7 +65,7 @@ void trace() throws NoSuchFieldException, IllegalAccessException, InvocationTarg

List<SpanData> spans = myExporter.getFinishedSpanItems();

TextMapPropagator textMapPropagator = openTelemetry.getPropagators().getTextMapPropagator();
TextMapPropagator[] textMapPropagators = TestUtil.getTextMapPropagators(openTelemetry);
IdGenerator idGenerator = TestUtil.getIdGenerator(openTelemetry);
Sampler sampler = TestUtil.getSampler(openTelemetry);

Expand All @@ -76,9 +78,10 @@ void trace() throws NoSuchFieldException, IllegalAccessException, InvocationTarg
assertEquals("http", spans.get(1).getAttributes().get(HTTP_SCHEME));
assertEquals("localhost:8081", spans.get(1).getAttributes().get(HTTP_HOST));
assertEquals("127.0.0.1", spans.get(1).getAttributes().get(HTTP_CLIENT_IP));
MatcherAssert.assertThat(textMapPropagator, instanceOf(W3CTraceContextPropagator.getInstance().getClass()));
MatcherAssert.assertThat(idGenerator, instanceOf(IdGenerator.random().getClass()));
MatcherAssert.assertThat(sampler.getClass().getName(), stringContainsInOrder("ParentBasedSampler"));
assertThat(textMapPropagators, arrayContainingInAnyOrder(W3CTraceContextPropagator.getInstance(),
W3CBaggagePropagator.getInstance()));
assertThat(idGenerator, instanceOf(IdGenerator.random().getClass()));
assertThat(sampler.getDescription(), stringContainsInOrder("ParentBased", "AlwaysOnSampler"));
assertNotNull(spans.get(1).getAttributes().get(HTTP_USER_AGENT));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
quarkus.application.name=resource-test
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ public final class OpenTelemetryConfig {
* <p>
* Valid values are {@code b3, b3multi, baggage, jaeger, ottrace, tracecontext, xray}.
* <p>
* Default value is {@code traceContext}
* Default value is {@code traceContext,baggage}
*/
@ConfigItem(defaultValue = "tracecontext")
@ConfigItem(defaultValue = "tracecontext,baggage")
public List<String> propagators;

/** Build / static runtime config for tracer */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import io.opentelemetry.sdk.OpenTelemetrySdk;

// We can remove this substitution once AutoConfigure SPI is a separate artifact
@TargetClass(className = "io.opentelemetry.sdk.autoconfigure.OpenTelemetrySdkAutoConfiguration")
@Substitute
public final class OpenTelemetrySdkAutoConfiguration {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkus.opentelemetry.runtime.tracing;

import java.util.Map;
import java.util.function.BiConsumer;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;

public class DelayedAttributes implements Attributes {
private Attributes delegate;

public void setAttributesDelegate(Attributes delegate) {
this.delegate = delegate;
}

@Override
public <T> T get(AttributeKey<T> attributeKey) {
return delegate.get(attributeKey);
}

@Override
public void forEach(BiConsumer<? super AttributeKey<?>, ? super Object> biConsumer) {
delegate.forEach(biConsumer);
}

@Override
public int size() {
return delegate.size();
}

@Override
public boolean isEmpty() {
return delegate.isEmpty();
}

@Override
public Map<AttributeKey<?>, Object> asMap() {
return delegate.asMap();
}

@Override
public AttributesBuilder toBuilder() {
return delegate.toBuilder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.quarkus.opentelemetry.runtime.tracing;

import java.util.List;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.data.LinkData;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.opentelemetry.sdk.trace.samplers.SamplingResult;

public class LateBoundSampler implements Sampler {
private Sampler delegate;

public void setSamplerDelegate(Sampler delegate) {
this.delegate = delegate;
}

@Override
public SamplingResult shouldSample(Context parentContext,
String traceId,
String name,
SpanKind spanKind,
Attributes attributes,
List<LinkData> parentLinks) {
return delegate.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks);
}

@Override
public String getDescription() {
return delegate.getDescription();
}
}
Loading

0 comments on commit dc8bd7a

Please sign in to comment.