Skip to content

Commit

Permalink
Native mode fails
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobat authored and geoand committed Sep 25, 2024
1 parent 9384388 commit 3ec1df9
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ void handleServices(OTelBuildConfig config,
runtimeReinitialized.produce(
new RuntimeReinitializedClassBuildItem("io.opentelemetry.sdk.autoconfigure.MeterProviderConfiguration"));
runtimeReinitialized.produce(
new RuntimeReinitializedClassBuildItem("io.opentelemetry.sdk.autoconfigure.LogMeterProviderConfiguration"));
new RuntimeReinitializedClassBuildItem("io.opentelemetry.sdk.autoconfigure.LoggerProviderConfiguration"));
runtimeReinitialized.produce(
new RuntimeReinitializedClassBuildItem("io.quarkus.opentelemetry.runtime.logs.OpenTelemetryLogHandler"));

services.produce(ServiceProviderBuildItem.allProvidersFromClassPath(
ConfigurableSamplerProvider.class.getName()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
package io.quarkus.opentelemetry.deployment.logging;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.function.BooleanSupplier;
import java.util.function.Function;

import org.jboss.jandex.AnnotationInstance;
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.DotName;
import org.jboss.jandex.FieldInfo;
import org.jboss.jandex.IndexView;
import org.jboss.jandex.MethodInfo;

import io.opentelemetry.sdk.logs.LogRecordProcessor;
import io.opentelemetry.sdk.logs.export.LogRecordExporter;
import io.quarkus.agroal.spi.OpenTelemetryInitBuildItem;
import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
import io.quarkus.arc.processor.DotNames;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.annotations.Consume;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.LogHandlerBuildItem;
import io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig;
import io.quarkus.opentelemetry.runtime.logs.OpenTelemetryLogConfig;
Expand All @@ -18,6 +33,57 @@
@BuildSteps(onlyIf = LogHandlerProcessor.LogsEnabled.class)
class LogHandlerProcessor {

private static final DotName LOG_RECORD_EXPORTER = DotName.createSimple(LogRecordExporter.class.getName());
private static final DotName LOG_RECORD_PROCESSOR = DotName.createSimple(LogRecordProcessor.class.getName());
// private static final DotName LOG_RECORD_HANDLER = DotName.createSimple(Handler.class.getName());

@BuildStep
UnremovableBeanBuildItem ensureProducersAreRetained(
CombinedIndexBuildItem indexBuildItem) {

IndexView index = indexBuildItem.getIndex();

// Find all known SpanExporters and SpanProcessors
Collection<String> knownClasses = new HashSet<>();
knownClasses.add(LOG_RECORD_EXPORTER.toString());
index.getAllKnownImplementors(LOG_RECORD_EXPORTER)
.forEach(classInfo -> knownClasses.add(classInfo.name().toString()));

knownClasses.add(LOG_RECORD_PROCESSOR.toString());
index.getAllKnownImplementors(LOG_RECORD_PROCESSOR)
.forEach(classInfo -> knownClasses.add(classInfo.name().toString()));

// knownClasses.add(LOG_RECORD_HANDLER.toString());
// index.getAllKnownImplementors(LOG_RECORD_HANDLER)
// .forEach(classInfo -> knownClasses.add(classInfo.name().toString()));

Set<String> retainProducers = new HashSet<>();

for (AnnotationInstance annotation : index.getAnnotations(DotNames.PRODUCES)) {
AnnotationTarget target = annotation.target();
switch (target.kind()) {
case METHOD:
MethodInfo method = target.asMethod();
String returnType = method.returnType().name().toString();
if (knownClasses.contains(returnType)) {
retainProducers.add(method.declaringClass().name().toString());
}
break;
case FIELD:
FieldInfo field = target.asField();
String fieldType = field.type().name().toString();
if (knownClasses.contains(fieldType)) {
retainProducers.add(field.declaringClass().name().toString());
}
break;
default:
break;
}
}

return new UnremovableBeanBuildItem(new UnremovableBeanBuildItem.BeanClassNamesExclusion(retainProducers));
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
@Consume(OpenTelemetryInitBuildItem.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import static io.opentelemetry.semconv.incubating.ThreadIncubatingAttributes.THREAD_NAME;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.File;
import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;
Expand Down Expand Up @@ -78,7 +79,7 @@ public void testLoggingData() {
.containsEntry(THREAD_NAME.getKey(), Thread.currentThread().getName())
.containsEntry(THREAD_ID.getKey(), Thread.currentThread().getId())
.containsEntry("log.logger.namespace", "org.jboss.logging.Logger")
.containsEntry(LOG_FILE_PATH, "target/quarkus.log")
.containsEntry(LOG_FILE_PATH, "target" + File.separator + "quarkus.log")
.containsKey(CODE_LINENO.getKey())
.doesNotContainKey(EXCEPTION_TYPE)
.doesNotContainKey(EXCEPTION_MESSAGE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import io.opentelemetry.api.logs.Severity;

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

private final OpenTelemetry openTelemetry;

public OpenTelemetryLogHandler(final OpenTelemetry openTelemetry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ public class LogsExporterCDIProvider implements ConfigurableLogRecordExporterPro
@Override
public LogRecordExporter createExporter(ConfigProperties configProperties) {
Instance<LogRecordExporter> exporters = CDI.current().select(LogRecordExporter.class, Any.Literal.INSTANCE);
log.fine("available exporters: " + exporters.stream()
log.info("available exporters: " + exporters.stream()
.map(e -> e.getClass().getName())
.reduce((a, b) -> a + ", " + b)
.orElse("none"));
if (exporters.isUnsatisfied()) {
return NoopLogRecordExporter.INSTANCE;
} else {
log.info("using exporter: " + exporters.get().getClass().getName());
return exporters.get();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ public List<MetricData> exportMetrics(@QueryParam("name") String name, @QueryPar
public List<LogRecordData> exportLogs(@QueryParam("body") String message) {
if (message == null) {
return inMemoryLogRecordExporter.getFinishedLogRecordItems().stream()
.filter(logRecordData -> !logRecordData.getAttributes().asMap()
.get(AttributeKey.stringKey("code.namespace"))
.equals("io.quarkus.bootstrap.runner.Timing"))
.collect(Collectors.toList());
}
return inMemoryLogRecordExporter.getFinishedLogRecordItems().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void testDirectEndpoint() {
.body("message", equalTo("Direct trace"));

// Wait for logs to be available as everything is async
await().atMost(Duration.ofSeconds(100)).until(() -> getLogs("directTrace called").size() == 1);
await().atMost(Duration.ofSeconds(5)).until(() -> getLogs("directTrace called").size() == 1);
Map<String, Object> logLine = getLogs("directTrace called").get(0);

await().atMost(Duration.ofMinutes(2)).until(() -> getSpans().size() == 1);
Expand All @@ -107,7 +107,7 @@ public void testException() {
.body(is("Oh no! An exception"));

// Wait for logs to be available as everything is async
await().atMost(Duration.ofSeconds(100)).until(() -> getLogs("Oh no Exception!").size() == 1);
await().atMost(Duration.ofSeconds(5)).until(() -> getLogs("Oh no Exception!").size() == 1);
Map<String, Object> logLine = getLogs("Oh no Exception!").get(0);

await().atMost(Duration.ofMinutes(2)).until(() -> getSpans().size() == 1);
Expand Down Expand Up @@ -156,7 +156,14 @@ void testChainedResourceTracing() {
assertEquals("200", server.get("attr_http.response.status_code"));

// Wait for logs to be available as everything is async
await().atMost(Duration.ofSeconds(100)).until(() -> getLogs("chainedTrace called").size() == 1);
await().atMost(Duration.ofSeconds(5)).until(() -> {
if (getLogs("chainedTrace called").size() == 1) {
return true;
} else {
System.out.println(getLogs());
return false;
}
});
final Map<String, Object> serverLine = getLogs("chainedTrace called").get(0);

final Map<String, Object> serverLineSpanContext = (Map<String, Object>) serverLine.get("spanContext");
Expand All @@ -171,7 +178,7 @@ void testChainedResourceTracing() {
assertEquals(SpanKind.INTERNAL.toString(), cdi.get("kind"));
assertEquals(server.get("spanId"), cdi.get("parent_spanId"));

await().atMost(Duration.ofSeconds(100)).until(() -> getLogs("Chained trace called").size() == 1);
await().atMost(Duration.ofSeconds(5)).until(() -> getLogs("Chained trace called").size() == 1);
final Map<String, Object> cdiLine = getLogs("Chained trace called").get(0);

final Map<String, Object> cdiLineSpanContext = (Map<String, Object>) cdiLine.get("spanContext");
Expand Down

0 comments on commit 3ec1df9

Please sign in to comment.