Skip to content

Commit

Permalink
Merge pull request #22750 from radcortez/fix-20698
Browse files Browse the repository at this point in the history
Use configured root-path and non-application-root-path to configure the NonApplicationEndpointSampler
  • Loading branch information
gsmet authored Jan 8, 2022
2 parents b962725 + 008832d commit 11c644d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkus.opentelemetry.deployment;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;

import javax.inject.Inject;

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

import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class NonAppEndpointsDisabledWithRootPathTest {
@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClass(TracerRouter.class)
.addClass(TestSpanExporter.class))
.overrideConfigKey("quarkus.http.root-path", "/app")
.overrideConfigKey("quarkus.http.non-application-root-path", "quarkus");

@Inject
TestSpanExporter testSpanExporter;

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

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

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

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

testSpanExporter.assertSpanCount(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,47 @@

import java.util.List;

import org.eclipse.microprofile.config.ConfigProvider;

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.SamplingDecision;
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
import io.quarkus.runtime.configuration.NormalizeRootHttpPathConverter;
import io.smallrye.config.SmallRyeConfig;

public class NonApplicationEndpointSampler implements Sampler {
private static final SamplingResult NEGATIVE_SAMPLING_RESULT = SamplingResult.create(SamplingDecision.DROP);

private final Sampler root;
private final Sampler sampler;
private final String namePattern;

public NonApplicationEndpointSampler(Sampler sampler) {
this.sampler = sampler;

public NonApplicationEndpointSampler(Sampler root) {
this.root = root;
// We don't use the HttpBuildTimeConfig because we don't want to add a dependency to vertx-http and vertx-http
// may not even be available.
SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
if (config.isPropertyPresent("quarkus.http.root-path")) {
String rootPath = config.getValue("quarkus.http.root-path", new NormalizeRootHttpPathConverter());
String nonApplicationRootPath = config.getRawValue("quarkus.http.non-application-root-path");
// span names don't include the leading slash
this.namePattern = rootPath.substring(1) + nonApplicationRootPath;
} else {
this.namePattern = null;
}
}

@Override
public SamplingResult shouldSample(Context parentContext, String traceId, String name, SpanKind spanKind,
Attributes attributes, List<LinkData> parentLinks) {
if (name.startsWith("q/") && spanKind == SpanKind.SERVER) {
if (namePattern != null && name.startsWith(namePattern) && spanKind == SpanKind.SERVER) {
return NEGATIVE_SAMPLING_RESULT;
}
return root.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks);
return sampler.shouldSample(parentContext, traceId, name, spanKind, attributes, parentLinks);
}

@Override
Expand Down

0 comments on commit 11c644d

Please sign in to comment.