Skip to content

Commit

Permalink
Support exclusion of non-application URIs if custom Sampler is used
Browse files Browse the repository at this point in the history
  • Loading branch information
mun711 committed Jun 25, 2022
1 parent 748908f commit 7f5fa03
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.quarkus.opentelemetry.deployment;

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

import java.util.List;

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

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

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;
import io.quarkus.opentelemetry.runtime.tracing.DropNamesSampler;
import io.quarkus.opentelemetry.runtime.tracing.TracerRuntimeConfig;
import io.quarkus.opentelemetry.runtime.tracing.TracerUtil;
import io.quarkus.test.QuarkusUnitTest;

public class TracerUtilTest {
@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar.addClass(TestSampler.class));
private static final String TEST_SAMPLER = "testSampler";

@Test
void testEnhanceExistingSampler() {
TracerRuntimeConfig.SamplerConfig samplerConfig = new TracerRuntimeConfig.SamplerConfig();
samplerConfig.parentBased = false;
samplerConfig.samplerName = "on";
Sampler sampler = TracerUtil.mapSampler(samplerConfig, List.of("test"));
assertEquals(TEST_SAMPLER, sampler.getDescription());
assertEquals(DropNamesSampler.class, sampler.getClass());
}

@ApplicationScoped
public static class SamplerConfiguration {

@Produces
public TestSampler sampler() {
return new TestSampler();
}
}

static class TestSampler implements Sampler {

@Override
public SamplingResult shouldSample(Context context, String s, String s1, SpanKind spanKind, Attributes attributes,
List<LinkData> list) {
return SamplingResult.recordAndSample();
}

@Override
public String getDescription() {
return TEST_SAMPLER;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

import javax.enterprise.inject.Any;
Expand Down Expand Up @@ -140,25 +139,14 @@ public void setupSampler(
List<String> dropStaticResources) {

LateBoundSampler lateBoundSampler = CDI.current().select(LateBoundSampler.class, Any.Literal.INSTANCE).get();
Optional<Sampler> samplerBean = CDI.current()
.select(Sampler.class, Any.Literal.INSTANCE)
.stream()
.filter(o -> !(o instanceof LateBoundSampler))
.findFirst();

// Define Sampler using bean if present
if (samplerBean.isPresent()) {
lateBoundSampler.setSamplerDelegate(samplerBean.get());
} else {
List<String> dropNames = new ArrayList<>();
if (config.suppressNonApplicationUris) {
dropNames.addAll(dropNonApplicationUris);
}
if (!config.includeStaticResources) {
dropNames.addAll(dropStaticResources);
}
// Define Sampler using config
lateBoundSampler.setSamplerDelegate(TracerUtil.mapSampler(config.sampler, dropNames));
List<String> dropNames = new ArrayList<>();
if (config.suppressNonApplicationUris) {
dropNames.addAll(dropNonApplicationUris);
}
if (!config.includeStaticResources) {
dropNames.addAll(dropStaticResources);
}
Sampler samplerBean = TracerUtil.mapSampler(config.sampler, dropNames);
lateBoundSampler.setSamplerDelegate(samplerBean);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import java.util.List;
import java.util.Optional;

import javax.enterprise.inject.Any;
import javax.enterprise.inject.spi.CDI;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.sdk.resources.Resource;
Expand Down Expand Up @@ -34,8 +37,13 @@ private static Sampler getBaseSampler(String samplerName, Optional<Double> ratio
}
}

public static Sampler mapSampler(TracerRuntimeConfig.SamplerConfig samplerConfig, List<String> dropNames) {
Sampler sampler = getBaseSampler(samplerConfig.samplerName, samplerConfig.ratio);
public static Sampler mapSampler(TracerRuntimeConfig.SamplerConfig samplerConfig,
List<String> dropNames) {
Sampler sampler = CDI.current()
.select(Sampler.class, Any.Literal.INSTANCE)
.stream()
.filter(o -> !(o instanceof LateBoundSampler))
.findFirst().orElseGet(() -> getBaseSampler(samplerConfig.samplerName, samplerConfig.ratio));

if (!dropNames.isEmpty()) {
sampler = new DropNamesSampler(sampler, dropNames);
Expand Down

0 comments on commit 7f5fa03

Please sign in to comment.