From aadabc1c7e74c4d90bc4457440969d2b15d69f91 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Tue, 5 Sep 2023 17:44:55 +0200 Subject: [PATCH] Add support for multiple headers in AlternateUrlSchemeProvider (#9389) --- .../http/AlternateUrlSchemeProvider.java | 19 ++++---- .../http/AlternateUrlSchemeProviderTest.java | 45 ++++++++++--------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/AlternateUrlSchemeProvider.java b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/AlternateUrlSchemeProvider.java index 27988775cf3f..4d33b8da0ee2 100644 --- a/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/AlternateUrlSchemeProvider.java +++ b/instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/AlternateUrlSchemeProvider.java @@ -5,8 +5,6 @@ package io.opentelemetry.instrumentation.api.instrumenter.http; -import static io.opentelemetry.instrumentation.api.instrumenter.http.HttpCommonAttributesExtractor.firstHeaderValue; - import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil; import java.util.Locale; import java.util.function.Function; @@ -34,18 +32,19 @@ public String apply(REQUEST request) { } // try Forwarded - String forwarded = firstHeaderValue(getter.getHttpRequestHeader(request, "forwarded")); - if (forwarded != null) { - forwarded = extractProtoFromForwardedHeader(forwarded); - if (forwarded != null) { - return forwarded; + for (String forwarded : getter.getHttpRequestHeader(request, "forwarded")) { + String proto = extractProtoFromForwardedHeader(forwarded); + if (proto != null) { + return proto; } } // try X-Forwarded-Proto - forwarded = firstHeaderValue(getter.getHttpRequestHeader(request, "x-forwarded-proto")); - if (forwarded != null) { - return extractProtoFromForwardedProtoHeader(forwarded); + for (String forwardedProto : getter.getHttpRequestHeader(request, "x-forwarded-proto")) { + String proto = extractProtoFromForwardedProtoHeader(forwardedProto); + if (proto != null) { + return proto; + } } return null; diff --git a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/AlternateUrlSchemeProviderTest.java b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/AlternateUrlSchemeProviderTest.java index ee5b0b51732c..c22c205b3ae8 100644 --- a/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/AlternateUrlSchemeProviderTest.java +++ b/instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/AlternateUrlSchemeProviderTest.java @@ -5,6 +5,7 @@ package io.opentelemetry.instrumentation.api.instrumenter.http; +import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; @@ -13,6 +14,7 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doReturn; +import java.util.List; import java.util.stream.Stream; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -42,8 +44,8 @@ void noHeaders() { @ParameterizedTest @ArgumentsSource(ForwardedHeaderValues.class) - void parseForwardedHeader(String headerValue, String expectedScheme) { - doReturn(singletonList(headerValue)).when(getter).getHttpRequestHeader(REQUEST, "forwarded"); + void parseForwardedHeader(List values, String expectedScheme) { + doReturn(values).when(getter).getHttpRequestHeader(REQUEST, "forwarded"); assertThat(underTest.apply(REQUEST)).isEqualTo(expectedScheme); } @@ -52,29 +54,28 @@ static final class ForwardedHeaderValues implements ArgumentsProvider { @Override public Stream provideArguments(ExtensionContext extensionContext) { return Stream.of( - arguments("for=1.1.1.1;proto=xyz", "xyz"), - arguments("for=1.1.1.1;proto=xyz;", "xyz"), - arguments("for=1.1.1.1;proto=xyz,", "xyz"), - arguments("for=1.1.1.1;proto=", null), - arguments("for=1.1.1.1;proto=;", null), - arguments("for=1.1.1.1;proto=,", null), - arguments("for=1.1.1.1;proto=\"xyz\"", "xyz"), - arguments("for=1.1.1.1;proto=\"xyz\";", "xyz"), - arguments("for=1.1.1.1;proto=\"xyz\",", "xyz"), - arguments("for=1.1.1.1;proto=\"", null), - arguments("for=1.1.1.1;proto=\"\"", null), - arguments("for=1.1.1.1;proto=\"\";", null), - arguments("for=1.1.1.1;proto=\"\",", null)); + arguments(singletonList("for=1.1.1.1;proto=xyz"), "xyz"), + arguments(singletonList("for=1.1.1.1;proto=xyz;"), "xyz"), + arguments(singletonList("for=1.1.1.1;proto=xyz,"), "xyz"), + arguments(singletonList("for=1.1.1.1;proto="), null), + arguments(singletonList("for=1.1.1.1;proto=;"), null), + arguments(singletonList("for=1.1.1.1;proto=,"), null), + arguments(singletonList("for=1.1.1.1;proto=\"xyz\""), "xyz"), + arguments(singletonList("for=1.1.1.1;proto=\"xyz\";"), "xyz"), + arguments(singletonList("for=1.1.1.1;proto=\"xyz\","), "xyz"), + arguments(singletonList("for=1.1.1.1;proto=\""), null), + arguments(singletonList("for=1.1.1.1;proto=\"\""), null), + arguments(singletonList("for=1.1.1.1;proto=\"\";"), null), + arguments(singletonList("for=1.1.1.1;proto=\"\","), null), + arguments(asList("for=1.1.1.1", "proto=xyz", "proto=abc"), "xyz")); } } @ParameterizedTest @ArgumentsSource(ForwardedProtoHeaderValues.class) - void parseForwardedProtoHeader(String headerValue, String expectedScheme) { + void parseForwardedProtoHeader(List values, String expectedScheme) { doReturn(emptyList()).when(getter).getHttpRequestHeader(REQUEST, "forwarded"); - doReturn(singletonList(headerValue)) - .when(getter) - .getHttpRequestHeader(REQUEST, "x-forwarded-proto"); + doReturn(values).when(getter).getHttpRequestHeader(REQUEST, "x-forwarded-proto"); assertThat(underTest.apply(REQUEST)).isEqualTo(expectedScheme); } @@ -82,7 +83,11 @@ static final class ForwardedProtoHeaderValues implements ArgumentsProvider { @Override public Stream provideArguments(ExtensionContext extensionContext) { - return Stream.of(arguments("xyz", "xyz"), arguments("\"xyz\"", "xyz"), arguments("\"", null)); + return Stream.of( + arguments(singletonList("xyz"), "xyz"), + arguments(singletonList("\"xyz\""), "xyz"), + arguments(singletonList("\""), null), + arguments(asList("xyz", "abc"), "xyz")); } } }