From c36cb73644834590fc34f3d89a8d891535ada668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Vav=C5=99=C3=ADk?= Date: Fri, 4 Aug 2023 15:51:40 +0200 Subject: [PATCH] Fix OpenTelemetry svc name precedence and improve test covergae --- .../OpenTelemetryServiceNameAppNameTest.java | 22 ++++++++++++++++ ... => OpenTelemetryServiceNameBaseTest.java} | 25 +++---------------- ...ryServiceNameCombinedResourceWinsTest.java | 22 ++++++++++++++++ ...tryServiceNameCombinedServiceWinsTest.java | 21 ++++++++++++++++ ...lemetryServiceNameNoResourceAttrsTest.java | 20 +++++++++++++++ ...nTelemetryServiceNameResourceAttrTest.java | 20 +++++++++++++++ .../runtime/tracing/TracerUtil.java | 16 ++++++------ 7 files changed, 115 insertions(+), 31 deletions(-) create mode 100644 extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameAppNameTest.java rename extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/{OpenTelemetryServiceNameTest.java => OpenTelemetryServiceNameBaseTest.java} (51%) create mode 100644 extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameCombinedResourceWinsTest.java create mode 100644 extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameCombinedServiceWinsTest.java create mode 100644 extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameNoResourceAttrsTest.java create mode 100644 extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameResourceAttrTest.java diff --git a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameAppNameTest.java b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameAppNameTest.java new file mode 100644 index 0000000000000..e6c3d688f0ba1 --- /dev/null +++ b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameAppNameTest.java @@ -0,0 +1,22 @@ +package io.quarkus.opentelemetry.deployment; + +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.extension.RegisterExtension; + +import io.quarkus.opentelemetry.deployment.common.TestSpanExporter; +import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider; +import io.quarkus.test.QuarkusUnitTest; + +public class OpenTelemetryServiceNameAppNameTest extends OpenTelemetryServiceNameBaseTest { + + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer( + () -> ShrinkWrap.create(JavaArchive.class) + .addClass(TestSpanExporter.class) + .addClass(TestSpanExporterProvider.class) + .addAsResource(new StringAsset("" + + "quarkus.otel.bsp.schedule.delay=50\n" + + "quarkus.application.name=" + SERVICE_NAME + "\n"), "application.properties")); +} 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/OpenTelemetryServiceNameBaseTest.java similarity index 51% rename from extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameTest.java rename to extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameBaseTest.java index 363cb7864c049..1b9f3f3d6cee4 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/OpenTelemetryServiceNameBaseTest.java @@ -11,41 +11,22 @@ import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; -import org.jboss.shrinkwrap.api.ShrinkWrap; -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; -import io.smallrye.config.SmallRyeConfig; -public class OpenTelemetryServiceNameTest { +public abstract class OpenTelemetryServiceNameBaseTest { - private static final String SERVICE_NAME = "FrankBullitt"; + protected 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("resource-config/application.properties", "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 - SmallRyeConfig config; @Inject TestSpanExporter spanExporter; @Test - void testSvcNameHasPriorityOverAppNameAndResourceAttr() { + void testServiceName() { RestAssured.when() .get("/hello").then() .statusCode(200) diff --git a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameCombinedResourceWinsTest.java b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameCombinedResourceWinsTest.java new file mode 100644 index 0000000000000..cb40e024aeab7 --- /dev/null +++ b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameCombinedResourceWinsTest.java @@ -0,0 +1,22 @@ +package io.quarkus.opentelemetry.deployment; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.opentelemetry.deployment.common.TestSpanExporter; +import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider; +import io.quarkus.test.QuarkusUnitTest; + +public class OpenTelemetryServiceNameCombinedResourceWinsTest extends OpenTelemetryServiceNameBaseTest { + + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer( + () -> ShrinkWrap.create(JavaArchive.class) + .addClass(TestSpanExporter.class) + .addClass(TestSpanExporterProvider.class)) + .overrideConfigKey("quarkus.application.name", "application-name-must-fail") + .overrideRuntimeConfigKey("quarkus.otel.bsp.schedule.delay", "50")// speed up test + .overrideRuntimeConfigKey("quarkus.otel.service.name", SERVICE_NAME) + .overrideRuntimeConfigKey("quarkus.otel.resource.attributes", "service.name=" + "attributes-must-fail"); +} diff --git a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameCombinedServiceWinsTest.java b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameCombinedServiceWinsTest.java new file mode 100644 index 0000000000000..39a3ad1aae3ba --- /dev/null +++ b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameCombinedServiceWinsTest.java @@ -0,0 +1,21 @@ +package io.quarkus.opentelemetry.deployment; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.opentelemetry.deployment.common.TestSpanExporter; +import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider; +import io.quarkus.test.QuarkusUnitTest; + +public class OpenTelemetryServiceNameCombinedServiceWinsTest extends OpenTelemetryServiceNameBaseTest { + + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer( + () -> ShrinkWrap.create(JavaArchive.class) + .addClass(TestSpanExporter.class) + .addClass(TestSpanExporterProvider.class)) + .overrideConfigKey("quarkus.application.name", "application-name-must-fail") + .overrideRuntimeConfigKey("quarkus.otel.bsp.schedule.delay", "50")// speed up test + .overrideRuntimeConfigKey("quarkus.otel.resource.attributes", "service.name=" + SERVICE_NAME); +} diff --git a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameNoResourceAttrsTest.java b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameNoResourceAttrsTest.java new file mode 100644 index 0000000000000..c693d9961fe86 --- /dev/null +++ b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameNoResourceAttrsTest.java @@ -0,0 +1,20 @@ +package io.quarkus.opentelemetry.deployment; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.opentelemetry.deployment.common.TestSpanExporter; +import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider; +import io.quarkus.test.QuarkusUnitTest; + +public class OpenTelemetryServiceNameNoResourceAttrsTest extends OpenTelemetryServiceNameBaseTest { + + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer( + () -> ShrinkWrap.create(JavaArchive.class) + .addClass(TestSpanExporter.class) + .addClass(TestSpanExporterProvider.class)) + .overrideConfigKey("quarkus.otel.service.name", SERVICE_NAME) + .overrideRuntimeConfigKey("quarkus.otel.bsp.schedule.delay", "50");// speed up test +} diff --git a/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameResourceAttrTest.java b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameResourceAttrTest.java new file mode 100644 index 0000000000000..c980f13dc641b --- /dev/null +++ b/extensions/opentelemetry/deployment/src/test/java/io/quarkus/opentelemetry/deployment/OpenTelemetryServiceNameResourceAttrTest.java @@ -0,0 +1,20 @@ +package io.quarkus.opentelemetry.deployment; + +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.jupiter.api.extension.RegisterExtension; + +import io.quarkus.opentelemetry.deployment.common.TestSpanExporter; +import io.quarkus.opentelemetry.deployment.common.TestSpanExporterProvider; +import io.quarkus.test.QuarkusUnitTest; + +public class OpenTelemetryServiceNameResourceAttrTest extends OpenTelemetryServiceNameBaseTest { + + @RegisterExtension + static final QuarkusUnitTest TEST = new QuarkusUnitTest().setArchiveProducer( + () -> ShrinkWrap.create(JavaArchive.class) + .addClass(TestSpanExporter.class) + .addClass(TestSpanExporterProvider.class)) + .overrideRuntimeConfigKey("quarkus.otel.bsp.schedule.delay", "50")// speed up test + .overrideRuntimeConfigKey("quarkus.otel.resource.attributes", "service.name=" + SERVICE_NAME); +} 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 3c9a6ff394bfc..84ebd239996fa 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()); } }