-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
91df07b
commit 02dae12
Showing
8 changed files
with
246 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...c/main/java/io/quarkus/opentelemetry/runtime/config/build/EndUserSpanProcessorConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
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 the {@link io.quarkus.opentelemetry.runtime.exporter.otlp.EndUserSpanProcessor}. | ||
* <p> | ||
* The {@link io.quarkus.opentelemetry.runtime.exporter.otlp.EndUserSpanProcessor} adds | ||
* the {@link io.opentelemetry.semconv.trace.attributes.SemanticAttributes.ENDUSER_ID} | ||
* and {@link io.opentelemetry.semconv.trace.attributes.SemanticAttributes.ENDUSER_ROLE} to the Span. | ||
*/ | ||
@WithDefault("false") | ||
Optional<Boolean> enabled(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...me/src/main/java/io/quarkus/opentelemetry/runtime/exporter/otlp/EndUserSpanProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.quarkus.opentelemetry.runtime.exporter.otlp; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.context.control.ActivateRequestContext; | ||
import jakarta.inject.Inject; | ||
|
||
import org.eclipse.microprofile.context.ManagedExecutor; | ||
|
||
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; | ||
|
||
@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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
integration-tests/opentelemetry/src/test/java/io/quarkus/it/opentelemetry/EndUserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.quarkus.it.opentelemetry; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.awaitility.Awaitility.await; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.List; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter; | ||
import io.opentelemetry.sdk.trace.data.SpanData; | ||
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; | ||
import io.quarkus.it.opentelemetry.util.EndUserProfile; | ||
import io.quarkus.it.opentelemetry.util.EndUserResource; | ||
import io.quarkus.test.common.http.TestHTTPEndpoint; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
import io.quarkus.test.junit.TestProfile; | ||
import io.quarkus.test.security.TestSecurity; | ||
|
||
@QuarkusTest | ||
@TestHTTPEndpoint(EndUserResource.class) | ||
@TestProfile(EndUserProfile.class) | ||
public class EndUserTest { | ||
|
||
@Inject | ||
InMemorySpanExporter inMemorySpanExporter; | ||
|
||
@BeforeEach | ||
@AfterEach | ||
void reset() { | ||
inMemorySpanExporter.reset(); | ||
} | ||
|
||
private List<SpanData> getSpans() { | ||
return inMemorySpanExporter.getFinishedSpanItems(); | ||
} | ||
|
||
@Test | ||
@TestSecurity(user = "testUser", roles = { "admin", "user" }) | ||
public void testEndUserInjections() { | ||
given() | ||
.when().get() | ||
.then() | ||
.statusCode(200); | ||
await().atMost(5, SECONDS).until(() -> getSpans().size() == 1); | ||
SpanData spanData = getSpans().get(0); | ||
Attributes attributes = spanData.getAttributes(); | ||
assertEquals(attributes.get(SemanticAttributes.ENDUSER_ID), "testUser"); | ||
assertEquals(attributes.get(SemanticAttributes.ENDUSER_ROLE), "[admin, user]"); | ||
} | ||
|
||
@Test | ||
@TestSecurity(user = "testUser", roles = { "admin", "user" }) | ||
public void testEndUserInjectionsAsync() { | ||
given() | ||
.when().get("/async") | ||
.then() | ||
.statusCode(200); | ||
await().atMost(5, SECONDS).until(() -> getSpans().size() == 1); | ||
SpanData spanData = getSpans().get(0); | ||
Attributes attributes = spanData.getAttributes(); | ||
assertEquals(attributes.get(SemanticAttributes.ENDUSER_ID), "testUser"); | ||
assertEquals(attributes.get(SemanticAttributes.ENDUSER_ROLE), "[admin, user]"); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...on-tests/opentelemetry/src/test/java/io/quarkus/it/opentelemetry/util/EndUserProfile.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.quarkus.it.opentelemetry.util; | ||
|
||
import java.util.Map; | ||
|
||
import io.quarkus.test.junit.QuarkusTestProfile; | ||
|
||
public class EndUserProfile implements QuarkusTestProfile { | ||
|
||
@Override | ||
public Map<String, String> getConfigOverrides() { | ||
return Map.of( | ||
"quarkus.otel.traces.eusp.enabled", "true"); | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...n-tests/opentelemetry/src/test/java/io/quarkus/it/opentelemetry/util/EndUserResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package io.quarkus.it.opentelemetry.util; | ||
|
||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.enterprise.inject.Instance; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.Response; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
|
||
import io.quarkus.opentelemetry.runtime.exporter.otlp.EndUserSpanProcessor; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@Path("/otel/enduser") | ||
@RequestScoped | ||
public class EndUserResource { | ||
|
||
@Inject | ||
Instance<EndUserSpanProcessor> endUserSpanProcessor; | ||
|
||
@GET | ||
public Response verifyOTelInjections() { | ||
verifyInjections(); | ||
return Response.ok().build(); | ||
} | ||
|
||
@GET | ||
@Path("/async") | ||
public Uni<Response> verifyOTelInjectionsAsync() { | ||
verifyInjections(); | ||
return Uni.createFrom().item(Response.ok().build()); | ||
} | ||
|
||
private void verifyInjections() { | ||
Assertions.assertTrue(endUserSpanProcessor.isResolvable(), "EndUserSpanProcessor cannot be injected"); | ||
} | ||
} |