From 78cd5b76dfde8719ef4e79dd934ee1af23a6f53f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Vav=C5=99=C3=ADk?= Date: Fri, 4 Aug 2023 11:40:48 +0200 Subject: [PATCH] Fix OpenTelemetry service name priorities --- .../OpenTelemetryResourceAttrSvcNameTest.java | 68 +++++++++++++++++++ .../OpenTelemetryServiceNameTest.java | 3 - ...elemetrySvcNameAndNoResourceAttrsTest.java | 68 +++++++++++++++++++ .../runtime/tracing/TracerUtil.java | 16 ++--- 4 files changed, 143 insertions(+), 12 deletions(-) create mode 100644 extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryResourceAttrSvcNameTest.java create mode 100644 extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetrySvcNameAndNoResourceAttrsTest.java diff --git a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryResourceAttrSvcNameTest.java b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryResourceAttrSvcNameTest.java new file mode 100644 index 00000000000000..a541be538f478b --- /dev/null +++ b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryResourceAttrSvcNameTest.java @@ -0,0 +1,68 @@ +package io.quarkus.opentelemetry.deployment; + +import static io.opentelemetry.api.trace.SpanKind.SERVER; +import static io.quarkus.opentelemetry.deployment.common.TestSpanExporter.getSpanByKindAndParentId; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; + +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +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.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.sdk.trace.data.SpanData; +import io.quarkus.opentelemetry.deployment.common.TestSpanExporter; +import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider; +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class OpenTelemetryResourceAttrSvcNameTest { + + private static final String SERVICE_NAME = "FrankBullitt"; + + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer( + () -> ShrinkWrap.create(JavaArchive.class) + .addClass(TestSpanExporter.class) + .addClass(TestSpanExporterProvider.class) + .addAsResource(new StringAsset("" + + "quarkus.otel.traces.exporter=test-span-exporter\n" + + "quarkus.otel.bsp.schedule.delay=50\n"), "application.properties") + .addAsResource( + "META-INF/services-config/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider", + "META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider")) + .overrideRuntimeConfigKey("quarkus.otel.resource.attributes", "service.name=" + SERVICE_NAME); + + @Inject + TestSpanExporter spanExporter; + + @Test + void testResourceAttrSvcNameHasPriorityOverAppName() { + RestAssured.when() + .get("/hello").then() + .statusCode(200) + .body(is("hello")); + + List spans = spanExporter.getFinishedSpanItems(1); + + final SpanData server = getSpanByKindAndParentId(spans, SERVER, "0000000000000000"); + assertEquals("GET /hello", server.getName()); + assertEquals(SERVICE_NAME, server.getResource().getAttribute(AttributeKey.stringKey("service.name"))); + } + + @Path("/hello") + public static class HelloResource { + @GET + public String hello() { + return "hello"; + } + } +} diff --git a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameTest.java b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameTest.java index 363cb7864c0499..5a1c62e56807db 100644 --- a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameTest.java +++ b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameTest.java @@ -22,7 +22,6 @@ import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider; import io.quarkus.test.QuarkusUnitTest; import io.restassured.RestAssured; -import io.smallrye.config.SmallRyeConfig; public class OpenTelemetryServiceNameTest { @@ -39,8 +38,6 @@ public class OpenTelemetryServiceNameTest { "META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider")) .overrideRuntimeConfigKey("quarkus.otel.service.name", SERVICE_NAME); - @Inject - SmallRyeConfig config; @Inject TestSpanExporter spanExporter; diff --git a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetrySvcNameAndNoResourceAttrsTest.java b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetrySvcNameAndNoResourceAttrsTest.java new file mode 100644 index 00000000000000..755a124d9b7ac8 --- /dev/null +++ b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetrySvcNameAndNoResourceAttrsTest.java @@ -0,0 +1,68 @@ +package io.quarkus.opentelemetry.deployment; + +import static io.opentelemetry.api.trace.SpanKind.SERVER; +import static io.quarkus.opentelemetry.deployment.common.TestSpanExporter.getSpanByKindAndParentId; +import static org.hamcrest.Matchers.is; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; + +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; + +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.Test; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.sdk.trace.data.SpanData; +import io.quarkus.opentelemetry.deployment.common.TestSpanExporter; +import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider; +import io.quarkus.test.QuarkusUnitTest; +import io.restassured.RestAssured; + +public class OpenTelemetrySvcNameAndNoResourceAttrsTest { + + private static final String SERVICE_NAME = "FrankBullitt"; + + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer( + () -> ShrinkWrap.create(JavaArchive.class) + .addClass(TestSpanExporter.class) + .addClass(TestSpanExporterProvider.class) + .addAsResource(new StringAsset("" + + "quarkus.otel.traces.exporter=test-span-exporter\n" + + "quarkus.otel.bsp.schedule.delay=50\n"), "application.properties") + .addAsResource( + "META-INF/services-config/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider", + "META-INF/services/io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider")) + .overrideRuntimeConfigKey("quarkus.otel.service.name", SERVICE_NAME); + + @Inject + TestSpanExporter spanExporter; + + @Test + void testSvcNameHasPriorityOverAppNameWhenNoResourceAttrs() { + RestAssured.when() + .get("/hello").then() + .statusCode(200) + .body(is("hello")); + + List spans = spanExporter.getFinishedSpanItems(1); + + final SpanData server = getSpanByKindAndParentId(spans, SERVER, "0000000000000000"); + assertEquals("GET /hello", server.getName()); + assertEquals(SERVICE_NAME, server.getResource().getAttribute(AttributeKey.stringKey("service.name"))); + } + + @Path("/hello") + public static class HelloResource { + @GET + public String hello() { + return "hello"; + } + } +} diff --git a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/TracerUtil.java b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/TracerUtil.java index 3c9a6ff394bfce..84ebd239996fa2 100644 --- a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/TracerUtil.java +++ b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/tracing/TracerUtil.java @@ -15,20 +15,18 @@ private TracerUtil() { } public static Resource mapResourceAttributes(List resourceAttributes, String serviceName) { - if (resourceAttributes.isEmpty()) { - return Resource.empty(); + final AttributesBuilder attributesBuilder = Attributes.builder(); + + if (!resourceAttributes.isEmpty()) { + OpenTelemetryUtil + .convertKeyValueListToMap(resourceAttributes) + .forEach(attributesBuilder::put); } - AttributesBuilder attributesBuilder = Attributes.builder(); - var attrNameToValue = OpenTelemetryUtil.convertKeyValueListToMap(resourceAttributes); - // override both default (app name) and explicitly set resource attribute - // it needs to be done manually because OpenTelemetry correctly sets 'otel.service.name' - // to existing (incoming) resource, but customizer output replaces originally set service name if (serviceName != null) { - attrNameToValue.put(SERVICE_NAME.getKey(), serviceName); + attributesBuilder.put(SERVICE_NAME.getKey(), serviceName); } - attrNameToValue.forEach(attributesBuilder::put); return Resource.create(attributesBuilder.build()); } }