Skip to content

Commit

Permalink
Adding UncheckedIOException to onFailure handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardo-mestre committed Dec 20, 2023
1 parent c46830a commit 0accdf3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ public CompletableResultCode export(T exportRequest, int numItems) {
try {
body = httpResponse.responseBody();
} catch (IOException ex) {
throw new IllegalStateException(ex);
logger.log(
Level.WARNING,
"Failed to export "
+ type
+ "s. Server responded with exception message "
+ ex.getMessage());
result.fail();
return;
}

String status = extractErrorStatus(httpResponse.statusMessage(), body);
Expand Down
1 change: 1 addition & 0 deletions exporters/sender/jdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies {
implementation(project(":sdk:common"))

compileOnly("com.fasterxml.jackson.core:jackson-core")
testImplementation("com.linecorp.armeria:armeria-junit5")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,30 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.linecorp.armeria.common.HttpResponse;
import com.linecorp.armeria.common.HttpStatus;
import com.linecorp.armeria.testing.junit5.server.mock.MockWebServerExtension;
import io.opentelemetry.exporter.internal.http.HttpExporter;
import io.opentelemetry.exporter.internal.http.HttpExporterBuilder;
import io.opentelemetry.exporter.internal.marshal.Marshaler;
import io.opentelemetry.exporter.internal.marshal.Serializer;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.common.export.RetryPolicy;
import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpConnectTimeoutException;
import java.time.Duration;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLException;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
Expand All @@ -37,6 +47,8 @@
@MockitoSettings(strictness = Strictness.LENIENT)
class JdkHttpSenderTest {

@RegisterExtension static final MockWebServerExtension server = new MockWebServerExtension();

private final HttpClient realHttpClient =
HttpClient.newBuilder().connectTimeout(Duration.ofMillis(10)).build();
@Mock private HttpClient mockHttpClient;
Expand Down Expand Up @@ -66,6 +78,28 @@ void setup() throws IOException, InterruptedException {
.build());
}

@SuppressWarnings("SystemOut")
@Test
void exportAsJson() {
HttpExporter<Marshaler> exporter =
new HttpExporterBuilder<>("jdk", "test", server.httpUri().toASCIIString())
.setAuthenticator(
() -> {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "auth");
return headers;
})
.exportAsJson()
.build();

server.enqueue(HttpResponse.of(HttpStatus.OK));

CompletableResultCode result = exporter.export(new NoOpMarshaler(), 0);
result.join(1, TimeUnit.MINUTES);
exporter.shutdown().join(1, TimeUnit.MINUTES);
Assertions.assertThat(result.isSuccess()).isTrue();
}

@Test
void sendInternal_RetryableConnectTimeoutException() throws IOException, InterruptedException {
assertThatThrownBy(() -> sender.sendInternal(new NoOpMarshaler()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void export_giveup() {
server.enqueue(HttpResponse.of(HttpStatus.UNAUTHORIZED));
return Collections.emptyMap();
})
.exportAsJson()
.build();
server.enqueue(HttpResponse.of(HttpStatus.UNAUTHORIZED));
assertThat(exporter.export(marshaler, 0).join(1, TimeUnit.MINUTES).isSuccess()).isFalse();
Expand Down

0 comments on commit 0accdf3

Please sign in to comment.