Skip to content

Commit

Permalink
Fix IDE warnings and clean up OkHttp interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
bidetofevil committed Oct 27, 2023
1 parent 8065b03 commit 422872f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.embrace.android.embracesdk.okhttp3;

import static io.embrace.android.embracesdk.config.behavior.NetworkSpanForwardingBehavior.TRACEPARENT_HEADER_NAME;
import static io.embrace.android.embracesdk.internal.utils.ThrowableUtilsKt.causeMessage;
import static io.embrace.android.embracesdk.internal.utils.ThrowableUtilsKt.causeName;

Expand Down Expand Up @@ -34,6 +33,7 @@
*/
@InternalApi
public class EmbraceOkHttp3ApplicationInterceptor implements Interceptor {
static final String TRACEPARENT_HEADER_NAME = "traceparent";
static final String UNKNOWN_EXCEPTION = "Unknown";
static final String UNKNOWN_MESSAGE = "An error occurred during the execution of this network request";
final Embrace embrace;
Expand All @@ -54,7 +54,7 @@ public Response intercept(Chain chain) throws IOException {
// we are not interested in response, just proceed
return chain.proceed(request);
} catch (EmbraceCustomPathException e) {
if (embrace.isStarted()) {
if (embrace.isStarted() && !embrace.getInternalInterface().isInternalNetworkCaptureDisabled()) {
String urlString = EmbraceHttpPathOverride.getURLString(new EmbraceOkHttp3PathOverrideRequest(request), e.getCustomPath());

embrace.recordNetworkRequest(
Expand All @@ -74,7 +74,7 @@ public Response intercept(Chain chain) throws IOException {
throw e;
} catch (Exception e) {
// we are interested in errors.
if (embrace.isStarted()) {
if (embrace.isStarted() && !embrace.getInternalInterface().isInternalNetworkCaptureDisabled()) {
String urlString = EmbraceHttpPathOverride.getURLString(new EmbraceOkHttp3PathOverrideRequest(request));
String errorType = e.getClass().getCanonicalName();
String errorMessage = e.getMessage();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package io.embrace.android.embracesdk.okhttp3;

import static io.embrace.android.embracesdk.config.behavior.NetworkSpanForwardingBehavior.TRACEPARENT_HEADER_NAME;
import static io.embrace.android.embracesdk.logging.InternalStaticEmbraceLogger.logDebug;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.embrace.android.embracesdk.Embrace;
import io.embrace.android.embracesdk.InternalApi;
import io.embrace.android.embracesdk.internal.ApkToolsConfig;
import io.embrace.android.embracesdk.internal.clock.Clock;
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest;
import io.embrace.android.embracesdk.internal.network.http.EmbraceHttpPathOverride;
import io.embrace.android.embracesdk.network.http.HttpMethod;
import io.embrace.android.embracesdk.internal.network.http.NetworkCaptureData;
import io.embrace.android.embracesdk.network.EmbraceNetworkRequest;
import io.embrace.android.embracesdk.network.http.HttpMethod;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Request;
Expand Down Expand Up @@ -50,6 +46,7 @@ public final class EmbraceOkHttp3NetworkInterceptor implements Interceptor {
static final String CONTENT_ENCODING_HEADER_NAME = "Content-Encoding";
static final String CONTENT_TYPE_HEADER_NAME = "Content-Type";
static final String CONTENT_TYPE_EVENT_STREAM = "text/event-stream";
static final String TRACEPARENT_HEADER_NAME = "traceparent";
private static final String[] networkCallDataParts = new String[]{
"Response Headers",
"Request Headers",
Expand All @@ -76,7 +73,7 @@ public EmbraceOkHttp3NetworkInterceptor() {
public Response intercept(Chain chain) throws IOException {
final Request originalRequest = chain.request();

if (ApkToolsConfig.IS_NETWORK_CAPTURE_DISABLED || !embrace.isStarted()) {
if (!embrace.isStarted() || embrace.getInternalInterface().isInternalNetworkCaptureDisabled()) {
return chain.proceed(originalRequest);
}

Expand All @@ -96,9 +93,10 @@ public Response intercept(Chain chain) throws IOException {

Long contentLength = null;
// Try to get the content length from the header
if (networkResponse.header(CONTENT_LENGTH_HEADER_NAME) != null) {
String contentLengthHeaderValue = networkResponse.header(CONTENT_LENGTH_HEADER_NAME);
if (contentLengthHeaderValue != null) {
try {
contentLength = Long.parseLong(networkResponse.header(CONTENT_LENGTH_HEADER_NAME));
contentLength = Long.parseLong(contentLengthHeaderValue);
} catch (Exception ex) {
// Ignore
}
Expand All @@ -114,9 +112,12 @@ public Response intercept(Chain chain) throws IOException {

if (!serverSentEvent && contentLength == null) {
try {
BufferedSource source = networkResponse.body().source();
source.request(Long.MAX_VALUE);
contentLength = source.buffer().size();
ResponseBody body = networkResponse.body();
if (body != null) {
BufferedSource source = body.source();
source.request(Long.MAX_VALUE);
contentLength = source.getBuffer().size();
}
} catch (Exception ex) {
// Ignore
}
Expand Down Expand Up @@ -211,7 +212,9 @@ private NetworkCaptureData getNetworkCaptureData(Request request, Response respo
}

dataCaptureErrorMessage = "There were errors in capturing the following part(s) of the network call: %s" + errors;
logDebug("Failure during the building of NetworkCaptureData. " + dataCaptureErrorMessage, e);
embrace.logInternalError(
new RuntimeException("Failure during the building of NetworkCaptureData. " + dataCaptureErrorMessage, e)
);
}

return new NetworkCaptureData(
Expand Down Expand Up @@ -251,7 +254,7 @@ private byte[] getRequestBody(final Request request) {
return buffer.readByteArray();
}
} catch (final IOException e) {
logDebug("Failed to capture okhttp request body.", e);
embrace.logInternalError("Failed to capture okhttp request body.", e.getClass().toString());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ internal class EmbraceOkHttp3InterceptorsTest {
private var postNetworkInterceptorBeforeRequestSupplier: (Request) -> Request = { request -> request }
private var postNetworkInterceptorAfterResponseSupplier: (Response) -> Response = { response -> response }
private var isSDKStarted = true
private var isNetworkCaptureDisabled = false
private var isNetworkSpanForwardingEnabled = false

@Before
Expand All @@ -89,6 +90,7 @@ internal class EmbraceOkHttp3InterceptorsTest {
every { mockInternalInterface.shouldCaptureNetworkBody(any(), "POST") } answers { true }
every { mockInternalInterface.shouldCaptureNetworkBody(any(), "GET") } answers { false }
every { mockInternalInterface.isNetworkSpanForwardingEnabled() } answers { isNetworkSpanForwardingEnabled }
every { mockInternalInterface.isInternalNetworkCaptureDisabled() } answers { isNetworkCaptureDisabled }
every { mockInternalInterface.getSdkCurrentTime() } answers { FAKE_SDK_TIME }
applicationInterceptor = EmbraceOkHttp3ApplicationInterceptor(mockEmbrace)
preNetworkInterceptorTestInterceptor = TestInspectionInterceptor(
Expand Down Expand Up @@ -122,6 +124,7 @@ internal class EmbraceOkHttp3InterceptorsTest {
every { mockEmbrace.generateW3cTraceparent() } answers { GENERATED_TRACEPARENT }
every { mockEmbrace.internalInterface } answers { mockInternalInterface }
isSDKStarted = true
isNetworkCaptureDisabled = false
isNetworkSpanForwardingEnabled = false
}

Expand Down Expand Up @@ -180,6 +183,16 @@ internal class EmbraceOkHttp3InterceptorsTest {
verify(exactly = 0) { mockEmbrace.recordNetworkRequest(any()) }
}

@Test
fun `completed requests are not recorded if network capture has been disabled internally`() {
isNetworkCaptureDisabled = true
server.enqueue(createBaseMockResponse())
runGetRequest()
server.enqueue(createBaseMockResponse())
runPostRequest()
verify(exactly = 0) { mockEmbrace.recordNetworkRequest(any()) }
}

@Test
fun `incomplete requests are not recorded if the SDK has not started`() {
isSDKStarted = false
Expand Down
5 changes: 5 additions & 0 deletions embrace-android-sdk/api/embrace-android-sdk.api
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ public abstract interface annotation class io/embrace/android/embracesdk/annotat

public abstract interface class io/embrace/android/embracesdk/internal/EmbraceInternalInterface {
public abstract fun getSdkCurrentTime ()J
public abstract fun isInternalNetworkCaptureDisabled ()Z
public abstract fun isNetworkSpanForwardingEnabled ()Z
public abstract fun logComposeTap (Landroid/util/Pair;Ljava/lang/String;)V
public abstract fun logError (Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Z)V
Expand All @@ -187,6 +188,10 @@ public abstract interface class io/embrace/android/embracesdk/internal/EmbraceIn
public abstract fun shouldCaptureNetworkBody (Ljava/lang/String;Ljava/lang/String;)Z
}

public final class io/embrace/android/embracesdk/internal/EmbraceInternalInterface$DefaultImpls {
public static fun isInternalNetworkCaptureDisabled (Lio/embrace/android/embracesdk/internal/EmbraceInternalInterface;)Z
}

public abstract interface class io/embrace/android/embracesdk/internal/clock/Clock {
public abstract fun now ()J
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ public interface EmbraceInternalInterface {
* not change after the SDK has started.
*/
public fun getSdkCurrentTime(): Long

public fun isInternalNetworkCaptureDisabled(): Boolean = ApkToolsConfig.IS_NETWORK_CAPTURE_DISABLED
}

internal val defaultImpl = object : EmbraceInternalInterface {
Expand Down

0 comments on commit 422872f

Please sign in to comment.