Skip to content

Commit

Permalink
Bump OpenTelemetry version to 1.19 (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Rzeszutek authored Oct 13, 2022
1 parent 44469dd commit f8ed61c
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 54 deletions.
2 changes: 1 addition & 1 deletion sample-app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ android {
}
}

val otelVersion = "1.18.0"
val otelVersion = "1.19.0"
val otelAlphaVersion = "$otelVersion-alpha"

dependencies {
Expand Down
2 changes: 1 addition & 1 deletion splunk-otel-android-volley/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ android {
}
}

val otelVersion = "1.18.0"
val otelVersion = "1.19.0"
val otelAlphaVersion = "$otelVersion-alpha"

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -29,22 +27,12 @@
public final class RequestWrapper {
private final Request<?> request;
private final Map<String, String> additionalHeaders;
@Nullable private URL url;

RequestWrapper(Request<?> request, Map<String, String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RequestWrapper> currentRequestWrapper = new ThreadLocal<>();
private final Instrumenter<RequestWrapper, HttpResponse> instrumenter;

TracingHurlStack(Instrumenter<RequestWrapper, HttpResponse> instrumenter) {
Expand Down Expand Up @@ -60,41 +57,22 @@ public HttpResponse executeRequest(Request<?> request, Map<String, String> 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);
}
}
123 changes: 123 additions & 0 deletions splunk-otel-android-volley/src/main/java/com/splunk/rum/UrlParser.java
Original file line number Diff line number Diff line change
@@ -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() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
2 changes: 1 addition & 1 deletion splunk-otel-android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ android {
}
}

val otelVersion = "1.18.0"
val otelVersion = "1.19.0"
val otelAlphaVersion = "$otelVersion-alpha"

dependencies {
Expand Down

0 comments on commit f8ed61c

Please sign in to comment.