Skip to content
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 @@ -13,7 +13,9 @@
import io.opentelemetry.sdk.common.internal.SemConvConstants;
import io.opentelemetry.sdk.common.internal.Signal;
import io.opentelemetry.sdk.common.internal.StandardComponentId;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.function.Supplier;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -55,10 +57,22 @@ public ExporterInstrumentation(
static Attributes extractServerAttributes(URI httpEndpoint) {
AttributesBuilder builder = Attributes.builder();
String host = httpEndpoint.getHost();
int port = httpEndpoint.getPort();
if (host == null) {
// URI.getHost() returns null for some valid DNS names (JDK-8188305); URL accepts them.
try {
URL url = httpEndpoint.toURL();
if (!url.getHost().isEmpty()) {
host = url.getHost();
}
port = url.getPort();
} catch (MalformedURLException e) {
// Not a URL either, leave host unset.
}
}
if (host != null) {
builder.put(SemConvConstants.SERVER_ADDRESS, host);
}
int port = httpEndpoint.getPort();
if (port == -1) {
String scheme = httpEndpoint.getScheme();
if ("https".equals(scheme)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,24 @@ void serverAttributesUnknownScheme() {
.containsEntry(SemConvConstants.SERVER_ADDRESS, "foo")
.containsEntry(SemConvConstants.SERVER_PORT, 1234);
}

@Test
void serverAttributesHostNotParsedByUri() {
// A label starting with a digit is a valid DNS name, but URI.getHost() returns null for it.
assertThat(URI.create("http://otlp.1234-ns:4318/v1/traces").getHost()).isNull();

assertThat(
ExporterInstrumentation.extractServerAttributes(
URI.create("http://otlp.1234-ns:4318/v1/traces")))
.hasSize(2)
.containsEntry(SemConvConstants.SERVER_ADDRESS, "otlp.1234-ns")
.containsEntry(SemConvConstants.SERVER_PORT, 4318);

assertThat(
ExporterInstrumentation.extractServerAttributes(
URI.create("https://otlp.1234-ns/v1/traces")))
.hasSize(2)
.containsEntry(SemConvConstants.SERVER_ADDRESS, "otlp.1234-ns")
.containsEntry(SemConvConstants.SERVER_PORT, 443);
}
}
Loading