-
Notifications
You must be signed in to change notification settings - Fork 16
Set Event.http.request.url on a best effort basis #99
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.collect.ImmutableList; | ||
| import java.net.MalformedURLException; | ||
| import java.net.URL; | ||
| import java.nio.ByteBuffer; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
@@ -301,13 +303,25 @@ public static Optional<String> getHttpMethod(Event event) { | |
| } | ||
|
|
||
| public static Optional<String> getFullHttpUrl(Event event) { | ||
| if (event.getHttp() != null && event.getHttp().getRequest() != null) { | ||
| return Optional.ofNullable(event.getHttp().getRequest().getUrl()); | ||
| if (event.getHttp() != null | ||
| && event.getHttp().getRequest() != null | ||
| && event.getHttp().getRequest().getUrl() != null | ||
| && isAbsoluteUrl(event.getHttp().getRequest().getUrl())) { | ||
| return Optional.of(event.getHttp().getRequest().getUrl()); | ||
| } | ||
|
|
||
| return Optional.empty(); | ||
| } | ||
|
|
||
| private static boolean isAbsoluteUrl(String urlStr) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Who is using the private function?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method getFullHttpUrl |
||
| try { | ||
| URL url = new URL(new URL("http://hypertrace.org"), urlStr); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any other url will be fine too. In case when input url is relative, this provided url makes up for the scheme, authority etc. |
||
| return url.toString().equals(urlStr); | ||
| } catch (MalformedURLException e) { | ||
| // ignore | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| public static Optional<Integer> getRequestSize(Event event) { | ||
| Protocol protocol = EnrichedSpanUtils.getProtocol(event); | ||
| if (protocol == null) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -446,7 +446,7 @@ private static void setUserAgent( | |
| .ifPresent(userAgent -> httpBuilder.getRequestBuilder().setUserAgent(userAgent)); | ||
| } | ||
|
|
||
| private static void setPath( | ||
| static void setPath( | ||
| Http.Builder httpBuilder, Map<String, JaegerSpanInternalModel.KeyValue> tagsMap) { | ||
| if (httpBuilder.getRequestBuilder().hasPath()) { | ||
| return; | ||
|
|
@@ -469,7 +469,7 @@ private static String removeTrailingSlash(String s) { | |
| return s.endsWith(SLASH) && s.length() > 1 ? s.substring(0, s.length() - 1) : s; | ||
| } | ||
|
|
||
| private static Optional<String> getPathFromUrlObject(String urlPath) { | ||
| static Optional<String> getPathFromUrlObject(String urlPath) { | ||
| try { | ||
| URL url = getNormalizedUrl(urlPath); | ||
| return Optional.of(url.getPath()); | ||
|
|
@@ -552,8 +552,6 @@ private void populateUrlParts(Request.Builder requestBuilder) { | |
| if (url.toString().equals(urlStr)) { // absolute URL | ||
| requestBuilder.setScheme(url.getProtocol()); | ||
| requestBuilder.setHost(url.getAuthority()); // Use authority so in case the port is specified it adds it to this | ||
| } else { // relative URL | ||
| requestBuilder.setUrl(null); // unset the URL as we only allow absolute/full URLs in the url field | ||
| } | ||
| setPathFromUrl(requestBuilder, url); | ||
| if (!requestBuilder.hasQueryString()) { | ||
|
|
@@ -569,13 +567,28 @@ private static URL getNormalizedUrl(String url) throws MalformedURLException { | |
| return new URL(new URL(RELATIVE_URL_CONTEXT), url); | ||
| } | ||
|
|
||
| /** | ||
| * If the requestBuilder already has a url, which is absolute do nothing | ||
| * if not, try building a url based on otel attributes | ||
| */ | ||
| private void maybeSetHttpUrlForOtelFormat( | ||
findingrish marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Request.Builder requestBuilder, | ||
| final Map<String, AttributeValue> attributeValueMap) { | ||
| if (requestBuilder.hasUrl()) { | ||
| if (requestBuilder.hasUrl() && isAbsoluteUrl(requestBuilder.getUrl())) { | ||
| return; | ||
| } | ||
| // if requestBuilder.getUrl() is not absolute try building the url from other attributes | ||
| Optional<String> url = HttpSemanticConventionUtils.getHttpUrlForOTelFormat(attributeValueMap); | ||
| url.ifPresent(requestBuilder::setUrl); | ||
| } | ||
|
|
||
| static boolean isAbsoluteUrl(String urlStr) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it's the same method defined in EnrichedSpanUtils above.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it is, since |
||
| try { | ||
| URL url = getNormalizedUrl(urlStr); | ||
| return url.toString().equals(urlStr); | ||
| } catch (MalformedURLException e) { | ||
| // ignore | ||
| } | ||
| return false; | ||
| } | ||
| } | ||
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.
when the URL isn't full URL, do you want to rather check if you can construct full URL back from scheme, authority, path and query params? That's what I had in mind.
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.
Oh.. if you're making sure this URL is always full URL, we don't even need this method I think.
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.
We would need it.
Earlier, event.getHttp().getRequest().getUrl() was null in case of relative url. Now since it is being populated in a best effort basis, so it maybe full url or not.
However the consumer of this method EnrichedSpanUtils#getFullHttpUrl would except a full url to be returned or null.
To ensure that behaviour a check has been added to this method.
However I think we can go with this pr first #104 for now. It is a subset of the changes made in this pr.