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

Fix latest dep tests #11925

Merged
merged 3 commits into from
Jul 31, 2024
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 @@ -18,14 +18,32 @@ dependencies {
testInstrumentation(project(":instrumentation:netty:netty-4.1:javaagent"))
}

otelJava {
// AHC uses Unsafe and so does not run on later java version
maxJavaVersionForTests.set(JavaVersion.VERSION_1_8)
val latestDepTest = findProperty("testLatestDeps") as Boolean
val testJavaVersion =
gradle.startParameter.projectProperties["testJavaVersion"]?.let(JavaVersion::toVersion)
?: JavaVersion.current()

if (!latestDepTest) {
otelJava {
// AHC uses Unsafe and so does not run on later java version
maxJavaVersionForTests.set(JavaVersion.VERSION_1_8)
}
}

tasks.withType<Test>().configureEach {
systemProperty("testLatestDeps", latestDepTest)
// async-http-client 3.0 requires java 11
// We are not using minJavaVersionSupported for latestDepTest because that way the instrumentation
// gets compiled with java 11 when running latestDepTest. This causes play-mvc-2.4 latest dep tests
// to fail because they require java 8 and instrumentation compiled with java 11 won't apply.
if (latestDepTest && testJavaVersion.isJava8) {
enabled = false
}
}

// async-http-client 2.0.0 does not work with Netty versions newer than this due to referencing an
// internal file.
if (!(findProperty("testLatestDeps") as Boolean)) {
if (!latestDepTest) {
configurations.configureEach {
if (!name.contains("muzzle")) {
resolutionStrategy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
import java.lang.reflect.Method;
import java.net.URI;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.asynchttpclient.AsyncCompletionHandler;
import org.asynchttpclient.AsyncHttpClient;
import org.asynchttpclient.DefaultAsyncHttpClientConfig;
import org.asynchttpclient.Dsl;
import org.asynchttpclient.Request;
import org.asynchttpclient.RequestBuilder;
Expand All @@ -31,11 +34,26 @@ class AsyncHttpClientTest extends AbstractHttpClientTest<Request> {
private static final int CONNECTION_TIMEOUT_MS = (int) CONNECTION_TIMEOUT.toMillis();

// request timeout is needed in addition to connect timeout on async-http-client versions 2.1.0+
private static final AsyncHttpClient client =
Dsl.asyncHttpClient(
Dsl.config()
.setConnectTimeout(CONNECTION_TIMEOUT_MS)
.setRequestTimeout(CONNECTION_TIMEOUT_MS));
private static final AsyncHttpClient client = Dsl.asyncHttpClient(configureTimeout(Dsl.config()));

private static DefaultAsyncHttpClientConfig.Builder configureTimeout(
DefaultAsyncHttpClientConfig.Builder builder) {
setTimeout(builder, "setConnectTimeout", CONNECTION_TIMEOUT_MS);
setTimeout(builder, "setRequestTimeout", CONNECTION_TIMEOUT_MS);
return builder;
}

private static void setTimeout(
DefaultAsyncHttpClientConfig.Builder builder, String methodName, int timeout) {
boolean testLatestDeps = Boolean.getBoolean("testLatestDeps");
try {
Method method =
builder.getClass().getMethod(methodName, testLatestDeps ? Duration.class : int.class);
method.invoke(builder, testLatestDeps ? Duration.ofMillis(timeout) : timeout);
} catch (Exception exception) {
throw new IllegalStateException("Failed to set timeout " + methodName, exception);
}
}

@Override
public Request buildRequest(String method, URI uri, Map<String, String> headers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,9 @@ public InstrumentationExtension getInstrumentationExtension() {
protected RedisClient createClient(String uri) {
return RedisClient.create(uri);
}

@Override
protected boolean connectHasSpans() {
return Boolean.getBoolean("testLatestDeps");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ boolean testCallback() {
return true;
}

protected boolean connectHasSpans() {
return false;
}

@Test
void testConnectUsingGetOnConnectionFuture() throws Exception {
RedisClient testConnectionClient = RedisClient.create(embeddedDbUri);
Expand All @@ -103,8 +107,13 @@ void testConnectUsingGetOnConnectionFuture() throws Exception {
cleanup.deferCleanup(testConnectionClient::shutdown);

assertThat(connection1).isNotNull();
// Lettuce tracing does not trace connect
assertThat(getInstrumentationExtension().spans()).isEmpty();
if (connectHasSpans()) {
// ignore CLIENT SETINFO traces
getInstrumentationExtension().waitForTraces(2);
} else {
// Lettuce tracing does not trace connect
assertThat(getInstrumentationExtension().spans()).isEmpty();
}
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension;
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -22,13 +21,6 @@
abstract class AbstractLettuceClientTest {
protected static final Logger logger = LoggerFactory.getLogger(AbstractLettuceClientTest.class);

@RegisterExtension
protected static final InstrumentationExtension testing = AgentInstrumentationExtension.create();

public InstrumentationExtension getInstrumentationExtension() {
return testing;
}

@RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create();

protected static final int DB_INDEX = 0;
Expand All @@ -40,19 +32,16 @@ public InstrumentationExtension getInstrumentationExtension() {
.waitingFor(Wait.forLogMessage(".*Ready to accept connections.*", 1));

protected static RedisClient redisClient;

protected static StatefulRedisConnection<String, String> connection;

protected abstract RedisClient createClient(String uri);

protected static String host;

protected static String ip;

protected static int port;

protected static String embeddedDbUri;

protected abstract RedisClient createClient(String uri);

protected abstract InstrumentationExtension getInstrumentationExtension();

protected ContainerConnection newContainerConnection() {
GenericContainer<?> server =
new GenericContainer<>("redis:6.2.3-alpine")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import io.lettuce.core.api.sync.RedisCommands;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
import io.opentelemetry.semconv.NetworkAttributes;
import io.opentelemetry.semconv.ServerAttributes;
import io.opentelemetry.semconv.incubating.DbIncubatingAttributes;
Expand Down Expand Up @@ -59,23 +60,76 @@ void testAuthCommand() throws Exception {

assertThat(result).isEqualTo("OK");

getInstrumentationExtension()
.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("AUTH")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(NetworkAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, ip),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(ServerAttributes.SERVER_ADDRESS, host),
equalTo(ServerAttributes.SERVER_PORT, port),
equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"),
equalTo(DbIncubatingAttributes.DB_STATEMENT, "AUTH ?"))
.hasEventsSatisfyingExactly(
event -> event.hasName("redis.encode.start"),
event -> event.hasName("redis.encode.end"))));
if (Boolean.getBoolean("testLatestDeps")) {
getInstrumentationExtension()
.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("CLIENT")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(NetworkAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, ip),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(ServerAttributes.SERVER_ADDRESS, host),
equalTo(ServerAttributes.SERVER_PORT, port),
equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"),
equalTo(
DbIncubatingAttributes.DB_STATEMENT,
"CLIENT SETINFO lib-name Lettuce"))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("CLIENT")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(NetworkAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, ip),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(ServerAttributes.SERVER_ADDRESS, host),
equalTo(ServerAttributes.SERVER_PORT, port),
equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"),
OpenTelemetryAssertions.satisfies(
DbIncubatingAttributes.DB_STATEMENT,
stringAssert ->
stringAssert.startsWith("CLIENT SETINFO lib-ver")))),
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("AUTH")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(NetworkAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, ip),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(ServerAttributes.SERVER_ADDRESS, host),
equalTo(ServerAttributes.SERVER_PORT, port),
equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"),
equalTo(DbIncubatingAttributes.DB_STATEMENT, "AUTH ?"))
.hasEventsSatisfyingExactly(
event -> event.hasName("redis.encode.start"),
event -> event.hasName("redis.encode.end"))));

} else {
getInstrumentationExtension()
.waitAndAssertTraces(
trace ->
trace.hasSpansSatisfyingExactly(
span ->
span.hasName("AUTH")
.hasKind(SpanKind.CLIENT)
.hasAttributesSatisfyingExactly(
equalTo(NetworkAttributes.NETWORK_TYPE, "ipv4"),
equalTo(NetworkAttributes.NETWORK_PEER_ADDRESS, ip),
equalTo(NetworkAttributes.NETWORK_PEER_PORT, port),
equalTo(ServerAttributes.SERVER_ADDRESS, host),
equalTo(ServerAttributes.SERVER_PORT, port),
equalTo(DbIncubatingAttributes.DB_SYSTEM, "redis"),
equalTo(DbIncubatingAttributes.DB_STATEMENT, "AUTH ?"))
.hasEventsSatisfyingExactly(
event -> event.hasName("redis.encode.start"),
event -> event.hasName("redis.encode.end"))));
}
}
}
Loading
Loading