Skip to content

Commit

Permalink
Merge pull request #38087 from brunobat/38084-npe
Browse files Browse the repository at this point in the history
Fix NPE and otel quickstart test
  • Loading branch information
geoand authored Jan 9, 2024
2 parents e4585da + 04312f1 commit b206815
Show file tree
Hide file tree
Showing 14 changed files with 451 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public OTelFallbackConfigSourceInterceptor() {
@Override
public ConfigValue getValue(final ConfigSourceInterceptorContext context, final String name) {
ConfigValue value = super.getValue(context, name);
if (name.equals("quarkus.otel.traces.sampler")) {
if (value != null && name.equals("quarkus.otel.traces.sampler")) {
return value.withValue(LEGACY_SAMPLER_NAME_CONVERTER.convert(value.getValue()));
}
return value;
Expand Down
188 changes: 188 additions & 0 deletions integration-tests/opentelemetry-quickstart/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-integration-tests-parent</artifactId>
<version>999-SNAPSHOT</version>
</parent>

<artifactId>quarkus-integration-test-opentelemetry-quickstart</artifactId>
<name>Quarkus - Integration Tests - OpenTelemetry quickstart</name>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-opentelemetry</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive</artifactId>
</dependency>


<!-- Needed for InMemorySpanExporter to verify captured traces -->
<dependency>
<groupId>io.opentelemetry</groupId>
<artifactId>opentelemetry-sdk-testing</artifactId>
</dependency>

<!-- Test Dependencies -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>

<!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-arc-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-reactive-jackson-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-opentelemetry-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<vertx.disableWebsockets>false</vertx.disableWebsockets>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>native-image</id>
<activation>
<property>
<name>native</name>
</property>
</activation>

<properties>
<quarkus.package.type>native</quarkus.package.type>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>${native.surefire.skip}</skipTests>
<systemPropertyVariables>
<vertx.disableWebsockets>false</vertx.disableWebsockets>
</systemPropertyVariables>
</configuration>
</plugin>

<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<vertx.disableWebsockets>false</vertx.disableWebsockets>
<native.image.path>
${project.build.directory}/${project.build.finalName}-runner
</native.image.path>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.quarkus.it.opentelemetry;

import java.util.List;
import java.util.stream.Collectors;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.inject.Produces;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Response;

import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter;
import io.opentelemetry.sdk.trace.data.SpanData;

@Path("")
public class ExporterResource {
@Inject
InMemorySpanExporter inMemorySpanExporter;

@GET
@Path("/reset")
public Response reset() {
inMemorySpanExporter.reset();
return Response.ok().build();
}

@GET
@Path("/export")
public List<SpanData> export() {
return inMemorySpanExporter.getFinishedSpanItems()
.stream()
.filter(sd -> !sd.getName().contains("export") && !sd.getName().contains("reset"))
.collect(Collectors.toList());
}

@ApplicationScoped
static class InMemorySpanExporterProducer {
@Produces
@Singleton
InMemorySpanExporter inMemorySpanExporter() {
return InMemorySpanExporter.create();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.it.opentelemetry;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/hello")
public class GreetingResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello from RESTEasy Reactive";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkus.it.opentelemetry.output;

import jakarta.inject.Singleton;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;

import io.opentelemetry.sdk.trace.data.SpanData;
import io.quarkus.jackson.ObjectMapperCustomizer;

@Singleton
public class SpanDataModuleSerializer implements ObjectMapperCustomizer {
@Override
public void customize(ObjectMapper objectMapper) {
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(SpanData.class, new SpanDataSerializer());
objectMapper.registerModule(simpleModule);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package io.quarkus.it.opentelemetry.output;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import io.opentelemetry.sdk.trace.data.SpanData;

public class SpanDataSerializer extends StdSerializer<SpanData> {
public SpanDataSerializer() {
this(null);
}

public SpanDataSerializer(Class<SpanData> type) {
super(type);
}

@Override
public void serialize(SpanData spanData, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException {
jsonGenerator.writeStartObject();

jsonGenerator.writeStringField("spanId", spanData.getSpanId());
jsonGenerator.writeStringField("traceId", spanData.getTraceId());
jsonGenerator.writeStringField("name", spanData.getName());
jsonGenerator.writeStringField("kind", spanData.getKind().name());
jsonGenerator.writeBooleanField("ended", spanData.hasEnded());

jsonGenerator.writeStringField("parentSpanId", spanData.getParentSpanContext().getSpanId());
jsonGenerator.writeStringField("parent_spanId", spanData.getParentSpanContext().getSpanId());
jsonGenerator.writeStringField("parent_traceId", spanData.getParentSpanContext().getTraceId());
jsonGenerator.writeBooleanField("parent_remote", spanData.getParentSpanContext().isRemote());
jsonGenerator.writeBooleanField("parent_valid", spanData.getParentSpanContext().isValid());

spanData.getAttributes().forEach((k, v) -> {
try {
jsonGenerator.writeStringField("attr_" + k.getKey(), v.toString());
} catch (IOException e) {
e.printStackTrace();
}
});

spanData.getResource().getAttributes().forEach((k, v) -> {
try {
jsonGenerator.writeStringField("resource_" + k.getKey(), v.toString());
} catch (IOException e) {
e.printStackTrace();
}
});

jsonGenerator.writeEndObject();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<html><body>Test</body></html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# speed up build
quarkus.otel.bsp.schedule.delay=0
quarkus.otel.bsp.export.timeout=5s
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.it.opentelemetry;

import static io.restassured.RestAssured.get;

import java.util.List;
import java.util.Map;

import io.restassured.common.mapper.TypeRef;

public class BaseTest {

protected List<Map<String, Object>> getSpans() {
return get("/export").body().as(new TypeRef<>() {
});
}

protected void buildGlobalTelemetryInstance() {
// Do nothing in JVM mode
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkus.it.opentelemetry;

import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.OpenTelemetrySdkBuilder;
import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class OpenTelemetryDisabledIT extends OpenTelemetryDisabledTest {
@Override
protected void buildGlobalTelemetryInstance() {
// When running native tests the test class is outside the Quarkus application,
// so we need to set the propagator on the GlobalOpenTelemetry instance
OpenTelemetrySdkBuilder builder = OpenTelemetrySdk.builder();
builder.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()));
builder.buildAndRegisterGlobal();
}
}
Loading

0 comments on commit b206815

Please sign in to comment.