Skip to content

Commit

Permalink
EndUserSpanProcessor integration
Browse files Browse the repository at this point in the history
  • Loading branch information
A.Moscatelli authored and A.Moscatelli committed Jul 7, 2023
1 parent 66e1053 commit 77d302a
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.annotations.ExecutionTime;
Expand All @@ -16,6 +17,7 @@
import io.quarkus.opentelemetry.runtime.config.runtime.OTelRuntimeConfig;
import io.quarkus.opentelemetry.runtime.config.runtime.exporter.OtlpExporterRuntimeConfig;
import io.quarkus.opentelemetry.runtime.exporter.otlp.OtlpExporterProvider;
import io.quarkus.opentelemetry.runtime.exporter.otlp.EndUserSpanProcessor;
import io.quarkus.opentelemetry.runtime.exporter.otlp.OtlpRecorder;

@BuildSteps(onlyIf = OtlpExporterProcessor.OtlpExporterEnabled.class)
Expand All @@ -40,6 +42,18 @@ AdditionalBeanBuildItem createBatchSpanProcessor() {
.setUnremovable().build();
}

@BuildStep
void createEndUserSpanProcessor(
BuildProducer<AdditionalBeanBuildItem> buildProducer,
OTelBuildConfig otelBuildConfig
) {
if (otelBuildConfig.traces().eusp().enabled().orElse(Boolean.FALSE)) {
buildProducer.produce(
AdditionalBeanBuildItem.unremovableOf(
EndUserSpanProcessor.class));
}
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
void installBatchSpanProcessorForOtlp(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.opentelemetry.runtime.config.build;

import java.util.Optional;

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

/**
* Tracing build time configuration
*/
@ConfigGroup
public interface EndUserSpanProcessorConfig {

/**
* Enable tracing with OpenTelemetry.
* <p>
* This property is not available in the Open Telemetry SDK. It's Quarkus specific.
* <p>
* Support for tracing will be enabled if OpenTelemetry support is enabled
* and either this value is true, or this value is unset.
*/
@WithDefault("false")
Optional<Boolean> enabled();

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public interface TracesBuildConfig {
*/
@WithDefault(SamplerType.Constants.PARENT_BASED_ALWAYS_ON)
String sampler();

/**
* EndUser SpanProcessor configurations.
*/
EndUserSpanProcessorConfig eusp();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.quarkus.opentelemetry.runtime.exporter.otlp;

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.trace.attributes.SemanticAttributes;
import io.quarkus.security.identity.SecurityIdentity;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.control.ActivateRequestContext;
import jakarta.inject.Inject;
import org.eclipse.microprofile.context.ManagedExecutor;

@ApplicationScoped
public class EndUserSpanProcessor implements SpanProcessor {

@Inject
protected SecurityIdentity securityIdentity;

@Inject
protected ManagedExecutor managedExecutor;

@Override
@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()
)
)
);
}

@Override
public boolean isStartRequired() {
return Boolean.TRUE;
}

@Override
public void onEnd(ReadableSpan span) {
}

@Override
public boolean isEndRequired() {
return Boolean.FALSE;
}

}

0 comments on commit 77d302a

Please sign in to comment.