Skip to content

Commit

Permalink
Remove annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
mcruzdev committed Nov 26, 2024
1 parent 30859cc commit 55f61e2
Show file tree
Hide file tree
Showing 14 changed files with 11 additions and 1,115 deletions.
69 changes: 9 additions & 60 deletions docs/src/main/asciidoc/opentelemetry-tracing.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -590,81 +590,30 @@ quarkus.otel.instrument.rest=false
quarkus.otel.instrument.resteasy=false
----

=== Disable specific REST endpoints
[[disabling-traces-for-app-endpoints]]
=== Disabling Specific Traces for Application Endpoints

There are two ways to disable tracing for a specific REST endpoint.
You can use the `quarkus.otel.traces.suppress-application-uris` property to exclude specific endpoints from being traced.

You can use the `@io.quarkus.opentelemetry.runtime.tracing.Traceless` (or simply `@Traceless`) annotation to disable tracing for a specific endpoint.

Examples:

==== `@Traceless` annotation on a class

[source,java]
.PingResource.java
----
@Path("/health")
public class PingResource {
@Path("/ping")
public String ping() {
return "pong";
}
}
----

When the `@Traceless` annotation is placed on a class, all methods annotated with `@Path` will be excluded from tracing.

==== `@Traceless` annotation on a method

[source,java]
.TraceResource.java
----
@Path("/trace")
@Traceless
public class TraceResource {
@Path("/no")
@GET
@Traceless
public String noTrace() {
return "no";
}
@Path("/yes")
@GET
public String withTrace() {
return "yes";
}
}
----

In the example above, only `GET /trace/yes` will be included in tracing.

==== Disable using configuration

If you do not want to modify the source code, you can use your `application.properties` to disable a specific endpoint through the `quarkus.otel.traces.suppress-application-uris` property.

Example:
==== Example Configuration

[source,properties]
.application.properties
----
# application.properties
quarkus.otel.traces.suppress-application-uris=trace,ping,people*
----

This configuration will:

- Disable tracing for the `/trace` URI;
- Disable tracing for the `/ping` URI;
- Disable tracing for the `/people` URI and all other URIs under it, e.g., `/people/1`, `/people/1/cars`.
- Disable tracing for the `/trace` URI.
- Disable tracing for the `/ping` URI.
- Disable tracing for the `/people` URI and all subpaths, such as `/people/1` and `/people/1/cars`.

[NOTE]
====
If you are using `quarkus.http.root-path`, you need to remember to include the root path in the configuration. Unlike `@Traceless`, this configuration does not automatically add the root path.
If you are using `quarkus.http.root-path`, ensure you include the root path in the configuration.
====


[[configuration-reference]]
== OpenTelemetry Configuration Reference

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,15 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.BooleanSupplier;

import jakarta.enterprise.inject.spi.EventContext;
import jakarta.inject.Singleton;

