From f90cda32992750f7e71cd58605935d9f3b43f652 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Mon, 30 Oct 2023 09:52:33 +0100 Subject: [PATCH] Change the precedence between :authority and Host headers --- .../http/ForwardedHostAddressAndPortExtractor.java | 8 ++++---- .../http/ForwardedHostAddressAndPortExtractorTest.java | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHostAddressAndPortExtractor.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHostAddressAndPortExtractor.java index 14bdf04b63b1..073aea6c11d5 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHostAddressAndPortExtractor.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHostAddressAndPortExtractor.java @@ -36,15 +36,15 @@ public void extract(AddressPortSink sink, REQUEST request) { } } - // try Host - for (String host : getter.getHttpRequestHeader(request, "host")) { + // try :authority (HTTP 2.0 pseudo-header) + for (String host : getter.getHttpRequestHeader(request, ":authority")) { if (extractHost(sink, host, 0, host.length())) { return; } } - // try :authority (HTTP 2.0 pseudo-header) - for (String host : getter.getHttpRequestHeader(request, ":authority")) { + // try Host + for (String host : getter.getHttpRequestHeader(request, "host")) { if (extractHost(sink, host, 0, host.length())) { return; } diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHostAddressAndPortExtractorTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHostAddressAndPortExtractorTest.java index 51b2d5df8bd9..3a8827beeb13 100644 --- a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHostAddressAndPortExtractorTest.java +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/ForwardedHostAddressAndPortExtractorTest.java @@ -96,11 +96,11 @@ void shouldParseForwardedHost( @ParameterizedTest @ArgumentsSource(HostArgs.class) - void shouldParseHost( + void shouldParsePseudoAuthority( List headers, @Nullable String expectedAddress, @Nullable Integer expectedPort) { doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, "forwarded"); doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, "x-forwarded-host"); - doReturn(headers).when(getter).getHttpRequestHeader(REQUEST, "host"); + doReturn(headers).when(getter).getHttpRequestHeader(REQUEST, ":authority"); AddressAndPort sink = new AddressAndPort(); underTest.extract(sink, REQUEST); @@ -111,12 +111,12 @@ void shouldParseHost( @ParameterizedTest @ArgumentsSource(HostArgs.class) - void shouldParsePseudoAuthority( + void shouldParseHost( List headers, @Nullable String expectedAddress, @Nullable Integer expectedPort) { doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, "forwarded"); doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, "x-forwarded-host"); - doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, "host"); - doReturn(headers).when(getter).getHttpRequestHeader(REQUEST, ":authority"); + doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, ":authority"); + doReturn(headers).when(getter).getHttpRequestHeader(REQUEST, "host"); AddressAndPort sink = new AddressAndPort(); underTest.extract(sink, REQUEST);