Skip to content

Commit

Permalink
Fix OpenTelemetry service name priorities
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvavrik committed Aug 4, 2023
1 parent f6f0849 commit 78cd5b7
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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<SpanData> 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";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<SpanData> 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";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,18 @@ private TracerUtil() {
}

public static Resource mapResourceAttributes(List<String> 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());
}
}

0 comments on commit 78cd5b7

Please sign in to comment.