import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.ClassInfo;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.IndexView;
Expand Down Expand Up @@ -56,7 +53,6 @@
import io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig;
import io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig.SecurityEvents.SecurityEventType;
import io.quarkus.opentelemetry.runtime.tracing.DelayedAttributes;
import io.quarkus.opentelemetry.runtime.tracing.Traceless;
import io.quarkus.opentelemetry.runtime.tracing.TracerRecorder;
import io.quarkus.opentelemetry.runtime.tracing.cdi.TracerProducer;
import io.quarkus.opentelemetry.runtime.tracing.security.EndUserSpanProcessor;
Expand All @@ -73,7 +69,6 @@ public class TracerProcessor {
private static final DotName SPAN_EXPORTER = DotName.createSimple(SpanExporter.class.getName());
private static final DotName SPAN_PROCESSOR = DotName.createSimple(SpanProcessor.class.getName());
private static final DotName TEXT_MAP_PROPAGATOR = DotName.createSimple(TextMapPropagator.class.getName());
private static final DotName TRACELESS = DotName.createSimple(Traceless.class.getName());
private static final DotName PATH = DotName.createSimple("jakarta.ws.rs.Path");

@BuildStep
Expand Down Expand Up @@ -137,29 +132,14 @@ UnremovableBeanBuildItem ensureProducersAreRetained(
return new UnremovableBeanBuildItem(new UnremovableBeanBuildItem.BeanClassNamesExclusion(retainProducers));
}

@BuildStep
void dropApplicationUris(
CombinedIndexBuildItem combinedIndexBuildItem,
BuildProducer<DropApplicationUrisBuildItem> uris) {
String rootPath = ConfigProvider.getConfig().getOptionalValue("quarkus.http.root-path", String.class).orElse("/");
IndexView index = combinedIndexBuildItem.getIndex();
Collection<AnnotationInstance> annotations = index.getAnnotations(TRACELESS);
Set<String> tracelessUris = generateTracelessUris(annotations.stream().toList(), rootPath);
for (String uri : tracelessUris) {
uris.produce(new DropApplicationUrisBuildItem(uri));
}
}

@BuildStep
void dropNames(
Optional<FrameworkEndpointsBuildItem> frameworkEndpoints,
Optional<StaticResourcesBuildItem> staticResources,
BuildProducer<DropNonApplicationUrisBuildItem> dropNonApplicationUris,
BuildProducer<DropStaticResourcesBuildItem> dropStaticResources,
List<DropApplicationUrisBuildItem> applicationUris) {
BuildProducer<DropStaticResourcesBuildItem> dropStaticResources) {

List<String> nonApplicationUris = new ArrayList<>(
applicationUris.stream().map(DropApplicationUrisBuildItem::uri).toList());
List<String> nonApplicationUris = new ArrayList<>();

// Drop framework paths
frameworkEndpoints.ifPresent(
Expand Down Expand Up @@ -192,77 +172,6 @@ void dropNames(
dropStaticResources.produce(new DropStaticResourcesBuildItem(resources));
}

private Set<String> generateTracelessUris(final List<AnnotationInstance> annotations, final String rootPath) {
final Set<String> applicationUris = new HashSet<>();
for (AnnotationInstance annotation : annotations) {
AnnotationTarget.Kind kind = annotation.target().kind();

switch (kind) {
case CLASS -> {
AnnotationInstance classAnnotated = annotation.target().asClass().annotations()
.stream().filter(TracerProcessor::isClassAnnotatedWithPath).findFirst().orElse(null);

if (Objects.isNull(classAnnotated)) {
throw new IllegalStateException(
String.format(
"The class '%s' is annotated with @Traceless but is missing the required @Path annotation. "
+
"Please ensure that the class is properly annotated with @Path annotation.",
annotation.target().asClass().name()));
}

String classPath = classAnnotated.value().asString();
String finalPath = combinePaths(rootPath, classPath);

if (containsPathExpression(finalPath)) {
applicationUris.add(sanitizeForTraceless(finalPath) + "*");
continue;
}

applicationUris.add(finalPath + "*");
applicationUris.add(finalPath);
}
case METHOD -> {
ClassInfo classInfo = annotation.target().asMethod().declaringClass();

AnnotationInstance possibleClassAnnotatedWithPath = classInfo.asClass()
.annotations()
.stream()
.filter(TracerProcessor::isClassAnnotatedWithPath)
.findFirst()
.orElse(null);

if (Objects.isNull(possibleClassAnnotatedWithPath)) {
throw new IllegalStateException(
String.format(
"The class '%s' contains a method annotated with @Traceless but is missing the required @Path annotation. "
+
"Please ensure that the class is properly annotated with @Path annotation.",
classInfo.name()));
}

String finalPath;
String classPath = possibleClassAnnotatedWithPath.value().asString();
AnnotationInstance possibleMethodAnnotatedWithPath = annotation.target().annotation(PATH);
if (possibleMethodAnnotatedWithPath != null) {
String methodValue = possibleMethodAnnotatedWithPath.value().asString();
finalPath = combinePaths(rootPath, combinePaths(classPath, methodValue));
} else {
finalPath = combinePaths(rootPath, classPath);
}

if (containsPathExpression(finalPath)) {
applicationUris.add(sanitizeForTraceless(finalPath) + "*");
continue;
}

applicationUris.add(finalPath);
}
}
}
return applicationUris;
}

@BuildStep
@Record(ExecutionTime.STATIC_INIT)
SyntheticBeanBuildItem setupDelayedAttribute(TracerRecorder recorder, ApplicationInfoBuildItem appInfo) {
Expand Down

This file was deleted.

Loading

0 comments on commit 55f61e2

Please sign in to comment.