diff --git a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/network/http/EmbraceUrlConnectionDelegate.java b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/network/http/EmbraceUrlConnectionDelegate.java index f10de2a03..170a5501b 100644 --- a/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/network/http/EmbraceUrlConnectionDelegate.java +++ b/embrace-android-sdk/src/main/java/io/embrace/android/embracesdk/internal/network/http/EmbraceUrlConnectionDelegate.java @@ -174,7 +174,8 @@ public void connect() throws IOException { @Override public void disconnect() { // The network call must be logged before we close the transport - internalLogNetworkCall(createdTime); + setStartTime(createdTime); + internalLogNetworkCall(); this.connection.disconnect(); } @@ -467,9 +468,8 @@ public int getResponseCode() { @Nullable public String getResponseMessage() throws IOException { identifyTraceId(); - String responseMsg = this.connection.getResponseMessage(); cacheNetworkCallData(); - return responseMsg; + return this.connection.getResponseMessage(); } @Override @@ -528,12 +528,11 @@ public boolean usingProxy() { *

* If this delegate has already logged the call it represents, this method is a no-op. */ - synchronized void internalLogNetworkCall(long startTime) { + synchronized void internalLogNetworkCall() { if (isSDKStarted && !this.didLogNetworkCall) { // We are proactive with setting this flag so that we don't get nested calls to log the network call by virtue of // extracting the data we need to log the network call. this.didLogNetworkCall = true; // TODO: Wouldn't this mean that the network call might not be logged - this.startTime = startTime; long endTime = embrace.getInternalInterface().getSdkCurrentTime(); String url = EmbraceHttpPathOverride.getURLString(new EmbraceHttpUrlConnectionOverride(this.connection)); @@ -628,7 +627,7 @@ private CountingInputStreamWithCallback countingInputStream(InputStream inputStr hasNetworkCaptureRules(), (responseBody) -> { cacheNetworkCallData(responseBody); - internalLogNetworkCall(startTime); + internalLogNetworkCall(); return null; }); } @@ -756,8 +755,7 @@ public Principal getPeerPrincipal() throws SSLPeerUnverifiedException { @Nullable private InputStream getWrappedInputStream(InputStream connectionInputStream) { identifyTraceId(); - startTime = embrace.getInternalInterface().getSdkCurrentTime(); - + setStartTime(embrace.getInternalInterface().getSdkCurrentTime()); InputStream in = null; if (shouldUncompressGzip()) { try { @@ -773,18 +771,11 @@ private InputStream getWrappedInputStream(InputStream connectionInputStream) { countingInputStream(new BufferedInputStream(connectionInputStream)) : connectionInputStream; } - cacheAndLogNetworkCall(startTime); + cacheNetworkCallData(); return in; } - private void cacheAndLogNetworkCall(long startTime) { - if (!enableWrapIoStreams) { - cacheNetworkCallData(); - internalLogNetworkCall(startTime); - } - } - private boolean hasNetworkCaptureRules() { if (!isSDKStarted || this.connection.getURL() == null) { return false; @@ -796,9 +787,7 @@ private boolean hasNetworkCaptureRules() { } private void cacheNetworkCallData() { - if (isSDKStarted) { - cacheNetworkCallData(null); - } + cacheNetworkCallData(null); } /** @@ -806,6 +795,12 @@ private void cacheNetworkCallData() { * is not available. */ private void cacheNetworkCallData(@Nullable byte[] responseBody) { + if (!isSDKStarted) { + return; + } + + setStartTime(embrace.getInternalInterface().getSdkCurrentTime()); + if (headerFields.get() == null) { synchronized (headerFields) { if (headerFields.get() == null) { @@ -852,6 +847,10 @@ private void cacheNetworkCallData(@Nullable byte[] responseBody) { } } + if (!enableWrapIoStreams) { + internalLogNetworkCall(); + } + if (shouldCaptureNetworkData()) { // If we don't have network capture rules, it's unnecessary to save these values synchronized (networkCaptureData) { @@ -901,6 +900,12 @@ private boolean shouldCaptureNetworkData() { (networkCaptureData.get() == null || networkCaptureData.get().getCapturedResponseBody() == null); } + private void setStartTime(@NonNull Long startTimeMs) { + if (startTime == null) { + startTime = startTimeMs; + } + } + private void logError(@NonNull Throwable t) { Embrace.getInstance().getInternalInterface().logInternalError(t); }