Skip to content

Commit

Permalink
Add support for multiple headers in AlternateUrlSchemeProvider (#9389)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek authored Sep 5, 2023
1 parent 9e12723 commit aadabc1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String> values, String expectedScheme) {
doReturn(values).when(getter).getHttpRequestHeader(REQUEST, "forwarded");
assertThat(underTest.apply(REQUEST)).isEqualTo(expectedScheme);
}

Expand All @@ -52,37 +54,40 @@ static final class ForwardedHeaderValues implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> 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<String> 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);
}

static final class ForwardedProtoHeaderValues implements ArgumentsProvider {

@Override
public Stream<? extends Arguments> 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"));
}
}
}

0 comments on commit aadabc1

Please sign in to comment.