Add http/2 tests for okhttp3 (#11523)

This commit is contained in:
Lauri Tulmin 2024-06-12 14:04:25 +03:00 committed by GitHub
parent dc8822cb01
commit 99aa42a680
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 150 additions and 11 deletions

View File

@ -20,3 +20,27 @@ dependencies {
testImplementation(project(":instrumentation:okhttp:okhttp-3.0:testing"))
}
val testLatestDeps = findProperty("testLatestDeps") as Boolean
testing {
suites {
val http2Test by registering(JvmTestSuite::class) {
dependencies {
if (testLatestDeps) {
implementation("com.squareup.okhttp3:okhttp:+")
compileOnly("com.google.android:annotations:4.1.1.4")
} else {
implementation("com.squareup.okhttp3:okhttp:3.11.0")
}
implementation(project(":instrumentation:okhttp:okhttp-3.0:testing"))
}
}
}
}
tasks {
check {
dependsOn(testing.suites)
}
}

View File

@ -0,0 +1,38 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.okhttp.v3_0;
import static java.util.Collections.singletonList;
import io.opentelemetry.instrumentation.okhttp.v3_0.AbstractOkHttp3Test;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import org.junit.jupiter.api.extension.RegisterExtension;
class OkHttp3Http2Test extends AbstractOkHttp3Test {
@RegisterExtension
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();
@Override
public Call.Factory createCallFactory(OkHttpClient.Builder clientBuilder) {
clientBuilder.protocols(singletonList(Protocol.H2_PRIOR_KNOWLEDGE));
return clientBuilder.build();
}
@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
super.configure(optionsBuilder);
// https does not work with H2_PRIOR_KNOWLEDGE
optionsBuilder.disableTestHttps();
optionsBuilder.setHttpProtocolVersion(uri -> "2");
}
}

View File

@ -5,6 +5,7 @@
package io.opentelemetry.javaagent.instrumentation.okhttp.v3_0;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.instrumentation.okhttp.v3_0.AbstractOkHttp3Test;
@ -13,16 +14,18 @@ import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumenta
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
public class OkHttp3Test extends AbstractOkHttp3Test {
class OkHttp3Test extends AbstractOkHttp3Test {
@RegisterExtension
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forAgent();
@Override
public Call.Factory createCallFactory(OkHttpClient.Builder clientBuilder) {
clientBuilder.protocols(singletonList(Protocol.HTTP_1_1));
return clientBuilder.build();
}

View File

@ -9,3 +9,28 @@ dependencies {
testImplementation(project(":instrumentation:okhttp:okhttp-3.0:testing"))
}
val testLatestDeps = findProperty("testLatestDeps") as Boolean
testing {
suites {
val http2Test by registering(JvmTestSuite::class) {
dependencies {
implementation(project())
if (testLatestDeps) {
implementation("com.squareup.okhttp3:okhttp:+")
compileOnly("com.google.android:annotations:4.1.1.4")
} else {
implementation("com.squareup.okhttp3:okhttp:3.11.0")
}
implementation(project(":instrumentation:okhttp:okhttp-3.0:testing"))
}
}
}
}
tasks {
check {
dependsOn(testing.suites)
}
}

View File

@ -0,0 +1,42 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.instrumentation.okhttp.v3_0;
import static java.util.Collections.singletonList;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import org.junit.jupiter.api.extension.RegisterExtension;
class OkHttp3Http2Test extends AbstractOkHttp3Test {
@RegisterExtension
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forLibrary();
@Override
public Call.Factory createCallFactory(OkHttpClient.Builder clientBuilder) {
clientBuilder.protocols(singletonList(Protocol.H2_PRIOR_KNOWLEDGE));
return OkHttpTelemetry.builder(testing.getOpenTelemetry())
.setCapturedRequestHeaders(singletonList(AbstractHttpClientTest.TEST_REQUEST_HEADER))
.setCapturedResponseHeaders(singletonList(AbstractHttpClientTest.TEST_RESPONSE_HEADER))
.build()
.newCallFactory(clientBuilder.build());
}
@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
super.configure(optionsBuilder);
// https does not work with H2_PRIOR_KNOWLEDGE
optionsBuilder.disableTestHttps();
optionsBuilder.setHttpProtocolVersion(uri -> "2");
}
}

View File

@ -58,6 +58,10 @@ public enum OkHttpAttributesGetter implements HttpClientAttributesGetter<Request
case SPDY_3:
return "spdy";
}
// added in 3.11.0
if ("H2_PRIOR_KNOWLEDGE".equals(response.protocol().name())) {
return "http";
}
return null;
}
@ -77,6 +81,10 @@ public enum OkHttpAttributesGetter implements HttpClientAttributesGetter<Request
case SPDY_3:
return "3.1";
}
// added in 3.11.0
if ("H2_PRIOR_KNOWLEDGE".equals(response.protocol().name())) {
return "2";
}
return null;
}

View File

@ -5,26 +5,27 @@
package io.opentelemetry.instrumentation.okhttp.v3_0;
import static java.util.Collections.singletonList;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
import java.util.Collections;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import org.junit.jupiter.api.extension.RegisterExtension;
public class OkHttp3Test extends AbstractOkHttp3Test {
class OkHttp3Test extends AbstractOkHttp3Test {
@RegisterExtension
static final InstrumentationExtension testing = HttpClientInstrumentationExtension.forLibrary();
@Override
public Call.Factory createCallFactory(OkHttpClient.Builder clientBuilder) {
clientBuilder.protocols(singletonList(Protocol.HTTP_1_1));
return OkHttpTelemetry.builder(testing.getOpenTelemetry())
.setCapturedRequestHeaders(
Collections.singletonList(AbstractHttpClientTest.TEST_REQUEST_HEADER))
.setCapturedResponseHeaders(
Collections.singletonList(AbstractHttpClientTest.TEST_RESPONSE_HEADER))
.setCapturedRequestHeaders(singletonList(AbstractHttpClientTest.TEST_REQUEST_HEADER))
.setCapturedResponseHeaders(singletonList(AbstractHttpClientTest.TEST_RESPONSE_HEADER))
.build()
.newCallFactory(clientBuilder.build());
}

View File

@ -5,7 +5,6 @@
package io.opentelemetry.instrumentation.okhttp.v3_0;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.api.common.AttributeKey;
@ -27,7 +26,6 @@ import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
@ -45,8 +43,7 @@ public abstract class AbstractOkHttp3Test extends AbstractHttpClientTest<Request
protected OkHttpClient.Builder getClientBuilder(boolean withReadTimeout) {
OkHttpClient.Builder builder =
new OkHttpClient.Builder()
.connectTimeout(CONNECTION_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS)
.protocols(singletonList(Protocol.HTTP_1_1));
.connectTimeout(CONNECTION_TIMEOUT.toMillis(), TimeUnit.MILLISECONDS);
if (withReadTimeout) {
builder
// don't want retries on time outs
@ -125,6 +122,7 @@ public abstract class AbstractOkHttp3Test extends AbstractHttpClientTest<Request
// response)
if ("http://localhost:61/".equals(uri.toString())
|| "https://192.0.2.1/".equals(uri.toString())
|| "http://192.0.2.1/".equals(uri.toString())
|| resolveAddress("/read-timeout").toString().equals(uri.toString())) {
attributes.remove(NetworkAttributes.NETWORK_PROTOCOL_VERSION);
}