diff --git a/exporters/otlp-http/logs/src/test/java/io/opentelemetry/exporter/otlp/http/logs/OtlpHttpLogExporterTest.java b/exporters/otlp-http/logs/src/test/java/io/opentelemetry/exporter/otlp/http/logs/OtlpHttpLogExporterTest.java index 33f1681b327..2ce87e8c15e 100644 --- a/exporters/otlp-http/logs/src/test/java/io/opentelemetry/exporter/otlp/http/logs/OtlpHttpLogExporterTest.java +++ b/exporters/otlp-http/logs/src/test/java/io/opentelemetry/exporter/otlp/http/logs/OtlpHttpLogExporterTest.java @@ -61,13 +61,15 @@ class OtlpHttpLogExporterTest { private static final MediaType APPLICATION_PROTOBUF = MediaType.create("application", "x-protobuf"); private static final HeldCertificate HELD_CERTIFICATE; + private static final String canonicalHostName; static { try { + canonicalHostName = InetAddress.getByName("localhost").getCanonicalHostName(); HELD_CERTIFICATE = new HeldCertificate.Builder() .commonName("localhost") - .addSubjectAlternativeName(InetAddress.getByName("localhost").getCanonicalHostName()) + .addSubjectAlternativeName(canonicalHostName) .build(); } catch (UnknownHostException e) { throw new IllegalStateException("Error building certificate.", e); @@ -94,7 +96,7 @@ protected void configureServer(ServerBuilder sb) { void setup() { builder = OtlpHttpLogExporter.builder() - .setEndpoint("http://localhost:" + server.httpPort() + "/v1/logs") + .setEndpoint("http://" + canonicalHostName + ":" + server.httpPort() + "/v1/logs") .addHeader("foo", "bar"); } @@ -110,13 +112,20 @@ void validConfig() { assertThatCode(() -> OtlpHttpLogExporter.builder().setTimeout(Duration.ofMillis(10))) .doesNotThrowAnyException(); - assertThatCode(() -> OtlpHttpLogExporter.builder().setEndpoint("http://localhost:4317")) + assertThatCode( + () -> + OtlpHttpLogExporter.builder().setEndpoint("http://" + canonicalHostName + ":4317")) .doesNotThrowAnyException(); - assertThatCode(() -> OtlpHttpLogExporter.builder().setEndpoint("http://localhost")) + assertThatCode( + () -> OtlpHttpLogExporter.builder().setEndpoint("http://" + canonicalHostName + "")) .doesNotThrowAnyException(); - assertThatCode(() -> OtlpHttpLogExporter.builder().setEndpoint("https://localhost")) + assertThatCode( + () -> OtlpHttpLogExporter.builder().setEndpoint("https://" + canonicalHostName + "")) .doesNotThrowAnyException(); - assertThatCode(() -> OtlpHttpLogExporter.builder().setEndpoint("http://foo:bar@localhost")) + assertThatCode( + () -> + OtlpHttpLogExporter.builder() + .setEndpoint("http://foo:bar@" + canonicalHostName + "")) .doesNotThrowAnyException(); assertThatCode(() -> OtlpHttpLogExporter.builder().setCompression("gzip")) @@ -151,15 +160,21 @@ void invalidConfig() { assertThatThrownBy(() -> OtlpHttpLogExporter.builder().setEndpoint(null)) .isInstanceOf(NullPointerException.class) .hasMessage("endpoint"); - assertThatThrownBy(() -> OtlpHttpLogExporter.builder().setEndpoint("😺://localhost")) + assertThatThrownBy( + () -> OtlpHttpLogExporter.builder().setEndpoint("😺://" + canonicalHostName + "")) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Invalid endpoint, must be a URL: 😺://localhost"); - assertThatThrownBy(() -> OtlpHttpLogExporter.builder().setEndpoint("localhost")) + .hasMessage("Invalid endpoint, must be a URL: 😺://" + canonicalHostName + ""); + assertThatThrownBy(() -> OtlpHttpLogExporter.builder().setEndpoint("" + canonicalHostName + "")) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Invalid endpoint, must start with http:// or https://: localhost"); - assertThatThrownBy(() -> OtlpHttpLogExporter.builder().setEndpoint("gopher://localhost")) + .hasMessage( + "Invalid endpoint, must start with http:// or https://: " + canonicalHostName + ""); + assertThatThrownBy( + () -> OtlpHttpLogExporter.builder().setEndpoint("gopher://" + canonicalHostName + "")) .isInstanceOf(IllegalArgumentException.class) - .hasMessage("Invalid endpoint, must start with http:// or https://: gopher://localhost"); + .hasMessage( + "Invalid endpoint, must start with http:// or https://: gopher://" + + canonicalHostName + + ""); assertThatThrownBy(() -> OtlpHttpLogExporter.builder().setCompression(null)) .isInstanceOf(NullPointerException.class) @@ -190,7 +205,7 @@ void testExportTls() { server.enqueue(successResponse()); OtlpHttpLogExporter exporter = builder - .setEndpoint("https://localhost:" + server.httpsPort() + "/v1/logs") + .setEndpoint("https://" + canonicalHostName + ":" + server.httpsPort() + "/v1/logs") .setTrustedCertificates( HELD_CERTIFICATE.certificatePem().getBytes(StandardCharsets.UTF_8)) .build(); diff --git a/exporters/otlp-http/metrics/src/test/java/io/opentelemetry/exporter/otlp/http/metrics/OtlpHttpMetricExporterTest.java b/exporters/otlp-http/metrics/src/test/java/io/opentelemetry/exporter/otlp/http/metrics/OtlpHttpMetricExporterTest.java index 41b3a6d2beb..436a6fa26e4 100644 --- a/exporters/otlp-http/metrics/src/test/java/io/opentelemetry/exporter/otlp/http/metrics/OtlpHttpMetricExporterTest.java +++ b/exporters/otlp-http/metrics/src/test/java/io/opentelemetry/exporter/otlp/http/metrics/OtlpHttpMetricExporterTest.java @@ -59,13 +59,15 @@ class OtlpHttpMetricExporterTest { private static final MediaType APPLICATION_PROTOBUF = MediaType.create("application", "x-protobuf"); private static final HeldCertificate HELD_CERTIFICATE; + private static final String canonicalHostName; static { try { + canonicalHostName = InetAddress.getByName("localhost").getCanonicalHostName(); HELD_CERTIFICATE = new HeldCertificate.Builder() .commonName("localhost") - .addSubjectAlternativeName(InetAddress.getByName("localhost").getCanonicalHostName()) + .addSubjectAlternativeName(canonicalHostName) .build(); } catch (UnknownHostException e) { throw new IllegalStateException("Error building certificate.", e); @@ -90,7 +92,7 @@ protected void configureServer(ServerBuilder sb) { void setup() { builder = OtlpHttpMetricExporter.builder() - .setEndpoint("https://localhost:" + server.httpsPort() + "/v1/metrics") + .setEndpoint("https://" + canonicalHostName + ":" + server.httpsPort() + "/v1/metrics") .addHeader("foo", "bar") .setTrustedCertificates( HELD_CERTIFICATE.certificatePem().getBytes(StandardCharsets.UTF_8)); diff --git a/exporters/otlp-http/trace/src/test/java/io/opentelemetry/exporter/otlp/http/trace/OtlpHttpSpanExporterTest.java b/exporters/otlp-http/trace/src/test/java/io/opentelemetry/exporter/otlp/http/trace/OtlpHttpSpanExporterTest.java index 5b58bc4e6b6..a8e2b403caf 100644 --- a/exporters/otlp-http/trace/src/test/java/io/opentelemetry/exporter/otlp/http/trace/OtlpHttpSpanExporterTest.java +++ b/exporters/otlp-http/trace/src/test/java/io/opentelemetry/exporter/otlp/http/trace/OtlpHttpSpanExporterTest.java @@ -60,13 +60,15 @@ class OtlpHttpSpanExporterTest { private static final MediaType APPLICATION_PROTOBUF = MediaType.create("application", "x-protobuf"); private static final HeldCertificate HELD_CERTIFICATE; + private static final String canonicalHostName; static { try { + canonicalHostName = InetAddress.getByName("localhost").getCanonicalHostName(); HELD_CERTIFICATE = new HeldCertificate.Builder() .commonName("localhost") - .addSubjectAlternativeName(InetAddress.getByName("localhost").getCanonicalHostName()) + .addSubjectAlternativeName(canonicalHostName) .build(); } catch (UnknownHostException e) { throw new IllegalStateException("Error building certificate.", e); @@ -93,7 +95,7 @@ protected void configureServer(ServerBuilder sb) { void setup() { builder = OtlpHttpSpanExporter.builder() - .setEndpoint("http://localhost:" + server.httpPort() + "/v1/traces") + .setEndpoint("http://" + canonicalHostName + ":" + server.httpPort() + "/v1/traces") .addHeader("foo", "bar"); } @@ -189,7 +191,7 @@ void testExportTls() { server.enqueue(successResponse()); OtlpHttpSpanExporter exporter = builder - .setEndpoint("https://localhost:" + server.httpsPort() + "/v1/traces") + .setEndpoint("https://" + canonicalHostName + ":" + server.httpsPort() + "/v1/traces") .setTrustedCertificates( HELD_CERTIFICATE.certificatePem().getBytes(StandardCharsets.UTF_8)) .build(); diff --git a/exporters/otlp/logs/src/testOkHttpOnly/java/io/opentelemetry/exporter/otlp/logs/OkHttpOnlyExportTest.java b/exporters/otlp/logs/src/testOkHttpOnly/java/io/opentelemetry/exporter/otlp/logs/OkHttpOnlyExportTest.java index 02c2f08d281..832660e9aaa 100644 --- a/exporters/otlp/logs/src/testOkHttpOnly/java/io/opentelemetry/exporter/otlp/logs/OkHttpOnlyExportTest.java +++ b/exporters/otlp/logs/src/testOkHttpOnly/java/io/opentelemetry/exporter/otlp/logs/OkHttpOnlyExportTest.java @@ -55,13 +55,15 @@ class OkHttpOnlyExportTest { .build()); private static final HeldCertificate HELD_CERTIFICATE; + private static final String canonicalHostName; static { try { + canonicalHostName = InetAddress.getByName("localhost").getCanonicalHostName(); HELD_CERTIFICATE = new HeldCertificate.Builder() .commonName("localhost") - .addSubjectAlternativeName(InetAddress.getByName("localhost").getCanonicalHostName()) + .addSubjectAlternativeName(canonicalHostName) .build(); } catch (UnknownHostException e) { throw new IllegalStateException("Error building certificate.", e); @@ -98,7 +100,7 @@ protected CompletionStage handleMessage( void gzipCompressionExport() { OtlpGrpcLogExporter exporter = OtlpGrpcLogExporter.builder() - .setEndpoint("http://localhost:" + server.httpPort()) + .setEndpoint("http://" + canonicalHostName + ":" + server.httpPort()) .setCompression("gzip") .build(); // See note on test method on why this checks isFalse. @@ -108,7 +110,9 @@ void gzipCompressionExport() { @Test void plainTextExport() { OtlpGrpcLogExporter exporter = - OtlpGrpcLogExporter.builder().setEndpoint("http://localhost:" + server.httpPort()).build(); + OtlpGrpcLogExporter.builder() + .setEndpoint("http://" + canonicalHostName + ":" + server.httpPort()) + .build(); assertThat(exporter.export(LOGS).join(10, TimeUnit.SECONDS).isSuccess()).isTrue(); } @@ -116,7 +120,7 @@ void plainTextExport() { void authorityWithAuth() { OtlpGrpcLogExporter exporter = OtlpGrpcLogExporter.builder() - .setEndpoint("http://foo:bar@localhost:" + server.httpPort()) + .setEndpoint("http://foo:bar@" + canonicalHostName + ":" + server.httpPort()) .build(); assertThat(exporter.export(LOGS).join(10, TimeUnit.SECONDS).isSuccess()).isTrue(); } @@ -125,7 +129,7 @@ void authorityWithAuth() { void testTlsExport() { OtlpGrpcLogExporter exporter = OtlpGrpcLogExporter.builder() - .setEndpoint("https://localhost:" + server.httpsPort()) + .setEndpoint("https://" + canonicalHostName + ":" + server.httpsPort()) .setTrustedCertificates( HELD_CERTIFICATE.certificatePem().getBytes(StandardCharsets.UTF_8)) .build(); @@ -136,7 +140,7 @@ void testTlsExport() { void testTlsExport_untrusted() { OtlpGrpcLogExporter exporter = OtlpGrpcLogExporter.builder() - .setEndpoint("https://localhost:" + server.httpsPort()) + .setEndpoint("https://" + canonicalHostName + ":" + server.httpsPort()) .build(); assertThat(exporter.export(LOGS).join(10, TimeUnit.SECONDS).isSuccess()).isFalse(); } diff --git a/exporters/otlp/metrics/src/testOkHttpOnly/java/io/opentelemetry/exporter/otlp/metrics/OkHttpOnlyExportTest.java b/exporters/otlp/metrics/src/testOkHttpOnly/java/io/opentelemetry/exporter/otlp/metrics/OkHttpOnlyExportTest.java index f1e3f372f17..440a8bb3855 100644 --- a/exporters/otlp/metrics/src/testOkHttpOnly/java/io/opentelemetry/exporter/otlp/metrics/OkHttpOnlyExportTest.java +++ b/exporters/otlp/metrics/src/testOkHttpOnly/java/io/opentelemetry/exporter/otlp/metrics/OkHttpOnlyExportTest.java @@ -57,13 +57,15 @@ class OkHttpOnlyExportTest { 5))))); private static final HeldCertificate HELD_CERTIFICATE; + private static final String canonicalHostName; static { try { + canonicalHostName = InetAddress.getByName("localhost").getCanonicalHostName(); HELD_CERTIFICATE = new HeldCertificate.Builder() .commonName("localhost") - .addSubjectAlternativeName(InetAddress.getByName("localhost").getCanonicalHostName()) + .addSubjectAlternativeName(canonicalHostName) .build(); } catch (UnknownHostException e) { throw new IllegalStateException("Error building certificate.", e); @@ -100,7 +102,7 @@ protected CompletionStage handleMessage( void gzipCompressionExportButFails() { OtlpGrpcMetricExporter exporter = OtlpGrpcMetricExporter.builder() - .setEndpoint("http://localhost:" + server.httpPort()) + .setEndpoint("http://" + canonicalHostName + ":" + server.httpPort()) .setCompression("gzip") .build(); // See note on test method on why this checks isFalse. @@ -111,7 +113,7 @@ void gzipCompressionExportButFails() { void plainTextExport() { OtlpGrpcMetricExporter exporter = OtlpGrpcMetricExporter.builder() - .setEndpoint("http://localhost:" + server.httpPort()) + .setEndpoint("http://" + canonicalHostName + ":" + server.httpPort()) .build(); assertThat(exporter.export(METRICS).join(10, TimeUnit.SECONDS).isSuccess()).isTrue(); } @@ -120,7 +122,7 @@ void plainTextExport() { void authorityWithAuth() { OtlpGrpcMetricExporter exporter = OtlpGrpcMetricExporter.builder() - .setEndpoint("http://foo:bar@localhost:" + server.httpPort()) + .setEndpoint("http://foo:bar@" + canonicalHostName + ":" + server.httpPort()) .build(); assertThat(exporter.export(METRICS).join(10, TimeUnit.SECONDS).isSuccess()).isTrue(); } @@ -129,7 +131,7 @@ void authorityWithAuth() { void testTlsExport() { OtlpGrpcMetricExporter exporter = OtlpGrpcMetricExporter.builder() - .setEndpoint("https://localhost:" + server.httpsPort()) + .setEndpoint("https://" + canonicalHostName + ":" + server.httpsPort()) .setTrustedCertificates( HELD_CERTIFICATE.certificatePem().getBytes(StandardCharsets.UTF_8)) .build(); @@ -140,7 +142,7 @@ void testTlsExport() { void testTlsExport_untrusted() { OtlpGrpcMetricExporter exporter = OtlpGrpcMetricExporter.builder() - .setEndpoint("https://localhost:" + server.httpsPort()) + .setEndpoint("https://" + canonicalHostName + ":" + server.httpsPort()) .build(); assertThat(exporter.export(METRICS).join(10, TimeUnit.SECONDS).isSuccess()).isFalse(); } diff --git a/exporters/otlp/trace/src/testOkhttpOnly/java/io/opentelemetry/exporter/otlp/trace/OkHttpOnlyExportTest.java b/exporters/otlp/trace/src/testOkhttpOnly/java/io/opentelemetry/exporter/otlp/trace/OkHttpOnlyExportTest.java index c47e0b402d2..1b0a1fc7aa0 100644 --- a/exporters/otlp/trace/src/testOkhttpOnly/java/io/opentelemetry/exporter/otlp/trace/OkHttpOnlyExportTest.java +++ b/exporters/otlp/trace/src/testOkhttpOnly/java/io/opentelemetry/exporter/otlp/trace/OkHttpOnlyExportTest.java @@ -45,13 +45,15 @@ class OkHttpOnlyExportTest { .build()); private static final HeldCertificate HELD_CERTIFICATE; + private static final String canonicalHostName; static { try { + canonicalHostName = InetAddress.getByName("localhost").getCanonicalHostName(); HELD_CERTIFICATE = new HeldCertificate.Builder() .commonName("localhost") - .addSubjectAlternativeName(InetAddress.getByName("localhost").getCanonicalHostName()) + .addSubjectAlternativeName(canonicalHostName) .build(); } catch (UnknownHostException e) { throw new IllegalStateException("Error building certificate.", e); @@ -88,7 +90,7 @@ protected CompletionStage handleMessage( void gzipCompressionExportAttemptedButFails() { OtlpGrpcSpanExporter exporter = OtlpGrpcSpanExporter.builder() - .setEndpoint("http://localhost:" + server.httpPort()) + .setEndpoint("http://" + canonicalHostName + ":" + server.httpPort()) .setCompression("gzip") .build(); @@ -99,7 +101,9 @@ void gzipCompressionExportAttemptedButFails() { @Test void plainTextExport() { OtlpGrpcSpanExporter exporter = - OtlpGrpcSpanExporter.builder().setEndpoint("http://localhost:" + server.httpPort()).build(); + OtlpGrpcSpanExporter.builder() + .setEndpoint("http://" + canonicalHostName + ":" + server.httpPort()) + .build(); assertThat(exporter.export(SPANS).join(10, TimeUnit.SECONDS).isSuccess()).isTrue(); } @@ -107,7 +111,7 @@ void plainTextExport() { void authorityWithAuth() { OtlpGrpcSpanExporter exporter = OtlpGrpcSpanExporter.builder() - .setEndpoint("http://foo:bar@localhost:" + server.httpPort()) + .setEndpoint("http://foo:bar@" + canonicalHostName + ":" + server.httpPort()) .build(); assertThat(exporter.export(SPANS).join(10, TimeUnit.SECONDS).isSuccess()).isTrue(); } @@ -116,7 +120,7 @@ void authorityWithAuth() { void testTlsExport() throws Exception { OtlpGrpcSpanExporter exporter = OtlpGrpcSpanExporter.builder() - .setEndpoint("https://localhost:" + server.httpsPort()) + .setEndpoint("https://" + canonicalHostName + ":" + server.httpsPort()) .setTrustedCertificates( HELD_CERTIFICATE.certificatePem().getBytes(StandardCharsets.UTF_8)) .build(); @@ -127,7 +131,7 @@ void testTlsExport() throws Exception { void testTlsExport_untrusted() { OtlpGrpcSpanExporter exporter = OtlpGrpcSpanExporter.builder() - .setEndpoint("https://localhost:" + server.httpsPort()) + .setEndpoint("https://" + canonicalHostName + ":" + server.httpsPort()) .build(); assertThat(exporter.export(SPANS).join(10, TimeUnit.SECONDS).isSuccess()).isFalse(); } diff --git a/sdk-extensions/autoconfigure/src/testOtlpHttp/java/io/opentelemetry/sdk/autoconfigure/OtlpHttpConfigTest.java b/sdk-extensions/autoconfigure/src/testOtlpHttp/java/io/opentelemetry/sdk/autoconfigure/OtlpHttpConfigTest.java index 953d3811af9..72fa3160173 100644 --- a/sdk-extensions/autoconfigure/src/testOtlpHttp/java/io/opentelemetry/sdk/autoconfigure/OtlpHttpConfigTest.java +++ b/sdk-extensions/autoconfigure/src/testOtlpHttp/java/io/opentelemetry/sdk/autoconfigure/OtlpHttpConfigTest.java @@ -42,6 +42,7 @@ import java.io.ByteArrayInputStream; import java.io.IOException; import java.net.InetAddress; +import java.net.UnknownHostException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -73,6 +74,15 @@ class OtlpHttpConfigTest { private static final BlockingQueue metricRequests = new LinkedBlockingDeque<>(); private static final BlockingQueue requestHeaders = new LinkedBlockingDeque<>(); + private static final String canonicalHostName; + + static { + try { + canonicalHostName = InetAddress.getByName("localhost").getCanonicalHostName(); + } catch (UnknownHostException e) { + throw new IllegalStateException("Error resolving canonical host name.", e); + } + } @RegisterExtension @Order(1) @@ -87,7 +97,7 @@ public void beforeAll(ExtensionContext context) throws Exception { heldCertificate = new HeldCertificate.Builder() .commonName("localhost") - .addSubjectAlternativeName(InetAddress.getByName("localhost").getCanonicalHostName()) + .addSubjectAlternativeName(canonicalHostName) .build(); Path file = Files.createTempFile("test-cert", ".pem"); Files.write(file, heldCertificate.certificatePem().getBytes(StandardCharsets.UTF_8)); @@ -161,7 +171,8 @@ public void tearDown() { void configureExportersGeneral() { Map props = new HashMap<>(); props.put("otel.exporter.otlp.protocol", "http/protobuf"); - props.put("otel.exporter.otlp.endpoint", "https://localhost:" + server.httpsPort()); + props.put( + "otel.exporter.otlp.endpoint", "https://" + canonicalHostName + ":" + server.httpsPort()); props.put("otel.exporter.otlp.certificate", certificateExtension.filePath); props.put("otel.exporter.otlp.headers", "header-key=header-value"); props.put("otel.exporter.otlp.compression", "gzip"); @@ -223,7 +234,7 @@ void configureSpanExporter() { props.put("otel.exporter.otlp.timeout", "10s"); props.put( "otel.exporter.otlp.traces.endpoint", - "https://localhost:" + server.httpsPort() + "/v1/traces"); + "https://" + canonicalHostName + ":" + server.httpsPort() + "/v1/traces"); props.put("otel.exporter.otlp.traces.certificate", certificateExtension.filePath); props.put("otel.exporter.otlp.traces.headers", "header-key=header-value"); props.put("otel.exporter.otlp.traces.compression", "gzip"); @@ -265,7 +276,7 @@ public void configureMetricExporter() { props.put("otel.exporter.otlp.timeout", "10s"); props.put( "otel.exporter.otlp.metrics.endpoint", - "https://localhost:" + server.httpsPort() + "/v1/metrics"); + "https://" + canonicalHostName + ":" + server.httpsPort() + "/v1/metrics"); props.put("otel.exporter.otlp.metrics.certificate", certificateExtension.filePath); props.put("otel.exporter.otlp.metrics.headers", "header-key=header-value"); props.put("otel.exporter.otlp.metrics.compression", "gzip"); @@ -349,7 +360,8 @@ private static MetricData generateFakeMetric() { void configuresGlobal() { System.setProperty("otel.exporter.otlp.protocol", "http/protobuf"); System.setProperty( - "otel.exporter.otlp.endpoint", "https://localhost:" + server.httpsPort() + "/"); + "otel.exporter.otlp.endpoint", + "https://" + canonicalHostName + ":" + server.httpsPort() + "/"); System.setProperty("otel.exporter.otlp.certificate", certificateExtension.filePath); System.setProperty("otel.imr.export.interval", "1s");