-
Notifications
You must be signed in to change notification settings - Fork 893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize tomcat uri construction #4008
Merged
trask
merged 3 commits into
open-telemetry:main
from
trask:optimize-tomcat-uri-construction
Aug 30, 2021
+118
−14
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
...mentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/UriBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.internal; | ||
|
||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
|
||
// internal until decisions made on | ||
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/3700 | ||
public class UriBuilder { | ||
|
||
// TODO (trask) investigate and document implications of URI encoding, see | ||
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/4008#discussion_r698027851 | ||
// | ||
// note: currently path must be empty or start with "/" but that can be relaxed if needed | ||
public static String uri( | ||
String scheme, String host, int serverPort, String path, @Nullable String query) { | ||
|
||
boolean isDefaultPort = | ||
(scheme.equals("http") && serverPort == 80) | ||
|| (scheme.equals("https") && serverPort == 443); | ||
|
||
// +3 is space for "://" | ||
int length = scheme.length() + 3 + host.length() + path.length(); | ||
if (!isDefaultPort && serverPort != -1) { | ||
// +6 is space for ":" and max port number (65535) | ||
length += 6; | ||
} | ||
if (query != null) { | ||
// the +1 is space for "?" | ||
length += 1 + query.length(); | ||
} | ||
|
||
StringBuilder url = new StringBuilder(length); | ||
url.append(scheme); | ||
url.append("://"); | ||
url.append(host); | ||
if (!isDefaultPort && serverPort != -1) { | ||
url.append(':'); | ||
url.append(serverPort); | ||
} | ||
url.append(path); | ||
if (query != null) { | ||
url.append('?'); | ||
url.append(query); | ||
} | ||
return url.toString(); | ||
} | ||
|
||
private UriBuilder() {} | ||
} |
63 changes: 63 additions & 0 deletions
63
...ation-api/src/test/java/io/opentelemetry/instrumentation/api/internal/UriBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.internal; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.ArgumentsProvider; | ||
import org.junit.jupiter.params.provider.ArgumentsSource; | ||
|
||
public class UriBuilderTest { | ||
|
||
@ParameterizedTest | ||
@ArgumentsSource(Parameters.class) | ||
public void test(String scheme, String host, int port, String path, String query) | ||
throws URISyntaxException { | ||
|
||
assertThat(UriBuilder.uri(scheme, host, port, path, query)) | ||
.isEqualTo(new URI(scheme, null, host, port, path, query, null).toString()); | ||
} | ||
|
||
// can't use parameterized test above because URI.toString() encodes the port when it is supplied, | ||
// even it's the default port | ||
@Test | ||
public void testHttpDefaultPort() { | ||
assertThat(UriBuilder.uri("http", "myhost", 80, "/mypath", "myquery")) | ||
.isEqualTo("http://myhost/mypath?myquery"); | ||
} | ||
|
||
// can't use parameterized test above because URI.toString() encodes the port when it is supplied, | ||
// even it's the default port | ||
@Test | ||
public void testHttpsDefaultPort() { | ||
assertThat(UriBuilder.uri("https", "myhost", 443, "/mypath", "myquery")) | ||
.isEqualTo("https://myhost/mypath?myquery"); | ||
} | ||
|
||
private static class Parameters implements ArgumentsProvider { | ||
@Override | ||
public Stream<? extends Arguments> provideArguments(ExtensionContext context) { | ||
return Stream.of( | ||
Arguments.of("http", "myhost", -1, "/mypath", "myquery"), // test default http port | ||
Arguments.of("http", "myhost", 8080, "/mypath", "myquery"), // test non-default http port | ||
Arguments.of("https", "myhost", -1, "/mypath", "myquery"), // test default https port | ||
Arguments.of( | ||
"https", "myhost", 8443, "/mypath", "myquery"), // test non-default https port | ||
Arguments.of("http", "myhost", -1, "/", "myquery"), // test root path | ||
Arguments.of("http", "myhost", -1, "", "myquery"), // test empty path | ||
Arguments.of("http", "myhost", -1, "/mypath", ""), // test empty query string | ||
Arguments.of("http", "myhost", -1, "/mypath", null) // test null query string | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially thought that as this does not encode any characters it could result in an invalid url (would that actually matter?), but javadoc for
HttpServletRequest.getQueryString
states that "The value is not decoded by the container" and the same forgetRequestURI
so as far as I understand this looks good.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point, I added a TODO to this method to investigate and document encoding requirement