Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support exclusion of non-application URIs if custom Sampler is used #26367

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package io.quarkus.opentelemetry.deployment;

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

import java.lang.reflect.InvocationTargetException;
import java.util.List;

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

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

import io.opentelemetry.api.OpenTelemetry;
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.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class OpenTelemetryCustomSamplerBeanTest {

private static final String TEST_SAMPLER = "testSampler";

@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClass(TracerRouter.class)
.addClass(TestSpanExporter.class)
.addClass(TestUtil.class));

@Inject
OpenTelemetry openTelemetry;

@Inject
TestSpanExporter testSpanExporter;

@Test
void testHealthEndpointNotTraced() {
RestAssured.when().get("/q/health").then()
.statusCode(200)
.body(containsString("\"status\": \"UP\""));

RestAssured.when().get("/q/health/live").then()
.statusCode(200)
.body(containsString("\"status\": \"UP\""));

RestAssured.when().get("/q/health/ready").then()
.statusCode(200)
.body(containsString("\"status\": \"UP\""));

RestAssured.when().get("/tracer").then()
.statusCode(200)
.body(is("Hello Tracer!"));

testSpanExporter.assertSpanCount(2);
}

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

assertThat(sampler.getDescription(), stringContainsInOrder(TEST_SAMPLER));
}

@ApplicationScoped
public static class OtelConfiguration {

@Produces
public Sampler sampler() {
return new 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 @@ -26,9 +26,6 @@ public class TracerRuntimeConfig {
* Suppress non-application uris from trace collection.
* This will suppress tracing of `/q` endpoints.
* <p>
* Providing a custom {@code io.opentelemetry.sdk.trace.samplers.Sampler} CDI Bean
* will ignore this setting.
* <p>
* Suppressing non-application uris is enabled by default.
*/
@ConfigItem(defaultValue = "true")
Expand All @@ -37,9 +34,6 @@ public class TracerRuntimeConfig {
/**
* Include static resources from trace collection.
* <p>
* Providing a custom {@code io.opentelemetry.sdk.trace.samplers.Sampler} CDI Bean
* will ignore this setting.
* <p>
* Include static resources is disabled by default.
*/
@ConfigItem(defaultValue = "false")
Expand Down
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