From f8ed61c98898c65272b53aa5df0141a87b6d172e Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Thu, 13 Oct 2022 13:46:25 +0200 Subject: [PATCH] Bump OpenTelemetry version to 1.19 (#383) --- sample-app/build.gradle.kts | 2 +- splunk-otel-android-volley/build.gradle.kts | 2 +- .../java/com/splunk/rum/RequestWrapper.java | 12 -- .../java/com/splunk/rum/TracingHurlStack.java | 48 ++----- .../main/java/com/splunk/rum/UrlParser.java | 123 ++++++++++++++++++ .../rum/VolleyNetClientAttributesGetter.java | 8 +- splunk-otel-android/build.gradle.kts | 2 +- 7 files changed, 143 insertions(+), 54 deletions(-) create mode 100644 splunk-otel-android-volley/src/main/java/com/splunk/rum/UrlParser.java diff --git a/sample-app/build.gradle.kts b/sample-app/build.gradle.kts index b1a9463bd..81e6353e5 100644 --- a/sample-app/build.gradle.kts +++ b/sample-app/build.gradle.kts @@ -49,7 +49,7 @@ android { } } -val otelVersion = "1.18.0" +val otelVersion = "1.19.0" val otelAlphaVersion = "$otelVersion-alpha" dependencies { diff --git a/splunk-otel-android-volley/build.gradle.kts b/splunk-otel-android-volley/build.gradle.kts index b5493d16f..8e2f71fc3 100644 --- a/splunk-otel-android-volley/build.gradle.kts +++ b/splunk-otel-android-volley/build.gradle.kts @@ -42,7 +42,7 @@ android { } } -val otelVersion = "1.18.0" +val otelVersion = "1.19.0" val otelAlphaVersion = "$otelVersion-alpha" dependencies { diff --git a/splunk-otel-android-volley/src/main/java/com/splunk/rum/RequestWrapper.java b/splunk-otel-android-volley/src/main/java/com/splunk/rum/RequestWrapper.java index 6640bbc1a..c4dabb2ea 100644 --- a/splunk-otel-android-volley/src/main/java/com/splunk/rum/RequestWrapper.java +++ b/splunk-otel-android-volley/src/main/java/com/splunk/rum/RequestWrapper.java @@ -16,9 +16,7 @@ package com.splunk.rum; -import androidx.annotation.Nullable; import com.android.volley.Request; -import java.net.URL; import java.util.HashMap; import java.util.Map; @@ -29,22 +27,12 @@ public final class RequestWrapper { private final Request request; private final Map additionalHeaders; - @Nullable private URL url; RequestWrapper(Request request, Map additionalHeaders) { this.request = request; this.additionalHeaders = new HashMap<>(additionalHeaders); } - void setUrl(URL url) { - this.url = url; - } - - @Nullable - URL getUrl() { - return url; - } - /** Returns the HTTP request that will be executed. */ public Request getRequest() { return request; diff --git a/splunk-otel-android-volley/src/main/java/com/splunk/rum/TracingHurlStack.java b/splunk-otel-android-volley/src/main/java/com/splunk/rum/TracingHurlStack.java index 35129cf0f..be7c6e66d 100644 --- a/splunk-otel-android-volley/src/main/java/com/splunk/rum/TracingHurlStack.java +++ b/splunk-otel-android-volley/src/main/java/com/splunk/rum/TracingHurlStack.java @@ -24,14 +24,11 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; import java.io.IOException; -import java.net.HttpURLConnection; -import java.net.URL; import java.util.Map; import javax.net.ssl.SSLSocketFactory; final class TracingHurlStack extends HurlStack { - private static final ThreadLocal currentRequestWrapper = new ThreadLocal<>(); private final Instrumenter instrumenter; TracingHurlStack(Instrumenter instrumenter) { @@ -60,41 +57,22 @@ public HttpResponse executeRequest(Request request, Map addit Context parentContext = Context.current(); RequestWrapper requestWrapper = new RequestWrapper(request, additionalHeaders); - currentRequestWrapper.set(requestWrapper); - try { - if (!instrumenter.shouldStart(parentContext, requestWrapper)) { - return super.executeRequest(request, additionalHeaders); - } - - Context context = instrumenter.start(parentContext, requestWrapper); - HttpResponse response = null; - Throwable throwable = null; - try (Scope ignored = context.makeCurrent()) { - response = super.executeRequest(request, requestWrapper.getAdditionalHeaders()); - return response; - } catch (Throwable t) { - throwable = t; - throw t; - } finally { - instrumenter.end(context, requestWrapper, response, throwable); - } - - } finally { - currentRequestWrapper.remove(); + if (!instrumenter.shouldStart(parentContext, requestWrapper)) { + return super.executeRequest(request, additionalHeaders); } - } - @Override - protected HttpURLConnection createConnection(URL url) throws IOException { - // requestWrapper cannot be null here, because this method is called only - // inside parent's executeRequest() (through a private method - openConnection()), - // so currentRequestWrapper.set() is always called before that - // null-check here is just to satisfy errorprone - RequestWrapper requestWrapper = currentRequestWrapper.get(); - if (requestWrapper != null) { - requestWrapper.setUrl(url); + Context context = instrumenter.start(parentContext, requestWrapper); + HttpResponse response = null; + Throwable throwable = null; + try (Scope ignored = context.makeCurrent()) { + response = super.executeRequest(request, requestWrapper.getAdditionalHeaders()); + return response; + } catch (Throwable t) { + throwable = t; + throw t; + } finally { + instrumenter.end(context, requestWrapper, response, throwable); } - return super.createConnection(url); } } diff --git a/splunk-otel-android-volley/src/main/java/com/splunk/rum/UrlParser.java b/splunk-otel-android-volley/src/main/java/com/splunk/rum/UrlParser.java new file mode 100644 index 000000000..9eed4b69f --- /dev/null +++ b/splunk-otel-android-volley/src/main/java/com/splunk/rum/UrlParser.java @@ -0,0 +1,123 @@ +/* + * Copyright Splunk Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.splunk.rum; + +// Includes work from: +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +import androidx.annotation.Nullable; + +/** + * Copy of + * https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/710429029450855c051ce45c8a8857c95f1cb255/instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/UrlParser.java + */ +class UrlParser { + + @Nullable + static String getHost(String url) { + + int schemeEndIndex = url.indexOf(':'); + if (schemeEndIndex == -1) { + // not a valid url + return null; + } + + int len = url.length(); + if (len <= schemeEndIndex + 2 + || url.charAt(schemeEndIndex + 1) != '/' + || url.charAt(schemeEndIndex + 2) != '/') { + // has no authority component + return null; + } + + // look for the end of the host: + // ':' ==> start of port, or + // '/', '?', '#' ==> start of path + int index; + for (index = schemeEndIndex + 3; index < len; index++) { + char c = url.charAt(index); + if (c == ':' || c == '/' || c == '?' || c == '#') { + break; + } + } + String host = url.substring(schemeEndIndex + 3, index); + return host.isEmpty() ? null : host; + } + + @Nullable + static Integer getPort(String url) { + + int schemeEndIndex = url.indexOf(':'); + if (schemeEndIndex == -1) { + // not a valid url + return null; + } + + int len = url.length(); + if (len <= schemeEndIndex + 2 + || url.charAt(schemeEndIndex + 1) != '/' + || url.charAt(schemeEndIndex + 2) != '/') { + // has no authority component + return null; + } + + // look for the end of the host: + // ':' ==> start of port, or + // '/', '?', '#' ==> start of path + int index; + int portIndex = -1; + for (index = schemeEndIndex + 3; index < len; index++) { + char c = url.charAt(index); + if (c == ':') { + portIndex = index + 1; + break; + } + if (c == '/' || c == '?' || c == '#') { + break; + } + } + + if (portIndex == -1) { + return null; + } + + // look for the end of the port: + // '/', '?', '#' ==> start of path + for (index = portIndex; index < len; index++) { + char c = url.charAt(index); + if (c == '/' || c == '?' || c == '#') { + break; + } + } + String port = url.substring(portIndex, index); + return port.isEmpty() ? null : safeParse(port); + } + + @Nullable + private static Integer safeParse(String port) { + try { + return Integer.valueOf(port); + } catch (NumberFormatException e) { + return null; + } + } + + private UrlParser() {} +} diff --git a/splunk-otel-android-volley/src/main/java/com/splunk/rum/VolleyNetClientAttributesGetter.java b/splunk-otel-android-volley/src/main/java/com/splunk/rum/VolleyNetClientAttributesGetter.java index 1af13d77e..e0f43e93c 100644 --- a/splunk-otel-android-volley/src/main/java/com/splunk/rum/VolleyNetClientAttributesGetter.java +++ b/splunk-otel-android-volley/src/main/java/com/splunk/rum/VolleyNetClientAttributesGetter.java @@ -32,13 +32,13 @@ public String transport(RequestWrapper requestWrapper, @Nullable HttpResponse ht @Nullable @Override - public String peerName(RequestWrapper requestWrapper, @Nullable HttpResponse httpResponse) { - return requestWrapper.getUrl() != null ? requestWrapper.getUrl().getHost() : null; + public String peerName(RequestWrapper requestWrapper) { + return UrlParser.getHost(requestWrapper.getRequest().getUrl()); } @Nullable @Override - public Integer peerPort(RequestWrapper requestWrapper, @Nullable HttpResponse httpResponse) { - return requestWrapper.getUrl() != null ? requestWrapper.getUrl().getPort() : null; + public Integer peerPort(RequestWrapper requestWrapper) { + return UrlParser.getPort(requestWrapper.getRequest().getUrl()); } } diff --git a/splunk-otel-android/build.gradle.kts b/splunk-otel-android/build.gradle.kts index bf33bd4bd..92eb0fd68 100644 --- a/splunk-otel-android/build.gradle.kts +++ b/splunk-otel-android/build.gradle.kts @@ -40,7 +40,7 @@ android { } } -val otelVersion = "1.18.0" +val otelVersion = "1.19.0" val otelAlphaVersion = "$otelVersion-alpha" dependencies {