Skip to content

Commit

Permalink
1st deployment test
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobat committed Aug 22, 2024
1 parent c52c74f commit a080b8b
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 2 deletions.
5 changes: 5 additions & 0 deletions extensions/opentelemetry/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@
<artifactId>quarkus-security-deployment</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.quarkus.opentelemetry.deployment.common;

import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.quarkus.arc.Unremovable;
import jakarta.enterprise.context.ApplicationScoped;
import org.awaitility.Awaitility;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;

@Unremovable
@ApplicationScoped
public class InMemoryLogRecordExporter implements LogRecordExporter {
final Queue<LogRecordData> finishedLogItems = new ConcurrentLinkedQueue();
boolean isStopped = false;

private InMemoryLogRecordExporter() {
}

public static InMemoryLogRecordExporter create() {
return new InMemoryLogRecordExporter();
}

public List<LogRecordData> getFinishedLogRecordItemsAtLeast(final int count) {
Awaitility.await().atMost(5, SECONDS)
.untilAsserted(() -> assertThat(getFinishedLogRecordItems().size()).isGreaterThanOrEqualTo(count));
return getFinishedLogRecordItems();
}

private List<LogRecordData> getFinishedLogRecordItems() {
return Collections.unmodifiableList(new ArrayList(this.finishedLogItems));
}


public void reset() {
this.finishedLogItems.clear();
}

public CompletableResultCode export(Collection<LogRecordData> logs) {
if (this.isStopped) {
return CompletableResultCode.ofFailure();
} else {
this.finishedLogItems.addAll(logs);
return CompletableResultCode.ofSuccess();
}
}

public CompletableResultCode flush() {
return CompletableResultCode.ofSuccess();
}

public CompletableResultCode shutdown() {
this.isStopped = true;
this.finishedLogItems.clear();
return CompletableResultCode.ofSuccess();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.opentelemetry.deployment.common;

import jakarta.enterprise.inject.spi.CDI;

import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.autoconfigure.spi.logs.ConfigurableLogRecordExporterProvider;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter;

public class InMemoryLogRecordExporterProvider implements ConfigurableLogRecordExporterProvider {
@Override
public LogRecordExporter createExporter(ConfigProperties configProperties) {
return CDI.current().select(InMemoryLogRecordExporter.class).get();
}

@Override
public String getName() {
return "in-memory";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package io.quarkus.opentelemetry.deployment.logs;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.instrumentation.annotations.WithSpan;
import io.opentelemetry.sdk.logs.data.LogRecordData;
import io.quarkus.opentelemetry.deployment.common.InMemoryLogRecordExporter;
import io.quarkus.opentelemetry.deployment.common.InMemoryLogRecordExporterProvider;
import io.quarkus.test.QuarkusUnitTest;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.jboss.logging.Logger;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

public class JBossLoggingTest {

private static final String MESSAGE = "Hello from JBoss Logging";

@RegisterExtension
static final QuarkusUnitTest TEST = new QuarkusUnitTest()
.setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class)
.addClass(HelloBean.class)
.addClasses(InMemoryLogRecordExporter.class, InMemoryLogRecordExporterProvider.class)
.addAsResource(new StringAsset(InMemoryLogRecordExporterProvider.class.getCanonicalName()),
"META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.logs.ConfigurableLogRecordExporterProvider")
.add(new StringAsset(
"quarkus.otel.logs.enabled=true\n" +
"quarkus.otel.traces.exporter=none\n"),
"application.properties"));

@Inject
InMemoryLogRecordExporter logRecordExporter;

@Inject
HelloBean helloBean;

@BeforeEach
void setup() {
logRecordExporter.reset();
}

@Test
public void testJBossLogging() {
helloBean.hello();
List<LogRecordData> finishedLogRecordItems = logRecordExporter.getFinishedLogRecordItemsAtLeast(4);
assertThat(finishedLogRecordItems.getLast().getBody().asString()).isEqualTo(MESSAGE);
}

@ApplicationScoped
public static class HelloBean {
private static final Logger LOG = Logger.getLogger(HelloBean.class.getName());

public String hello() {
LOG.info(MESSAGE);
return "hello";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.opentelemetry.deployment.common.InMemoryLogRecordExporterProvider
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Traces
quarkus.otel.traces.exporter=test-span-exporter
quarkus.otel.bsp.schedule.delay=50ms
quarkus.otel.bsp.export.timeout=1s

# Metrics
quarkus.otel.metrics.exporter=in-memory
quarkus.otel.metric.export.interval=300ms
quarkus.otel.metric.export.interval=300ms

# Logs
quarkus.otel.logs.exporter=in-memory
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
import io.opentelemetry.api.logs.LogRecordBuilder;
import io.opentelemetry.api.logs.Severity;
import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;

public class OpenTelemetryLogHandler extends Handler {
private static final String THROWN_ATTRIBUTE = "thrown";

@Override
public void publish(LogRecord record) {
try (InstanceHandle<OpenTelemetry> openTelemetry = Arc.container().instance(OpenTelemetry.class)) {
ArcContainer container = Arc.container();
if (container == null || !container.instance(OpenTelemetry.class).isAvailable()) {
// "quarkus-opentelemetry-deployment stopped in Xs" will never be sent.
return; // evaluate to perform cache of log entries here and replay them later.
}
try (InstanceHandle<OpenTelemetry> openTelemetry = container.instance(OpenTelemetry.class)) {
if (openTelemetry.isAvailable()) {
LogRecordBuilder logRecordBuilder = openTelemetry.get().getLogsBridge().loggerBuilder(INSTRUMENTATION_NAME)
.build().logRecordBuilder()
Expand Down

0 comments on commit a080b8b

Please sign in to comment.