Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding UncheckedIOException to onFailure handler #16

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ public CompletableResultCode export(T exportRequest, int numItems) {

exporterMetrics.addFailed(numItems);

byte[] body;
byte[] body = null;
try {
body = httpResponse.responseBody();
} catch (IOException ex) {
throw new IllegalStateException(ex);
logger.log(Level.FINE, "Unable to obtain response body", ex);
}

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,8 +14,14 @@
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;
Expand All @@ -24,10 +30,12 @@
import java.util.Collections;
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 +45,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 +76,20 @@ void setup() throws IOException, InterruptedException {
.build());
}

@Test
void exportAsJson() {
HttpExporter<Marshaler> exporter =
new HttpExporterBuilder<>("jdk", "test", server.httpUri().toASCIIString())
.exportAsJson()
.build();

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what this test has to do with the code we've been talking about.

Copy link
Author

@ricardo-mestre ricardo-mestre Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intend to cover Lines 220 and 221 of JdkHttpSender. I can't override the method writeJsonTo on the NoOpMarshaler so I'm using a succes flow instead adding .exportAsJson() in the builder.

Copy link
Author

@ricardo-mestre ricardo-mestre Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just removed the authentication and shutdown part to focus more on the regular success flow

}

@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