Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send host.name in all spans #36968

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading