Skip to content

Commit

Permalink
Handle more edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
bidetofevil committed Apr 19, 2024
1 parent 6ba1f17 commit 12e0eb2
Showing 1 changed file with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -528,12 +528,11 @@ public boolean usingProxy() {
* <p>
* 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));
Expand Down Expand Up @@ -628,7 +627,7 @@ private CountingInputStreamWithCallback countingInputStream(InputStream inputStr
hasNetworkCaptureRules(),
(responseBody) -> {
cacheNetworkCallData(responseBody);
internalLogNetworkCall(startTime);
internalLogNetworkCall();
return null;
});
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -796,16 +787,20 @@ private boolean hasNetworkCaptureRules() {
}

private void cacheNetworkCallData() {
if (isSDKStarted) {
cacheNetworkCallData(null);
}
cacheNetworkCallData(null);
}

/**
* Cache values from response at the first point of availability so that we won't try to retrieve these values when the response
* 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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 12e0eb2

Please sign in to comment.