Skip to content

Commit

Permalink
Merge pull request quarkusio#36968 from brunobat/record-hostname
Browse files Browse the repository at this point in the history
Send host.name in all spans
  • Loading branch information
brunobat authored Nov 9, 2023
2 parents c4011fb + 9673b48 commit 2251ce3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static io.quarkus.opentelemetry.deployment.common.TestSpanExporter.getSpanByKindAndParentId;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import java.util.List;

Expand Down Expand Up @@ -54,6 +55,7 @@ void resource() {
assertEquals("authservice", server.getResource().getAttribute(AttributeKey.stringKey("service.name")));
assertEquals(config.getRawValue("quarkus.uuid"),
server.getResource().getAttribute(AttributeKey.stringKey("service.instance.id")));
assertNotNull(server.getResource().getAttribute(AttributeKey.stringKey("host.name")));
}

@Path("/hello")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.lang.Boolean.TRUE;
import static java.util.Collections.emptyList;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
Expand Down Expand Up @@ -71,12 +73,21 @@ public Resource apply(Resource existingResource, ConfigProperties configProperti
.filter(sn -> !sn.equals(appConfig.name.orElse("unset")))
.orElse(null);

// must be resolved at startup, once.
String hostname = null;
try {
hostname = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
hostname = "unknown";
}

// Merge resource instances with env attributes
Resource resource = resources.stream()
.reduce(Resource.empty(), Resource::merge)
.merge(TracerUtil.mapResourceAttributes(
oTelRuntimeConfig.resourceAttributes().orElse(emptyList()),
serviceName)); // from properties
serviceName, // from properties
hostname));
return consolidatedResource.merge(resource);
} else {
return Resource.builder().build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.opentelemetry.runtime.tracing;

import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.HOST_NAME;
import static io.opentelemetry.semconv.resource.attributes.ResourceAttributes.SERVICE_NAME;

import java.util.List;
Expand All @@ -14,7 +15,7 @@ public class TracerUtil {
private TracerUtil() {
}

public static Resource mapResourceAttributes(List<String> resourceAttributes, String serviceName) {
public static Resource mapResourceAttributes(List<String> resourceAttributes, String serviceName, String hostname) {
final AttributesBuilder attributesBuilder = Attributes.builder();

if (!resourceAttributes.isEmpty()) {
Expand All @@ -27,6 +28,10 @@ public static Resource mapResourceAttributes(List<String> resourceAttributes, St
attributesBuilder.put(SERVICE_NAME.getKey(), serviceName);
}

if (hostname != null) {
attributesBuilder.put(HOST_NAME, hostname);
}

return Resource.create(attributesBuilder.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void testMapResourceAttributes() {
"service.namespace=mynamespace",
"service.version=1.0",
"deployment.environment=production");
Resource resource = TracerUtil.mapResourceAttributes(resourceAttributes, null);
Resource resource = TracerUtil.mapResourceAttributes(resourceAttributes, null, null);
Attributes attributes = resource.getAttributes();
Assertions.assertThat(attributes.size()).isEqualTo(4);
Assertions.assertThat(attributes.get(ResourceAttributes.SERVICE_NAME)).isEqualTo("myservice");
Expand Down

0 comments on commit 2251ce3

Please sign in to comment.