Skip to content

Commit

Permalink
Added Interceptor for IOExceptions generated by OKHttp and wrapped as…
Browse files Browse the repository at this point in the history
… runtime exceptions
  • Loading branch information
ricardo-mestre committed Dec 14, 2023
1 parent 6e53623 commit cdcfcd4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.exporter.sender.okhttp.internal;

import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

/**
* Unwrapper of IOExceptions generated by the OkHttp requests.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
*/
public class IOeExceptionInterceptor implements Interceptor {
@NotNull
@Override
public Response intercept(@NotNull Chain chain) throws IOException {
try {
return chain.proceed(chain.request());
} catch (Throwable e) {
Throwable exceptionCause = e.getCause();
if (exceptionCause instanceof IOException) {
throw (IOException) exceptionCause;
}
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public OkHttpGrpcSender(
new OkHttpClient.Builder()
.dispatcher(OkHttpUtil.newDispatcher())
.callTimeout(Duration.ofNanos(timeoutNanos));
clientBuilder.addInterceptor(new IOeExceptionInterceptor());
if (retryPolicy != null) {
clientBuilder.addInterceptor(
new RetryInterceptor(retryPolicy, OkHttpGrpcSender::isRetryable));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public OkHttpHttpSender(
});
}

builder.addInterceptor(new IOeExceptionInterceptor());
if (retryPolicy != null) {
builder.addInterceptor(new RetryInterceptor(retryPolicy, OkHttpHttpSender::isRetryable));
}
Expand Down

0 comments on commit cdcfcd4

Please sign in to comment.