Skip to content

Commit

Permalink
Merge pull request #39707 from brunobat/revert-user-info-backport
Browse files Browse the repository at this point in the history
Backport hide EndUserSpanProcessor integration
  • Loading branch information
geoand authored Mar 27, 2024
2 parents 4145f02 + a1f4fab commit 9002ec7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 12 deletions.
4 changes: 0 additions & 4 deletions docs/src/main/asciidoc/opentelemetry.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,6 @@ public class CustomConfiguration {
}
----

==== User data

By setting `quarkus.otel.traces.eusp.enabled=true` you can add information about the user related to each span. The user's ID and roles will be added to the span attributes, if available.

[[sampler]]
=== Sampler
A https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#sampling[sampler] decides whether a trace should be discarded or forwarded, effectively managing noise and reducing overhead by limiting the number of collected traces sent to the collector.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jboss.jandex.DotName;
import org.jboss.jandex.ParameterizedType;
import org.jboss.jandex.Type;
import org.jboss.logging.Logger;

import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
Expand All @@ -35,6 +36,8 @@
@BuildSteps(onlyIf = OtlpExporterProcessor.OtlpExporterEnabled.class)
public class OtlpExporterProcessor {

private static final Logger LOG = Logger.getLogger(OtlpExporterProcessor.class);

static class OtlpExporterEnabled implements BooleanSupplier {
OtlpExporterBuildConfig exportBuildConfig;
OTelBuildConfig otelBuildConfig;
Expand All @@ -55,6 +58,8 @@ void createEndUserSpanProcessor(
buildProducer.produce(
AdditionalBeanBuildItem.unremovableOf(
EndUserSpanProcessor.class));
LOG.warn("End User Span tracking has been enabled by setting quarkus.otel.traces.eusp.enabled=true. Please " +
"AVOID using this feature in production with the current Quarkus version.");
}
}

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

import java.util.Optional;

import io.quarkus.runtime.annotations.ConfigDocIgnore;
import io.quarkus.runtime.annotations.ConfigGroup;
import io.smallrye.config.WithDefault;

Expand All @@ -18,6 +19,7 @@ public interface EndUserSpanProcessorConfig {
* the {@link io.opentelemetry.semconv.SemanticAttributes.ENDUSER_ID}
* and {@link io.opentelemetry.semconv.SemanticAttributes.ENDUSER_ROLE} to the Span.
*/
@ConfigDocIgnore
@WithDefault("false")
Optional<Boolean> enabled();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
import jakarta.inject.Inject;

import org.eclipse.microprofile.context.ManagedExecutor;
import org.jboss.logging.Logger;

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.semconv.SemanticAttributes;
import io.quarkus.arc.Arc;
import io.quarkus.arc.ManagedContext;
import io.quarkus.security.identity.SecurityIdentity;

@ApplicationScoped
public class EndUserSpanProcessor implements SpanProcessor {

private static final Logger LOG = Logger.getLogger(EndUserSpanProcessor.class);

@Inject
protected SecurityIdentity securityIdentity;

Expand All @@ -27,14 +32,31 @@ public class EndUserSpanProcessor implements SpanProcessor {
@ActivateRequestContext
public void onStart(Context parentContext, ReadWriteSpan span) {
managedExecutor.execute(
() -> span.setAllAttributes(
securityIdentity.isAnonymous()
? Attributes.empty()
: Attributes.of(
SemanticAttributes.ENDUSER_ID,
securityIdentity.getPrincipal().getName(),
SemanticAttributes.ENDUSER_ROLE,
securityIdentity.getRoles().toString())));
() -> {
try {
if (!span.isRecording()) {
LOG.debug("Failed to set end user attributes on spanId:" +
span.getSpanContext().getSpanId() + " Span not recording");
return;
}
ManagedContext managedContext = Arc.container().requestContext();
if (!managedContext.isActive()) {
LOG.debug("Failed to set end user attributes on span.spanId:" +
span.getSpanContext().getSpanId() + " Request Context not active");
return;
}
span.setAllAttributes(
securityIdentity.isAnonymous()
? Attributes.empty()
: Attributes.of(
SemanticAttributes.ENDUSER_ID,
securityIdentity.getPrincipal().getName(),
SemanticAttributes.ENDUSER_ROLE,
securityIdentity.getRoles().toString()));
} catch (Exception e) {
LOG.debug("Failed to set end user attributes on span", e);
}
});
}

@Override
Expand Down

0 comments on commit 9002ec7

Please sign in to comment.